下面我来详细讲解一下“Spring Data JPA实现排序与分页查询超详细流程讲解”的完整攻略。本教程共包含以下五个步骤:
- 引入依赖
在pom.xml文件中加入以下依赖:
<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>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
- 创建实体类和DAO层
创建实体类Book和对应的DAO层BookRepository:
Book实体类:
@Entity
@Table(name = "tb_book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
private Double price;
@Column(name = "publication_date")
private Date publicationDate;
//getters and setters
}
BookRepository接口:
public interface BookRepository extends JpaRepository<Book, Long> {
Page<Book> findByTitleContaining(String title, Pageable pageable);
}
- 创建控制器
创建一个RestController,提供两个接口分别实现分页和排序查询:
@RestController
public class BookController {
@Autowired
private BookRepository bookRepository;
@GetMapping("/books")
public ResponseEntity<Page<Book>> findBooksByPage(@RequestParam(defaultValue = "0") Integer page,
@RequestParam(defaultValue = "5") Integer size,
@RequestParam(required = false) String title) {
Pageable pageable = PageRequest.of(page, size, Sort.Direction.ASC,"id");
Page<Book> books = bookRepository.findByTitleContaining(title, pageable);
return ResponseEntity.ok(books);
}
@GetMapping("/books/sort")
public ResponseEntity<List<Book>> findBooksBySort() {
Sort sort = Sort.by(Sort.Direction.DESC, "price");
List<Book> books = bookRepository.findAll(sort);
return ResponseEntity.ok(books);
}
}
其中,findBooksByPage
方法实现分页查询,findBooksBySort
方法实现排序查询。
- 测试接口
使用REST client等工具测试接口,例如分页查询:
http://localhost:8080/books?page=1&size=10&title=java
返回结果为:
{
"content": [
{
"id": 2,
"title": "Head First Java",
"author": "Bert Bates, Kathy Sierra",
"price": 79.0,
"publicationDate": "2018-01-01"
}
],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"pageSize": 10,
"pageNumber": 1,
"offset": 10,
"paged": true,
"unpaged": false
},
"last": false,
"totalPages": 1,
"totalElements": 1,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"first": false,
"numberOfElements": 1,
"size": 10,
"number": 1,
"empty": false
}
- 结束语
至此,我们介绍了使用Spring Data JPA实现排序与分页查询的完整攻略,希望对你有所帮助。
示例2:从前往后的正序排列
@GetMapping("/books/sort")
public ResponseEntity<List<Book>> findBooksBySort() {
Sort sort = Sort.by(Sort.Direction.ASC, "publicationDate");
List<Book> books = bookRepository.findAll(sort);
return ResponseEntity.ok(books);
}
测试接口:
http://localhost:8080/books/sort
返回结果为:
[
{
"id": 2,
"title": "Head First Java",
"author": "Bert Bates, Kathy Sierra",
"price": 79.0,
"publicationDate": "2018-01-01"
},
{
"id": 1,
"title": "Head First Design Patterns",
"author": "Eric Freeman, Elisabeth Robson",
"price": 59.0,
"publicationDate": "2019-01-01"
}
]
以上就是两个示例的具体流程,希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring Data JPA实现排序与分页查询超详细流程讲解 - Python技术站