使用 SpringBoot 和 Spring Data JPA 构建一个 CRUD 示例

本篇文档将指导你如何使用 SpringBootSpring Data JPAIntellij IDEA 构建一个简单的应用程序,并实现 CRUD(创建、读取、更新、删除)功能。

开始之前

本篇教程涉及到的软件介绍如下:

  • Spring Data JPA:JPA(Java Persistence API,Java 持久层 API)是一种规范,是 JDK 5.0 注解或 XML 描述对象与关系表的映射关系,并将运行期的实体对象持久化到数据库中。Spring Data JPA 是一个 Java 对象映射关系的解决方案的 ORM(Object-Relational Mapping)框架,是一个将面向对象的域模型映射到关系数据库的开源框架。

  • Intellij IDEA:IntelliJ IDEA 是一种商业化销售的 Java 集成开发环境(Integrated Development Environment,IDE)工具软件。它所拥有诸多插件,可以提高我们的工作效率。

  • Maven:Maven 是 Java 中功能强大的项目管理工具,可以根据 pom.xml 文件中的配置自动下载和导入 Jar 文件。这个特性减少了不同版本 Jar 文件之间的冲突。

  • Spring:Spring 是 Java 中最流行的框架之一,越来越多的企业使用 Spring 框架来构建他们的项目。Spring Boot 构建在传统的 Spring 框架之上。因此,它提供了 Spring 的所有特性,而且比 Spring 更易用。

  • Postman: Postman 是一个用于 API 测试的应用程序。它是一个 HTTP 客户端,利用图形用户界面测试 HTTP 请求,以获得需要进行验证的不同类型的响应。

配置环境

1. 安装构建 MatrixOne

按照步骤介绍完成安装单机版 MatrixOne 0.6,在 MySQL 客户端新建一个命名为 test 数据库。

  1. mysql> create database test;

2. 使用 IntelliJ IDEA 创建一个新的 Spring Boot 项目

选择 Spring Initializer,按需命名项目名称。

image-20221027094625081

选择如下依赖项:

  • Spring Web
  • JDBC API
  • Spring Data JPA
  • MySQL Driver

image-20221027101504418

点击 Create 创建项目。依赖项列在 pom.xml 文件中。通常你无需修改任何东西。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>jpademo</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>jpademo</name>
  9. <description>jpademo</description>
  10. <properties>
  11. <java.version>1.8</java.version>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  14. <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-data-jpa</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-jdbc</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>mysql</groupId>
  31. <artifactId>mysql-connector-java</artifactId>
  32. <scope>runtime</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. <exclusions>
  39. <exclusion>
  40. <groupId>org.junit.vintage</groupId>
  41. <artifactId>junit-vintage-engine</artifactId>
  42. </exclusion>
  43. </exclusions>
  44. </dependency>
  45. </dependencies>
  46. <dependencyManagement>
  47. <dependencies>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-dependencies</artifactId>
  51. <version>${spring-boot.version}</version>
  52. <type>pom</type>
  53. <scope>import</scope>
  54. </dependency>
  55. </dependencies>
  56. </dependencyManagement>
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.apache.maven.plugins</groupId>
  61. <artifactId>maven-compiler-plugin</artifactId>
  62. <version>3.8.1</version>
  63. <configuration>
  64. <source>1.8</source>
  65. <target>1.8</target>
  66. <encoding>UTF-8</encoding>
  67. </configuration>
  68. </plugin>
  69. <plugin>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-maven-plugin</artifactId>
  72. <version>2.3.7.RELEASE</version>
  73. <configuration>
  74. <mainClass>com.example.jpademo.JpademoApplication</mainClass>
  75. </configuration>
  76. <executions>
  77. <execution>
  78. <id>repackage</id>
  79. <goals>
  80. <goal>repackage</goal>
  81. </goals>
  82. </execution>
  83. </executions>
  84. </plugin>
  85. </plugins>
  86. </build>
  87. </project>

3. 修改 application.properties 文件

进入到 resources 文件目录下,配置 application.properties 文件,完成 MatrixOne 连接。

  1. # Application Name
  2. spring.application.name=jpademo
  3. # Database driver
  4. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  5. # Data Source name
  6. spring.datasource.name=defaultDataSource
  7. # Database connection url, modify to MatrixOne address and port, with parameters
  8. spring.datasource.url=jdbc:mysql://127.0.0.1:6001/test?characterSetResults=UTF-8&continueBatchOnError=false&useServerPrepStmts=true&alwaysSendSetIsolation=false&useLocalSessionState=true&zeroDateTimeBehavior=CONVERT_TO_NULL&failoverReadOnly=false&serverTimezone=Asia/Shanghai&socketTimeout=30000
  9. # Database username and password
  10. spring.datasource.username=dump
  11. spring.datasource.password=111
  12. # Web application port
  13. server.port=8080
  14. # Hibernate configurations
  15. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
  16. spring.jpa.properties.hibernate.id.new_generator_mappings = false
  17. spring.jpa.properties.hibernate.format_sql = true
  18. spring.jpa.hibernate.ddl-auto = validate

4. 在 MatrixOne 中新建表并插入数据

使用 MySQL 客户端连接到 MatrixOne 并执行以下 SQL 语句。你可以将这些 SQL 语句保存在 /resource/database/ 目录下的 book.sql 中。

  1. mysql> USE test;
  2. mysql> CREATE TABLE IF NOT EXISTS `book` (
  3. `id` int(11) NOT NULL AUTO_INCREMENT,
  4. `author` varchar(255) DEFAULT NULL,
  5. `category` varchar(255) DEFAULT NULL,
  6. `name` varchar(255) DEFAULT NULL,
  7. `pages` int(11) DEFAULT NULL,
  8. `price` int(11) DEFAULT NULL,
  9. `publication` varchar(255) DEFAULT NULL,
  10. PRIMARY KEY (`id`)
  11. );
  12. mysql> INSERT INTO `book` (`id`, `author`, `category`, `name`, `pages`, `price`, `publication`) VALUES
  13. (1, 'Antoine de Saint-Exupery', 'Fantancy', 'The Little Prince', 100, 50, 'Amazon'),
  14. (2, 'J. K. Rowling', 'Fantancy', 'Harry Potter and the Sorcerer''s Stone', 1000, 200, 'Amazon'),
  15. (3, 'Lewis Carroll', 'Fantancy', 'Alice''s Adventures in Wonderland', 1500, 240, 'Amazon');

编写代码

完成环境配置后,我们编写代码来实现一个简单的 CRUD 应用程序。

在完成编写编码后,你将拥有一个如下所示的项目结构。你可以预先创建这些包和 java 类。

我们将为这个演示应用程序编写创建、更新、插入、删除和选择操作。

image-20221027105233860

1. BookStoreController.java

  1. package com.example.jpademo.controller;
  2. import com.example.jpademo.entity.Book;
  3. import com.example.jpademo.services.IBookStoreService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.util.List;
  10. @Controller
  11. @RequestMapping("bookservice")
  12. public class BookStoreController {
  13. @Autowired
  14. private IBookStoreService service;
  15. @GetMapping("books")
  16. public ResponseEntity<List<Book>> getBooks(){
  17. List<Book> books = service.getBooks();
  18. return new ResponseEntity<List<Book>>(books, HttpStatus.OK);
  19. }
  20. @GetMapping("books/{id}")
  21. public ResponseEntity<Book> getBook(@PathVariable("id") Integer id){
  22. Book book = service.getBook(id);
  23. return new ResponseEntity<Book>(book, HttpStatus.OK);
  24. }
  25. @PostMapping("books")
  26. public ResponseEntity<Book> createBook(@RequestBody Book book){
  27. Book b = service.createBook(book);
  28. return new ResponseEntity<Book>(b, HttpStatus.OK);
  29. }
  30. @PutMapping("books/{id}")
  31. public ResponseEntity<Book> updateBook(@PathVariable("id") int id, @RequestBody Book book){
  32. Book b = service.updateBook(id, book);
  33. return new ResponseEntity<Book>(b, HttpStatus.OK);
  34. }
  35. @DeleteMapping("books/{id}")
  36. public ResponseEntity<String> deleteBook(@PathVariable("id") int id){
  37. boolean isDeleted = service.deleteBook(id);
  38. if(isDeleted){
  39. String responseContent = "Book has been deleted successfully";
  40. return new ResponseEntity<String>(responseContent,HttpStatus.OK);
  41. }
  42. String error = "Error while deleting book from database";
  43. return new ResponseEntity<String>(error,HttpStatus.INTERNAL_SERVER_ERROR);
  44. }
  45. }

