下面是关于“SpringBoot2 实现JPA分页和排序分页的案例”的完整攻略:
1. 简介
SpringBoot是一款轻量级的Java开发框架,它可以用来构建各种类型的Web应用程序。其中,JPA(Java Persistence API)是Java EE规范的一部分,用于管理Java对象和关系型数据库之间的映射关系。JPA的分页和排序功能在实际开发中非常有用。
2. 实现JPA分页
2.1 添加maven依赖
在pom.xml文件中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
pagehelper是一款用于Mybatis和Hibernate的分页插件,其与SpringBoot完美集成,同时也可以用于JPA分页。
2.2 添加Repository
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}
2.3 添加Service
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserRepository userRepository;
@Override
public PageInfo<User> findPage(int pageNum, int pageSize) {
PageHelper.startPage(pageNum,pageSize);
List<User> userList = userRepository.findAll();
return new PageInfo<>(userList);
}
}
2.4 添加Controller
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public PageInfo<User> getUsers(@RequestParam("pageNum")int pageNum,
@RequestParam("pageSize")int pageSize){
return userService.findPage(pageNum,pageSize);
}
}
2.5 效果演示
启动SpringBoot应用,并访问http://localhost:8080/users?pageNum=1&pageSize=10
,该接口将返回第1页每页10个用户的信息。
3. 实现JPA排序分页
3.1 添加Repository
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
//排序分页
Page<User> findAll(Pageable pageable);
}
3.2 添加Service
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserRepository userRepository;
@Override
public Page<User> findPageAndSort(int pageNum, int pageSize,String sortField,String sortOrder) {
Sort sort = Sort.by(Sort.Direction.fromString(sortOrder),sortField);
Pageable pageable = PageRequest.of(pageNum,pageSize,sort);
return userRepository.findAll(pageable);
}
}
3.3 添加Controller
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public Page<User> getUsers(@RequestParam("pageNum")int pageNum,
@RequestParam("pageSize")int pageSize,
@RequestParam(value = "sortField", defaultValue = "id")String sortField,
@RequestParam(value = "sortOrder", defaultValue = "ASC")String sortOrder){
return userService.findPageAndSort(pageNum,pageSize,sortField,sortOrder);
}
}
3.4 效果演示
启动SpringBoot应用,并访问http://localhost:8080/users?pageNum=1&pageSize=10&sortField=age&sortOrder=DESC
,该接口将返回第1页每页10个用户的信息,并按照age字段进行降序排序。
以上就是使用SpringBoot2实现JPA分页和排序分页的案例的完整攻略。
下面是两个示例说明:
示例1
需求:查询用户表中年龄大于等于18岁的用户,按照行id升序排序,取第1页,每页10个用户。
请求URL:http://localhost:8080/users?age=18&pageNum=1&pageSize=10&sortField=id&sortOrder=ASC
响应数据:
{
"content": [
{
"id": 1,
"name": "张三",
"age": 18,
"phone": "18888888888"
},
{
"id": 2,
"name": "李四",
"age": 19,
"phone": "17777777777"
},
...
],
"totalPages": 2,
"totalElements": 13,
"number": 1,
"size": 10,
"sort": [
{
"direction": "ASC",
"property": "id",
"ignoreCase": false,
"nullHandling": "NATIVE",
"ascending": true,
"descending": false
}
],
"first": true,
"last": false,
"empty": false
}
示例2
需求:查询年龄大于等于20岁的用户,按照年龄降序排序,取第3页,每页20个用户。
请求URL:http://localhost:8080/users?age=20&pageNum=3&pageSize=20&sortField=age&sortOrder=DESC
响应数据:
{
"content": [
{
"id": 17,
"name": "小明",
"age": 20,
"phone": "16666666666"
},
{
"id": 11,
"name": "张三丰",
"age": 20,
"phone": "17777777777"
},
...
],
"totalPages": 1,
"totalElements": 12,
"number": 2,
"size": 20,
"sort": [
{
"direction": "DESC",
"property": "age",
"ignoreCase": false,
"nullHandling": "NATIVE",
"ascending": false,
"descending": true
}
],
"first": false,
"last": true,
"empty": false
}
以上就是关于“SpringBoot2 实现JPA分页和排序分页的案例”的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot2 实现JPA分页和排序分页的案例 - Python技术站