Spring Boot 整合持久层之Spring Data JPA
作者:一只小熊猫呀 发布时间:2022-07-29 04:00:38
整合Spring Data JPA
JPA (Java Persistence API)和 Spring Data 是两个范畴的概念。
Hibernate 是一个 ORM 框架,JPA 则是一种ORM,JPA 和 Hibernate 的关系就像 JDBC 与 JDBC 驱动,即 JPA 制定了 ORM 规范,而 Hibernate 是这些规范的实现(事实上,是现有 Hibernate 后有 JPA ,JPA 规范的起草也是 Hibernate 的作者),因此从功能上来说,JPA 相当于 Hibernate 的一个子集。
Spring Data 是 Spring 的一个子项目,致力于简化数据库访问,通过规范的方法名称来分析开发者的意图,进而减少数据库访问层的代码量。Spring Data 不仅支持关系型数据库,也支持非关系型数据库。Spring Data JPA 可以有效简化关系型数据库访问代码。
Spring Boot 整合 Spring Data JPA 步骤如下:
1. 创建数据库
创建数据库即可,不用创建表
创建数据库 jpa,如下
create database `jpa` default character set utf8;
2. 创建项目
创建 Spring Boot 项目,添加 MySQL 和 Spring Data JPA 的依赖
<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>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3. 数据库配置
在 application.properties 中配置数据库基本信息以及 JPA 相关配置
# 数据库基本配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
# JPA 配置
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
4. 创建实体类
@Entity(name = "t_book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "book_name",nullable = false)
private String name;
private String author;
private Float price;
@Transient
private String description;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", description='" + description + '\'' +
'}';
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
}
代码解释:
@Entity 注解表示该类是一个实体类,在项目启动时会根据该类生成一张表,表的名称即 @Entity 注解中的 name 的值,如果不配置 name ,默认表名为类名
所有的实体类都要有主键,@Id 注解表示改属性是一个主键,@GeneratedValue 注解表示主键自动生成,strategy 表示生成主键的策略
默认情况下,生成的表中字段的名称就是实体类中属性的名称,通过 @Column 注解可以定制生成的字段的属性,name 表示该属性对应的数据表中字段的名称,nullable 表示该字段非空
@Transient 注解表示在生成数据库中的表时,该属性被忽略,即不生成对应的字段
5. 创建 BookDao 接口
public interface BookDao extends JpaRepository<Book, Integer> {
List<Book> getBooksByAuthorStartingWith(String author);
List<Book> getBooksByPriceGreaterThan(Float price);
@Query(value = "select * from t_book where id=(select max(id) from t_book)", nativeQuery = true)
Book getMaxIdBook();
@Query("select b from t_book b where b.id>:id and b.author=:author")
List<Book> getBookByIdAndAuthor(@Param("author") String author, @Param("id") Integer id);
@Query("select b from t_book b where b.id<?2 and b.name like %?1%")
List<Book> getBooksByIdAndName(String name, Integer id);
}
代码解释:
自定义 BookDao 继承 JpaRepository 。JpaRepository 中提供了一些基本的数据库操作方法,有基本的增删改查、分页查询、排序查询等
getBooksByAuthorStartingWith() 方法表示查询以某个字符开始的所有的书
getBooksByPriceGreaterThan() 方法表示查询单价大于某个值的所有书
在Spring Data JPA 中,只要方法的定义符合既定规范,Spring Data JPA 就能分析出开发者的意图,从而避免开发中定义 SQL 。 所谓的既定规范,就是一定的方法命名规则,支持的命名规则如下:
| 关键字 | 方法命名 | sql where字句 |
| — | — | — |
| And | findByNameAndPwd | where name= ? and pwd =? |
| Or | findByNameOrSex | where name= ? or sex=? |
| Is,Equals | findById,findByIdEquals | where id= ? |
| Between | findByIdBetween | where id between ? and ? |
| LessThan | findByIdLessThan | where id < ? |
| LessThanEqual | findByIdLessThanEqual | where id <= ? |
| GreaterThan | findByIdGreaterThan | where id > ? |
| GreaterThanEqual | findByIdGreaterThanEqual | where id > = ? |
| After | findByIdAfter | where id > ? |
| Before | findByIdBefore | where id < ? |
| IsNull | findByNameIsNull | where name is null |
| isNotNull,NotNull | findByNameNotNull | where name is not null |
| Like | findByNameLike | where name like ? |
| NotLike | findByNameNotLike | where name not like ? |
| StartingWith | findByNameStartingWith | where name like ‘?%’ |
| EndingWith | findByNameEndingWith | where name like ‘%?’ |
| Containing | findByNameContaining | where name like ‘%?%’ |
| OrderBy | findByIdOrderByXDesc | where id=? order by x desc |
| Not | findByNameNot | where name <> ? |
| In | findByIdIn(Collection<?> c) | where id in (?) | | NotIn | findByIdNotIn(Collection<?> c) | where id not in (?) |
| True | findByAaaTue | where aaa = true |
| False | findByAaaFalse | where aaa = false |
| IgnoreCase | findByNameIgnoreCase | where UPPER(name)=UPPER(?) |
既定的方法命名规则不一定满足所有的开发需求,因此 Spring Data JPA 也支持自定义 JPQL 或者原生 SQL 。getMaxIdBook() 方法表示查询id最大的书,nativeQuery = true 表示使用原生的 SQL 查询
getBookByIdAndAuthor() 表示根据 id 和 author 进行查询,这里使用默认的 JPQL 语句。JPQL 是一种面向对象表达式语言,可以将 SQL 语法和简单查询语句绑定在一起,使用这种语言编写的查询是可以移植的,可以被编译成所有主流数据库服务器上的 SQL 。JPQL 与原生 SQL 语句类似,并且完全面向对象,通过类名和属性访问,而不是表名和表的属性。getBookByIdAndAuthor() 方法使用 :id、:name 这种方式来进行参数绑定。注意:这里使用的列名是属性名称,而不是数据库中的列的名称。
getBooksByIdAndName() 方法也是自定义 JPQL 查询,不同的是传参使用 ?1、?2 这种方式。注意:方法中的参数的顺序要与参数声明的顺序一致
如果 BookDao 中的方法设计修改操作,就需要添加 @Modifying 注解并添加事务
6. 创建 BookService
@Service
public class BookService {
@Autowired
BookDao bookDao;
public void addBook(Book book) {
bookDao.save(book);
}
public Page<Book> getBookByPage(Pageable pageable) {
return bookDao.findAll(pageable);
}
public List<Book> getBooksByAuthorStartingWith(String author) {
return bookDao.getBooksByAuthorStartingWith(author);
}
public List<Book> getBooksByPriceGreaterThan(Float price) {
return bookDao.getBooksByPriceGreaterThan(price);
}
public Book getMaxIdBook() {
return bookDao.getMaxIdBook();
}
public List<Book> getBookByIdAndAuthor(String author, Integer id) {
return bookDao.getBookByIdAndAuthor(author, id);
}
public List<Book> getBooksByIdAndName(String name, Integer id) {
return bookDao.getBooksByIdAndName(name, id);
}
}
代码解释:
bookDao.save(book) 标识将对象数据保存到数据库,save 方法由 JpaRepository 接口提供
bookDao.findAll(pageable) 是一个分页查询,使用 findAll 方法,返回值为 Page ,该对象中包含有分页常用数据,例如总记录数,总页数、每页记录数、当前页记录数等
7. 创建 BookController
@RestController
public class BookController {
@Autowired
BookService bookService;
@GetMapping("/findAll")
public void findAll() {
PageRequest pageable = PageRequest.of(2, 3);
Page<Book> page = bookService.getBookByPage(pageable);
System.out.println("总页数:"+page.getTotalPages());
System.out.println("总记录数:"+page.getTotalElements());
System.out.println("查询结果:"+page.getContent());
System.out.println("当前页数:"+(page.getNumber()+1));
System.out.println("当前页记录数:"+page.getNumberOfElements());
System.out.println("每页记录数:"+page.getSize());
}
@GetMapping("/search")
public void search() {
List<Book> bs1 = bookService.getBookByIdAndAuthor("鲁迅", 7);
List<Book> bs2 = bookService.getBooksByAuthorStartingWith("吴");
List<Book> bs3 = bookService.getBooksByIdAndName("西", 8);
List<Book> bs4 = bookService.getBooksByPriceGreaterThan(30F);
Book b = bookService.getMaxIdBook();
System.out.println("bs1:"+bs1);
System.out.println("bs2:"+bs2);
System.out.println("bs3:"+bs3);
System.out.println("bs4:"+bs4);
System.out.println("b:"+b);
}
@GetMapping("/save")
public void save() {
Book book = new Book();
book.setAuthor("鲁迅");
book.setName("呐喊");
book.setPrice(23F);
bookService.addBook(book);
}
}
代码解释:
在 findAll 接口中,首先通过调用 PageRequest 中的 of 方法构造 PageRequest 对象。of 方法接收两个参数:第一个参数是页数,从 0 开始计;第二个参数是每页显示的条数
在save 接口中构造一个 Book 对象,直接调用 save 方法保存即可
8. 测试
启动项目,查看数据库发现 t_book 表已自动新建,添加测试数据
INSERT INTO `jpa`.`t_book`(`id`, `author`, `book_name`, `price`) VALUES (1, '罗贯中', '三国演义', 30);
INSERT INTO `jpa`.`t_book`(`id`, `author`, `book_name`, `price`) VALUES (2, '曹雪芹', '红楼梦', 35);
INSERT INTO `jpa`.`t_book`(`id`, `author`, `book_name`, `price`) VALUES (3, '吴承恩', '西游记', 29);
INSERT INTO `jpa`.`t_book`(`id`, `author`, `book_name`, `price`) VALUES (4, '施耐庵', '水浒传', 29);
INSERT INTO `jpa`.`t_book`(`id`, `author`, `book_name`, `price`) VALUES (5, '钱钟书', '宋诗选注', 33);
INSERT INTO `jpa`.`t_book`(`id`, `author`, `book_name`, `price`) VALUES (6, '鲁迅', '朝花夕拾', 18);
INSERT INTO `jpa`.`t_book`(`id`, `author`, `book_name`, `price`) VALUES (7, '鲁迅', '故事新编', 22);
然后调用 /findAll 接口,控制台打印日志如下:
Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.book_name as book_nam3_0_, book0_.price as price4_0_ from t_book book0_ limit ?, ?
总页数:3
总记录数:7
查询结果:[Book{id=7, name='故事新编', author='鲁迅', price=22.0, description='null'}]
当前页数:3
当前页记录数:1
每页记录数:3
接着调用 /save 接口 ,查看数据库表数据,如下
最后调用 /search 接口,控制台打印日志如下
Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.book_name as book_nam3_0_, book0_.price as price4_0_ from t_book book0_ where book0_.id>? and book0_.author=?
Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.book_name as book_nam3_0_, book0_.price as price4_0_ from t_book book0_ where book0_.author like ? escape ?
Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.book_name as book_nam3_0_, book0_.price as price4_0_ from t_book book0_ where book0_.id<? and (book0_.book_name like ?)
Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.book_name as book_nam3_0_, book0_.price as price4_0_ from t_book book0_ where book0_.price>?
Hibernate: select * from t_book where id=(select max(id) from t_book)
bs1:[Book{id=8, name='呐喊', author='鲁迅', price=23.0, description='null'}]
bs2:[Book{id=3, name='西游记', author='吴承恩', price=29.0, description='null'}]
bs3:[Book{id=3, name='西游记', author='吴承恩', price=29.0, description='null'}]
bs4:[Book{id=2, name='红楼梦', author='曹雪芹', price=35.0, description='null'}, Book{id=5, name='宋诗选注', author='钱钟书', price=33.0, description='null'}]
b:Book{id=8, name='呐喊', author='鲁迅', price=23.0, description='null'}
来源:https://blog.csdn.net/GXL_1012/article/details/125980809


猜你喜欢
- 方式一:通过java.net.InetAddress类获取public void test1() { try { InetAdd
- 在最近的两个项目中,项目需求要求我们实现 /*登陆页面的内容能够随着键盘的弹出而被顶上去,避免键盘遮挡住登陆按钮*/ 这样的效果,宝宝心里苦
- 实现刮刮卡我们可以Get到哪些技能?* 圆形圆角图片的实现原理* 双缓冲技术绘图* Bitmap获取像素值数据* 获取绘制文本的长宽* 自定
- 对象嵌套关联查询一对多List集合查询mybatis嵌套关联查询如下由于我的是一对集合查询,所以我有两个类。@Data@TableName(
- 背景最近项目中需要上传视频文件,由于视频文件可能会比较大,但是我们应用服务器tomcat设置单次只支持的100M,因此决定开发一个分片上传接
- 目录开启定时任务注解@EnableScheduling@Scheduled添加定时任务Cron表达式在线cron工具适应场景springBo
- 员工管理系统1、准备工作资料下载内含源码 + 笔记 + web素材源码下载地址:http://xiazai.jb51.net/202105/
- //初始化propertiesProperties pro = new Properties();try { &nbs
- 混淆器通过删除从未用过的代码和使用晦涩名字重命名类、字段和方法,对代码进行压缩,优化和混淆。结果是一个比較小的.apk文件,该文件比較难进行
- 本文实例为大家分享了Android音乐播放器的具体代码,供大家参考,具体内容如下1.播放项目内的音乐package com.thm.g150
- Spring Data JPA映射自定义实体类这个问题困扰了我2天=-=,好像也能使用 jpql解决先说下自己的功能:查询oracle最近s
- 今天写这篇文章的缘由是前一段时间一个网友在我的博客上面留言,想要实现在GridLayout(相当于九宫格)中点击每项可左右滑动显
- 在C#中,可以使用一些第三方库或内置类库实现动态执行脚本的功能。以下是几个常用的方案:1.使用Roslyn编译器Roslyn是微软推出的一个
- C++11 引入一个全新的线程库,包含启动和管理线程的工具,提供了同步(互斥、锁和原子变量)的方法,我将试图为你介绍这个全新的线
- 概述从今天开始, 小白我将带大家开启 Java 数据结构 & 算法的新篇章.链表链表 (Linked List) 是一种递归的动态数
- 一、访问或添加request/session/application属性public String scope() throws Excep
- 纯Java代码模拟Hibernate一级缓存原理,简单易懂。import java.util.ArrayList;import java.u
- 这篇文章主要介绍了springboot乱码问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 之前接了需求要让视频播放时可以像优酷视频那样在悬浮窗里播放,并且悬浮窗和主播放页面之间要实现无缝切换,项目中使用的是自封装的ijkplaye
- Process#waitFor()阻塞问题有时需要在程序中调用可执行程序或脚本命令:Process process = Runtime.ge