微信公衆號:一個優秀的廢人 若有問題或建議,請後臺留言,我會盡力解決你的問題。php
如題,今天介紹 Spring Data JPA 的使用。前端
在介紹 Spring Data JPA 以前,首先介紹 Hibernate 。 Hibernate 使用 O/R 映射 (Object-Relation Mapping) 技術實現數據訪問, O/R 映射即將領域模型類與數據庫的表進行映射,經過程序操做對象而實現表數據操做的能力,讓數據訪問操做無需關注數據庫相關技術。java
Hibernate 主導了 EJB 3.0 的 JPA 規範, JPA 即 Java Persistence API。JPA 是一個基於 O/R 映射的標準協議(目前最新版本是 JPA 2.1)。所謂規範即只定義標準規制(如註解、接口),不提供實現,軟件提供商能夠按照標準規範來實現,而使用者只需按照規範中定義的方式來使用,而不用和軟件提供商的實現打交道。JPA 的主要實現由 Hibernate 、 EclipseLink 和 OpenJPA 等完成,咱們只要使用 JPA 來開發,不管是哪個開發方式都是同樣的。mysql
Spring Data JPA 是 Spring Data 的一個子項目,它經過基於 JPA 的 Repository 極大地減小了 JPA 做爲數據訪問方案的代碼量。git
簡而言之,JPA 是一種 ORM 規範,但並未提供 ORM 實現,而 Hibernate 是一個 ORM 框架,它提供了 ORM 實現。github
pom.xml 文件引入的依賴以下:web
<?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 http://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.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nasus</groupId>
<artifactId>jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jpa</name>
<description>jpa Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- JPA 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- web 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql 鏈接類 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok 依賴 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 單元測試依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製代碼
簡單說下,加入 JPA 依賴;mysql 鏈接類用於鏈接數據;web 啓動類,但凡是 web 應用都須要依賴它;lombok 用於簡化實體類。不會的看這篇舊文介紹:SpringBoot 實戰 (三) | 使用 LomBok算法
spring:
# 數據庫相關
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=true
username: root
password: 123456
# JPA 相關
jpa:
hibernate:
ddl-auto: update #ddl-auto:設爲 create 表示每次都從新建表
show-sql: true
複製代碼
package com.nasus.jpa.repository;
import com.nasus.jpa.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
/** * Project Name:springboot_jpa_demo <br/> * Package Name:com.nasus.jpa.repository <br/> * Date:2019/2/19 21:37 <br/> * <b>Description:</b> TODO: 描述該類的做用 <br/> * @author <a href="turodog@foxmail.com">nasus</a><br/> */
@Repository
public interface StudentRepository extends JpaRepository<Student,Integer>, CrudRepository<Student, Integer> {
}
複製代碼
從上圖,能夠看出 JpaRepository 繼承於 PangingAndSortingRepository 繼承於 CrudRepository 。spring
CrudRepository 提供基本的增刪改查PagingAndSortingRepository 提供分頁和排序方法;JpaRepository 提供 JPA 須要的方法。在使用的時候,能夠根據具體須要選中繼承哪一個接口。sql
使用這些接口的好處有:
package com.nasus.jpa.service;
import com.nasus.jpa.entity.Student;
import java.util.List;
/** * Project Name:springboot_jpa_demo <br/> * Package Name:com.nasus.jpa.service <br/> * Date:2019/2/19 21:41 <br/> * <b>Description:</b> TODO: 描述該類的做用 <br/> * @author <a href="turodog@foxmail.com">nasus</a><br/> */
public interface StudentService {
Student save(Student student);
Student findStudentById(Integer id);
void delete(Integer id);
void updateStudent(Student student);
List<Student> findStudentList();
}
複製代碼
實現類:
package com.nasus.jpa.service.impl;
import com.nasus.jpa.entity.Student;
import com.nasus.jpa.repository.StudentRepository;
import com.nasus.jpa.service.StudentService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/** * Project Name:springboot_jpa_demo <br/> * Package Name:com.nasus.jpa.service.impl <br/> * Date:2019/2/19 21:43 <br/> * <b>Description:</b> TODO: 描述該類的做用 <br/> * @author <a href="turodog@foxmail.com">nasus</a><br/> */
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
/** * 保存學生信息 * @param student * @return */
@Override
public Student save(Student student) {
return studentRepository.save(student);
}
/** * 根據 Id 查詢學生信息 * @param id * @return */
@Override
public Student findStudentById(Integer id) {
return studentRepository.findById(id).get();
}
/** * 刪除學生信息 * @param id */
@Override
public void delete(Integer id) {
Student student = this.findStudentById(id);
studentRepository.delete(student);
}
/** * 更新學生信息 * @param student */
@Override
public void updateStudent(Student student) {
studentRepository.save(student);
}
/** * 查詢學生信息列表 * @return */
@Override
public List<Student> findStudentList() {
return studentRepository.findAll();
}
}
複製代碼
package com.nasus.jpa.controller;
import com.nasus.jpa.entity.Student;
import com.nasus.jpa.service.StudentService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** * Project Name:springboot_jpa_demo <br/> * Package Name:com.nasus.jpa.controller <br/> * Date:2019/2/19 21:55 <br/> * <b>Description:</b> TODO: 描述該類的做用 <br/> * @author <a href="turodog@foxmail.com">nasus</a><br/> */
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@PostMapping("/save")
public Student saveStudent(@RequestBody Student student){
return studentService.save(student);
}
@GetMapping("/{id}")
public Student findStudentById(@PathVariable("id") Integer id){
return studentService.findStudentById(id);
}
@GetMapping("/list")
public List<Student> findStudentList(){
return studentService.findStudentList();
}
@DeleteMapping("/{id}")
public void deleteStudentById(@PathVariable("id") Integer id){
studentService.delete(id);
}
@PutMapping("/update")
public void updateStudent(@RequestBody Student student){
studentService.updateStudent(student);
}
}
複製代碼
其餘接口已經過 postman 測試,無問題。
以上爲 SpringBoot 使用 Spring Data JPA 訪問 Mysql 數據庫的教程。最後,對 Python 、Java 感興趣請長按二維碼關注一波,我會努力帶給大家價值,若是以爲本文對你哪怕有一丁點幫助,請幫忙點好看,讓更多人知道。
另外,關注以後在發送 1024 可領取免費學習資料。資料內容詳情請看這篇舊文:Python、C++、Java、Linux、Go、前端、算法資料分享