1 添加依赖
在pom.xml中添加SpringBoot JPA使用必须的相关依赖(主要依赖为:spring-boot-starter-data-jpa、spring-boot-starter-web、mysql-connector-java、lombok)。
在初始化SpringBoot项目时勾选,即可自动添加。
<?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.example</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot</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-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> <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> <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>
2 配置JPA连接数据库相关信息
在application.yml中配置数据库相关信息。
spring: datasource: url: jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver jpa: show-sql: true properties: hibernate: format_sql: true
3 根据数据库表信息设计entity类
数据库表信息:
在entity包中创建出相应的类:
package com.example.entity; import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; @Entity @Data public class Book { @Id private Integer id; private String name; private String author; }
4 根据entity.Book类创建数据库访问接口
package com.example.repository; import com.example.entity.Book; import org.springframework.data.jpa.repository.JpaRepository; public interface BookRepository extends JpaRepository<Book, Integer> { }
5 测试
在BookRepository上右键,创建相应测试类。
点击OK后,会在相应的目录中创建测试类:
在该类中编写相应的测试方法,运行进行测试:
package com.example.repository; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.persistence.Temporal; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest class BookRepositoryTest { @Autowired private BookRepository bookRepository; @Test void findAll() { System.out.println(bookRepository.findAll()); } }
6 创建controller类,为前端提供数据接口
package com.example.controller; import com.example.entity.Book; import com.example.repository.BookRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/book") public class BookHandler { @Autowired BookRepository bookRepository; @GetMapping("findAll") public List<Book> findAll() { return bookRepository.findAll(); } }
至此,一个前端 —> SpringBoot —> 数据库的工作流程编写完毕。
工作流程