JPA即Java Persistence API,它的查询语言是面向对象而非面向数据库,可以看作是Hibernate hql的等价物。这一节记录Springboot整合JPA反向创建数据库表并实现增删改查功能。
1、创建springboot项目,如下:
添加web、JPA、mysql数据库驱动依赖
2、完成JPA和数据源配置
# 开发环境
server.port=8082
# 自动生成数据库表
spring.jpa.hibernate.ddl-auto=update
# 数据源
spring.datasource.url=jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# jpa配置
# 在控制台显示SQL
spring.jpa.show-sql=true
spring.thymeleaf.cache=false
3、创建层级结构
3.1、创建层级结构目录
3.2 创建Book实体类,用于映射生成book数据库表
package com.galago.demo_jpa.entity;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.util.Date;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true)
private String bookName;
@Column(nullable = true)
private String author;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date publishTime;
public Integer getId() {
return id;
}
public String getBookName() {
return bookName;
}
public String getAuthor() {
return author;
}
public Date getPublishTime() {
return publishTime;
}
public void setId(Integer id) {
this.id = id;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPublishTime(Date publishTime) {
this.publishTime = publishTime;
}
public Book(Integer id, String bookName, String author, Date publishTime) {
this.id = id;
this.bookName = bookName;
this.author = author;
this.publishTime = publishTime;
}
public Book(String bookName, String author, Date publishTime) {
this.bookName = bookName;
this.author = author;
this.publishTime = publishTime;
}
public Book() {
}
}
启动项目生成book数据库表,运行结果如下:
3.3 编写BookRepo接口类,并extends JpaRepository接口
package com.galago.demo_jpa.repo;
import com.galago.demo_jpa.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookRepo extends JpaRepository<Book, Integer> {
// 该接口类包含增删改查等功能接口,减少了很多编写代码
}
查看JpaRepository接口源码,其内置了增删改查等功能,如下
3.4 编写BookController类
package com.galago.demo_jpa.controller;
import com.galago.demo_jpa.entity.Book;
import com.galago.demo_jpa.repo.BookRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@RequestMapping("/book")
@Controller
public class UserController {
@Autowired
private BookRepo bookRepo;
@RequestMapping("/getbook")
@ResponseBody
public List<Book> getbook() {
return bookRepo.findAll();
}
@PostMapping("/add")
@ResponseBody
public Book addBook(Book book) {
return bookRepo.save(book);
}
@RequestMapping("/del")
@ResponseBody
public void deleteById(Integer id) {
System.out.println(id);
bookRepo.deleteById(id);
}
}
4、测试
参考: