Spring Boot中使用Spring Data JPA实现分页查询 在 Spring Boot 项目中,使用 Spring Data JPA 实现分页查询是一种非常常见的需求。下面我们将介绍如何使用 JPA 进行多条件查询以及查询列表分页。 我们需要在 pom.xml 文件中添加相关的依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 接下来,我们需要在 application.yml 文件中添加相关的配置信息: ```yaml spring: thymeleaf: cache: true check-template-location: true content-type: text/html enabled: true encoding: utf-8 mode: HTML5 prefix: classpath:/templates/ suffix: .html datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/restful?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: root initialize: true init-db: true jpa: database: mysql show-sql: true hibernate: ddl-auto: update naming: strategy: org.hibernate.cfg.ImprovedNamingStrategy ``` 然后,我们需要编写实体 Bean,例如: ```java @Entity @Table(name="book") public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", updatable = false) private Long id; @Column(nullable = false,name = "name") private String name; @Column(nullable = false,name = "isbn") private String isbn; @Column(nullable = false,name = "author") private String author; public Book(String name,String isbn,String author){ this.name = name; this.isbn = isbn; this.author = author; } public Book(){ } // 此处省去 get、set 方法 } ``` 接下来,我们需要编写查询实体 Bean,例如: ```java public class BookQuery { private String name; private String isbn; private String author; // 此处省去 get、set 方法 } ``` 我们需要编写 Repository 接口,例如: ```java @Repository("bookRepository") public interface BookRepository extends JpaRepository<Book,Long>,JpaSpecificationExecutor<Book> { } ``` 使用 JPA Specification 进行多条件查询,我们可以使用 `JpaSpecificationExecutor` 接口来实现。例如: ```java @Service public class BookService { @Autowired private BookRepository bookRepository; public List<Book> findBooks(BookQuery query) { Specification<Book> specification = new Specification<Book>() { @Override public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<>(); if (query.getName() != null) { predicates.add(cb.like(root.get("name"), "%" + query.getName() + "%")); } if (query.getIsbn() != null) { predicates.add(cb.equal(root.get("isbn"), query.getIsbn())); } if (query.getAuthor() != null) { predicates.add(cb.like(root.get("author"), "%" + query.getAuthor() + "%")); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); } }; return bookRepository.findAll(specification); } } ``` 在上面的代码中,我们使用 `JpaSpecificationExecutor` 接口来实现多条件查询。我们可以使用 `Specification` 接口来定义查询条件,然后使用 `JpaSpecificationExecutor` 接口来执行查询。 在分页查询中,我们可以使用 `Pageable` 接口来实现分页。例如: ```java @Service public class BookService { @Autowired private BookRepository bookRepository; public Page<Book> findBooks(BookQuery query, Pageable pageable) { Specification<Book> specification = new Specification<Book>() { @Override public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<>(); if (query.getName() != null) { predicates.add(cb.like(root.get("name"), "%" + query.getName() + "%")); } if (query.getIsbn() != null) { predicates.add(cb.equal(root.get("isbn"), query.getIsbn())); } if (query.getAuthor() != null) { predicates.add(cb.like(root.get("author"), "%" + query.getAuthor() + "%")); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); } }; return bookRepository.findAll(specification, pageable); } } ``` 在上面的代码中,我们使用 `Pageable` 接口来实现分页查询。我们可以使用 `Pageable` 接口来定义分页条件,然后使用 `JpaSpecificationExecutor` 接口来执行分页查询。 使用 Spring Data JPA 实现分页查询是一种非常常见的需求。在本文中,我们介绍了如何使用 JPA 进行多条件查询以及查询列表分页。我们可以使用 `JpaSpecificationExecutor` 接口来实现多条件查询,然后使用 `Pageable` 接口来实现分页查询。





















- 粉丝: 3
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- CAD实习工作总结4篇.docx
- 2023年软件测试面试题.doc
- 单片机交通灯课程设计课程设计报告.doc
- 基因工程内皮祖细胞介导的靶向血管新生治疗胶质瘤的研究的开题报告.docx
- 图形显示软件.pptx
- 2023年软件程序员年度工作总结范文五篇.doc
- 北京理工大学2021年9月《ASP作业考核试题及答案参考.NET开发技术》9.docx
- Java--多元化--(补考)(姓名).docx
- 旅游网站对潜在人流的导引率研究——以携程结伴同游为例的开题报告.docx
- 操作系统复习答案题.doc
- 3DMA和AUTOCAD实训报告.docx
- 2023年厦门电大计算机考试内容.doc
- (完整)各种网络安全设备巡检报告汇总-推荐文档.pdf
- 高桩框架式码头CAD软件说课材料.ppt
- GB_T_30466_2013_粮食干燥系统安全操作规范.pdf
- CAD基础教程新手入门教程.pptx


