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` 接口来实现分页查询。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/release/download_crawler_static/12776415/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 3
- 资源: 916
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)