本章將通過使用SpringBoot訪問MySQL結合SpringDataJPA完成CRUD(Create,Read,Update,Delete)簡單操作。
一、瞭解SpringDataJPA
Spring Data是一個用於簡化數據庫訪問,並支持雲服務的開源框架,其主要目標是使得對數據的訪問變得方便快捷。Spring Data包含多個子項目,spring-data-jpa就是其子項目之一。JPA(Java Persistence API)是一種Java持久化解決方案,負責把數據保存到數據庫,實際上它就是一種標準、規範,而不是具體的實現。JPA屬於重量級的,因爲它需要運行在JAVA EE容器中,而Spring Data JPA提供了輕量級的實現,在任何servlet容器中都可以運行。
Spring Data JPA相對於JAVA EE中的JPA,配置更簡單,以輕量級的方式實現了部分在 EJB 容器環境下才具有的功能,將 EntityManager 的創建與銷燬、事務管理等代碼抽取出來,並由其統一管理,並且極大的簡化了數據庫訪問層的代碼。
官網地址:http://projects.spring.io/spring-data-jpa/
二、SpringBoot整合SpringDataJPA
1.添加依賴關係
打開POM文件,添加SpringBoot對MySQL以及SpringDataJPA的支持依賴。
添加mysql-connector-java的支持<!--引入MySQL的依賴關係-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
添加spring-data-jpa的支持<!--引入JPA的依賴關係-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2.配置數據源以及JPA
MySQL數據庫是我事先安裝好的,我在本地安裝了MySQL 5.7.20.0版本,並且創建了一個名叫test的數據庫,如果需求可以查看安裝MySQL詳解MySQL數據庫管理工具SQLyog,在test數據庫下新建表結構t_user,詳情如下:
修改application.properties文件,添加數據源配置。
修改application.properties文件,添加JPA配置。
JPA的配置了show-sql用來在控制檯輸出JPA自動生成的sql語句。
3.編寫實體類
根據數據庫中的字段來創建一個user實體類作爲對應操作。
package com.mxy.springboot.entity;
import javax.persistence.*;
@Entity
@Table(name="t_user")
public class User {
@Id
@GeneratedValue
@Column(name = "t_id")
private Long id;
@Column(name = "t_name")
private String name;
@Column(name = "t_age")
private String age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name = "t_address")
private String address;
}
4.編寫控制器Controller
我們採用@RestController註解來編寫一個控制器
package com.mxy.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/user")
public class JPAController {
}
5.創建JPA接口類
創建UserJPA接口並且繼承SpringDataJPA內的接口作爲父類,UserJPA繼承了JpaRepository接口(SpringDataJPA提供的簡單數據操作接口)、JpaSpecificationExecutor(SpringDataJPA提供的複雜查詢接口)、Serializable(序列化接口)。
package com.mxy.springboot.jpa;
import com.mxy.springboot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.io.Serializable;
public interface UserJPA extends JpaRepository<User,Long>,JpaSpecificationExecutor<User>,Serializable {
}
我們並不需要做其他的任何操作了,因爲SpringBoot以及SpringDataJPA會爲我們全部搞定,SpringDataJPA內部使用了類代理的方式讓繼承了它接口的子接口都以spring管理的Bean的形式存在。以上,我們已經完成了SpringBoot整合JPA的工作,接下來我們將使用SpringDataJPA完成數據的CRUD(Create,Read,Update,Delete)簡單操作。
三、使用SpringDataJPA完成數據的CRUD
JPAController控制器內編寫代碼,完成數據的CRUD操作。
1.添加新增、更新方法
package com.mxy.springboot.controller;
import com.mxy.springboot.entity.User;
import com.mxy.springboot.jpa.UserJPA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/user")
public class JPAController {
@Autowired
private UserJPA userJPA;
/**
* 數據新增或更新,save方法可以執行添加也可以執行更新,如果需要執行持久化的實體存在主鍵值則更新數據,如果不存在則添加數據。
*/
@RequestMapping(value = "/save", method = RequestMethod.GET)
public User save(User user) {
return userJPA.save(user);
}
}
運行程序,查看結果,添加一條用戶信息到數據庫中,請求地址:http://localhost:8080/user/save?name=zhangsan&age=20&address=beijing
查看數據庫,保存成功。
更新地址爲“北京市朝陽區”,請求地址:http://localhost:8080/user/save?id=1&address=北京市朝陽區
查看數據庫結果,更新數據成功。
2.查詢方法
JPAController中增加查詢列表的方法
/**
* 查詢用戶信息
* */
@RequestMapping(value = "/list",method = RequestMethod.GET)
public List<User> list(){
return userJPA.findAll();
}
其中userJps.findAll()方法就是SpringDataJPA爲我們提供的內置方法,它可以查詢表內所有的數據。
運行程序,請求地址:http://localhost:8080/user/list
3.添加刪除方法
/**
* 刪除用戶信息,刪除信息後返回剩餘信息
* */
@RequestMapping(value = "/delete",method = RequestMethod.GET)
public List<User> delete(Long id){
userJPA.deleteById(id);
return userJPA.findAll();
}
執行結果,請求地址:http://localhost:8080/user/delete?id=1
可以看到,成功刪除id=1的記錄。