Spring Boot 2.X快速整合jpa過程解析
一、JPA介紹
JPA是Java Persistence API的簡(jiǎn)稱,中文名Java持久層API,是JDK 5.0注解或XML描述對(duì)象-關(guān)系表的映射關(guān)系,并將運(yùn)行期的實(shí)體對(duì)象持久化到數(shù)據(jù)庫中。
SpringData是Spring的一個(gè)子項(xiàng)目。用于簡(jiǎn)化數(shù)據(jù)庫訪問,支持NoSQL和關(guān)系數(shù)據(jù)存儲(chǔ),其主要目標(biāo)是使數(shù)據(jù)庫的訪問變得方便快捷。
Spring Data JPA致力于減少數(shù)據(jù)訪問層(DAO)的開發(fā)量,開發(fā)者唯一要做的就只是聲明持久層的接口,其他都交給Spring Data JPA 來幫你完成!
二、Spring Data JPA類結(jié)構(gòu)圖
1、類的結(jié)構(gòu)關(guān)系圖

三、代碼實(shí)現(xiàn)
1、添加對(duì)應(yīng)的Starter
pom.xml中添加下面依賴
<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>
<scope>runtime</scope>
</dependency>
2、添加連接數(shù)據(jù)庫的配置
#修改tomcat默認(rèn)端口號(hào) server.port=8090 #配置數(shù)據(jù)源信息 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.0.1:3306/test spring.datasource.username=root spring.datasource.password=123456 #配置jpa spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jackson.serialization.indent_output=true
3、主要代碼
UserRepository.java
/**
* 注意:
* 1.這里這里是interface,不是class
* 2.CrudRepository里面的泛型,第一個(gè)是實(shí)體類,第二個(gè)是主鍵的類型
*/
public interface UserRepository extends CrudRepository<User, Integer> {
@Query("from User where id =:id ")
public User getUser(@Param("id") Integer id);
}
User.java
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserController.java
@RestController
public class UserController {
@Autowired
UserService service;
/**
* 根據(jù)id 查詢
*/
@RequestMapping("/getUser/{id}")
public User getUser(@PathVariable("id") Integer id) {
return service.getUser(id);
}
/**
* 根據(jù)id 刪除
*/
@RequestMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") Integer id) {
service.deleteUser(id);
return "刪除成功!";
}
}
user表數(shù)據(jù)截圖如下:

項(xiàng)目啟動(dòng)成功后訪問http://localhost:8090/getUser/2和http://localhost:8090/deleteUser/2后截圖如下:


到此發(fā)送Spring Boot整合JPA全部實(shí)現(xiàn),有問題歡迎留言溝通哦!
完整源碼地址: https://github.com/suisui2019/springboot-study
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA2020 Plugins不能用的解決辦法及Plugins 搜索不了插件的問題
這篇文章主要介紹了IDEA2020 Plugins不能用的解決辦法,文中給大家介紹了Intellij IDEA 2020.1 的Plugins 搜索不了插件,連接超時(shí)的問題,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-06-06
解決引用jip-common jar包,報(bào)401 Unauthorized錯(cuò)誤問題
這篇文章主要介紹了解決引用jip-common jar包,報(bào)401 Unauthorized錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
springboot對(duì)象為null的屬性在json中不顯示的解決
這篇文章主要介紹了springboot對(duì)象為null的屬性在json中不顯示的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
使用Mybatis Plus整合多數(shù)據(jù)源和讀寫分離的詳細(xì)過程
這篇文章主要介紹了Mybatis Plus整合多數(shù)據(jù)源和讀寫分離的詳細(xì)過程,mybatisplus可以整合阿里的分布式事務(wù)組件seata,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-09-09
SpringAOP中的動(dòng)態(tài)代理技術(shù)深入解析
這篇文章主要介紹了SpringAOP中的動(dòng)態(tài)代理技術(shù)深入解析,spring默認(rèn)使用JDK動(dòng)態(tài)代理實(shí)現(xiàn)AOP,類如果實(shí)現(xiàn)了接口,spring就會(huì)用JDK動(dòng)態(tài)代理實(shí)現(xiàn)AOP,如果目標(biāo)類沒有實(shí)現(xiàn)接口,spring則使用Cglib動(dòng)態(tài)代理來實(shí)現(xiàn)AOP,需要的朋友可以參考下2024-01-01
SpringBoot2 集成log4j2日志框架的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot2 集成log4j2日志框架的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
java基礎(chǔ)實(shí)現(xiàn)猜數(shù)字小游戲
這篇文章主要為大家詳細(xì)介紹了java基礎(chǔ)實(shí)現(xiàn)猜數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11

