springboot整合JPA過(guò)程解析
這篇文章主要介紹了springboot整合JPA過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下


接下來(lái)具體看看是怎么弄的。
1、新建一個(gè)springboot項(xiàng)目,選擇web、data jdbc、data jpa、mysql driver。
2、建立以下目錄及結(jié)構(gòu):

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gong</groupId>
<artifactId>springbootjpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootjpa</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、在application.yml中配置連接數(shù)據(jù)庫(kù)和jpa相關(guān)配置
spring: datasource: url: jdbc:mysql://192.168.124.22:3306/jpa username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: #更新或者創(chuàng)建數(shù)據(jù)表結(jié)構(gòu) ddl-auto: update #控制臺(tái)顯示SQL show-sql: true
4、新建一個(gè)entity包,新建實(shí)體類User.java
package com.gong.springbootjpa.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
//使用JPA注解配置映射關(guān)系
@Entity //告訴JPA這是一個(gè)實(shí)體類(和數(shù)據(jù)表映射的類)
@Table(name = "tbl_user") //@Table來(lái)指定和哪個(gè)數(shù)據(jù)表對(duì)應(yīng);如果省略默認(rèn)表名就是user;
@JsonIgnoreProperties(value = {"hibernateLazyInitializer","handler"})
public class User {
@Id //這是一個(gè)主鍵
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增主鍵
private Integer id;
@Column(name = "last_name",length = 50) //這是和數(shù)據(jù)表對(duì)應(yīng)的一個(gè)列
private String lastName;
@Column //省略默認(rèn)列名就是屬性名
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
5、新建一個(gè)repository包,新建一個(gè)UserRepository.java
package com.gong.springbootjpa.repository;
import com.gong.springbootjpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
//繼承JpaRepository來(lái)完成對(duì)數(shù)據(jù)庫(kù)的操作,在JdbcRepository中指定實(shí)體類,數(shù)據(jù)庫(kù)中主鍵對(duì)應(yīng)的java類型
public interface UserRepository extends JpaRepository<User,Integer> {
}
6、新建一個(gè)controller包,新建一個(gè)UserController.java
經(jīng)過(guò)上述配置之后,我們就可以直接利用UserRepository中的一些方法進(jìn)行數(shù)據(jù)庫(kù)的操作啦,是不是很方便。
package com.gong.springbootjpa.controller;
import com.gong.springbootjpa.entity.User;
import com.gong.springbootjpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Integer id){
User user = userRepository.getOne(id);
return user;
}
@GetMapping("/user")
public User insertUser(User user){
User save = userRepository.save(user);
return save;
}
}
7、啟動(dòng)服務(wù)器
插入一條數(shù)據(jù):

查詢一條數(shù)據(jù):

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Netty分布式高性能工具類recycler的使用及創(chuàng)建
這篇文章主要為大家介紹了Netty分布式高性能工具類recycler的使用和創(chuàng)建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
MyBatis啟動(dòng)時(shí)控制臺(tái)無(wú)限輸出日志的原因及解決辦法
這篇文章主要介紹了MyBatis啟動(dòng)時(shí)控制臺(tái)無(wú)限輸出日志的原因及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-07-07
淺析Spring?Cloud?Gateway中的令牌桶限流算法
這篇文章主要為大家淺析了Spring?Cloud?Gateway中的令牌桶限流算法原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
SpringBoot項(xiàng)目啟動(dòng)時(shí)預(yù)加載操作方法
Spring Boot是一種流行的Java開(kāi)發(fā)框架,它提供了許多方便的功能來(lái)簡(jiǎn)化應(yīng)用程序的開(kāi)發(fā)和部署,這篇文章主要介紹了SpringBoot項(xiàng)目啟動(dòng)時(shí)預(yù)加載,需要的朋友可以參考下2023-09-09
Java 添加、更新和移除PDF超鏈接的實(shí)現(xiàn)方法
PDF超鏈接用一個(gè)簡(jiǎn)單的鏈接包含了大量的信息,滿足了人們?cè)诓徽加锰嗫臻g的情況下渲染外部信息的需求。這篇文章主要介紹了Java 添加、更新和移除PDF超鏈接的實(shí)現(xiàn)方法,需要的朋友可以參考下2019-05-05
java讀取枚舉類的值轉(zhuǎn)成list和map方式
這篇文章主要介紹了java讀取枚舉類的值轉(zhuǎn)成list和map方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