2. BooStoreDAO.java

  1. package com.example.jpademo.dao;
  2. import com.example.jpademo.entity.Book;
  3. import org.springframework.stereotype.Repository;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import javax.persistence.EntityManager;
  6. import javax.persistence.PersistenceContext;
  7. import javax.persistence.Query;
  8. import java.util.List;
  9. @Transactional
  10. @Repository
  11. public class BookStoreDAO implements IBookStoreDAO {
  12. @PersistenceContext
  13. private EntityManager entityManager;
  14. /**
  15. * This method is responsible to get all books available in database and return it as List<Book>
  16. */
  17. @SuppressWarnings("unchecked")
  18. @Override
  19. public List<Book> getBooks() {
  20. String hql = "FROM Book as atcl ORDER BY atcl.id";
  21. return (List<Book>) entityManager.createQuery(hql).getResultList();
  22. }
  23. /**
  24. * This method is responsible to get a particular Book detail by given book id
  25. */
  26. @Override
  27. public Book getBook(int bookId) {
  28. return entityManager.find(Book.class, bookId);
  29. }
  30. /**
  31. * This method is responsible to create new book in database
  32. */
  33. @Override
  34. public Book createBook(Book book) {
  35. entityManager.persist(book);
  36. Book b = getLastInsertedBook();
  37. return b;
  38. }
  39. /**
  40. * This method is responsible to update book detail in database
  41. */
  42. @Override
  43. public Book updateBook(int bookId, Book book) {
  44. //First We are taking Book detail from database by given book id and
  45. // then updating detail with provided book object
  46. Book bookFromDB = getBook(bookId);
  47. bookFromDB.setName(book.getName());
  48. bookFromDB.setAuthor(book.getAuthor());
  49. bookFromDB.setCategory(book.getCategory());
  50. bookFromDB.setPublication(book.getPublication());
  51. bookFromDB.setPages(book.getPages());
  52. bookFromDB.setPrice(book.getPrice());
  53. entityManager.flush();
  54. //again i am taking updated result of book and returning the book object
  55. Book updatedBook = getBook(bookId);
  56. return updatedBook;
  57. }
  58. /**
  59. * This method is responsible for deleting a particular(which id will be passed that record)
  60. * record from the database
  61. */
  62. @Override
  63. public boolean deleteBook(int bookId) {
  64. Book book = getBook(bookId);
  65. entityManager.remove(book);
  66. //we are checking here that whether entityManager contains earlier deleted book or not
  67. // if contains then book is not deleted from DB that's why returning false;
  68. boolean status = entityManager.contains(book);
  69. if(status){
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * This method will get the latest inserted record from the database and return the object of Book class
  76. * @return book
  77. */
  78. private Book getLastInsertedBook(){
  79. String hql = "from Book order by id DESC";
  80. Query query = entityManager.createQuery(hql);
  81. query.setMaxResults(1);
  82. Book book = (Book)query.getSingleResult();
  83. return book;
  84. }
  85. }

3. IBookStoreDAO.java

  1. package com.example.jpademo.dao;
  2. import com.example.jpademo.entity.Book;
  3. import java.util.List;
  4. public interface IBookStoreDAO {
  5. List<Book> getBooks();
  6. Book getBook(int bookId);
  7. Book createBook(Book book);
  8. Book updateBook(int bookId,Book book);
  9. boolean deleteBook(int bookId);
  10. }

4. Book.java

  1. package com.example.jpademo.entity;
  2. import javax.persistence.*;
  3. import java.io.Serializable;
  4. @Entity
  5. @Table(name="book")
  6. public class Book implements Serializable {
  7. private static final long serialVersionUID = 1L;
  8. @Id
  9. @GeneratedValue(strategy= GenerationType.AUTO)
  10. @Column(name="id")
  11. private int id;
  12. @Column(name="name")
  13. private String name;
  14. @Column(name="author")
  15. private String author;
  16. @Column(name="publication")
  17. private String publication;
  18. @Column(name="category")
  19. private String category;
  20. @Column(name="pages")
  21. private int pages;
  22. @Column(name="price")
  23. private int price;
  24. public int getId() {
  25. return id;
  26. }
  27. public void setId(int id) {
  28. this.id = id;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. public String getAuthor() {
  37. return author;
  38. }
  39. public void setAuthor(String author) {
  40. this.author = author;
  41. }
  42. public String getPublication() {
  43. return publication;
  44. }
  45. public void setPublication(String publication) {
  46. this.publication = publication;
  47. }
  48. public String getCategory() {
  49. return category;
  50. }
  51. public void setCategory(String category) {
  52. this.category = category;
  53. }
  54. public int getPages() {
  55. return pages;
  56. }
  57. public void setPages(int pages) {
  58. this.pages = pages;
  59. }
  60. public int getPrice() {
  61. return price;
  62. }
  63. public void setPrice(int price) {
  64. this.price = price;
  65. }
  66. }

5. BookStoreService.java

  1. package com.example.jpademo.services;
  2. import com.example.jpademo.dao.IBookStoreDAO;
  3. import com.example.jpademo.entity.Book;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import java.util.List;
  7. @Service
  8. public class BookStoreService implements IBookStoreService {
  9. @Autowired
  10. private IBookStoreDAO dao;
  11. @Override
  12. public List<Book> getBooks() {
  13. return dao.getBooks();
  14. }
  15. @Override
  16. public Book createBook(Book book) {
  17. return dao.createBook(book);
  18. }
  19. @Override
  20. public Book updateBook(int bookId, Book book) {
  21. return dao.updateBook(bookId, book);
  22. }
  23. @Override
  24. public Book getBook(int bookId) {
  25. return dao.getBook(bookId);
  26. }
  27. @Override
  28. public boolean deleteBook(int bookId) {
  29. return dao.deleteBook(bookId);
  30. }
  31. }

6. IBookStoreService.java

  1. package com.example.jpademo.services;
  2. import com.example.jpademo.entity.Book;
  3. import java.util.List;
  4. public interface IBookStoreService {
  5. List<Book> getBooks();
  6. Book createBook(Book book);
  7. Book updateBook(int bookId, Book book);
  8. Book getBook(int bookId);
  9. boolean deleteBook(int bookId);
  10. }

7. JpademoApplication

  1. package com.example.jpademo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class JpademoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(JpademoApplication.class, args);
  8. }
  9. }

测试

构建并启动这个项目。

image-20221027110133726

当出现下面的消息时,表示应用程序已经正常启动,你可以使用 Postman 调用 REST 端口。

  1. 2022-10-27 11:16:16.793 INFO 93488 --- [ main] com.example.jpademo.JpademoApplication : Starting JpademoApplication on nandeng-macbookpro.local with PID 93488 (/Users/nandeng/IdeaProjects/jpademo/target/classes started by nandeng in /Users/nandeng/IdeaProjects/jpademo)
  2. 2022-10-27 11:16:16.796 INFO 93488 --- [ main] com.example.jpademo.JpademoApplication : No active profile set, falling back to default profiles: default
  3. 2022-10-27 11:16:18.022 INFO 93488 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
  4. 2022-10-27 11:16:18.093 INFO 93488 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 50ms. Found 0 JPA repository interfaces.
  5. 2022-10-27 11:16:18.806 INFO 93488 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
  6. 2022-10-27 11:16:18.814 INFO 93488 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  7. 2022-10-27 11:16:18.814 INFO 93488 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
  8. 2022-10-27 11:16:18.886 INFO 93488 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  9. 2022-10-27 11:16:18.886 INFO 93488 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2005 ms
  10. 2022-10-27 11:16:19.068 INFO 93488 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
  11. 2022-10-27 11:16:19.119 INFO 93488 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.25.Final
  12. 2022-10-27 11:16:19.202 INFO 93488 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
  13. 2022-10-27 11:16:19.282 INFO 93488 --- [ main] com.zaxxer.hikari.HikariDataSource : defaultDataSource - Starting...
  14. 2022-10-27 11:16:20.025 INFO 93488 --- [ main] com.zaxxer.hikari.HikariDataSource : defaultDataSource - Start completed.
  15. 2022-10-27 11:16:20.035 INFO 93488 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
  16. 2022-10-27 11:16:21.929 INFO 93488 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
  17. 2022-10-27 11:16:21.937 INFO 93488 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
  18. 2022-10-27 11:16:22.073 WARN 93488 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
  19. 2022-10-27 11:16:22.221 INFO 93488 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  20. 2022-10-27 11:16:22.415 INFO 93488 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
  21. 2022-10-27 11:16:22.430 INFO 93488 --- [ main] com.example.jpademo.JpademoApplication : Started JpademoApplication in 6.079 seconds (JVM running for 8.765)
  22. 2022-10-27 11:16:40.180 INFO 93488 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
  23. 2022-10-27 11:16:40.183 INFO 93488 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  24. 2022-10-27 11:16:40.249 INFO 93488 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 66 ms

1. 获取 Book 列表,使用 GET 请求调用以下端口

  1. http://localhost:8080/bookservice/books

image-20221027112426189

2. 创建一个新 Book,使用 POST 请求调用以下端口

  1. http://localhost:8080/bookservice/books

将 Header 中的内容类型设置为 application/json,将 Request Body 设置为原始 JSON 有效负载。

  1. {
  2. "name": "The Lion, the Witch and the Wardrobe",
  3. "author": "C. S. Lewis",
  4. "publication": "Amazon",
  5. "category": "Fantancy",
  6. "pages": 123,
  7. "price": 10
  8. }

image-20221027115733788

3. 如需获取特定 Book,使用 GET 请求调用以下端口

  1. http://localhost:8080/bookservice/books/<id>

image-20221027115844378

4. 在数据库中升级 Book,使用 PUT 请求调用以下端口

  1. http://localhost:8080/bookservice/books/<id>

set content type as in header as application/json

set request body as raw with JSON payload

  • 将 Header 中的内容类型设置为 application/json

  • 将 Request Body 设置为原始 JSON 有效负载

  1. {
  2. "name": "Black Beauty",
  3. "author": "Anna Sewell",
  4. "publication": "Amazon",
  5. "category": "Fantancy",
  6. "pages": 134,
  7. "price": 12
  8. }

image-20221027120144112

5. 如需从数据库中删除特定的 Book,使用 DELETE 请求调用以下端口

  1. http://localhost:8080/bookservice/books/<id>

image-20221027120306830

  1. mysql> select * from book;
  2. +----+--------------------------+----------+----------------------------------+-------+-------+-------------+
  3. | id | author | category | name | pages | price | publication |
  4. +----+--------------------------+----------+----------------------------------+-------+-------+-------------+
  5. | 1 | Antoine de Saint-Exupery | Fantancy | The Little Prince | 100 | 50 | Amazon |
  6. | 2 | Anna Sewell | Fantancy | Black Beauty | 134 | 12 | Amazon |
  7. | 3 | Lewis Carroll | Fantancy | Alice's Adventures in Wonderland | 1500 | 240 | Amazon |
  8. +----+--------------------------+----------+----------------------------------+-------+-------+-------------+
  9. 3 rows in set (0.02 sec)