这是表结构:
前置知识:
分页查询公式():
-- 推导一个公式
-- select * from emp
-- order by empno
-- limit 每页显示记录数 * (第几页-1),每页显示记录数
统计条数公式:
select count(*) from emp;
自定义一个请求返回格式,使请求结果统一:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private Integer code;// 响应码: 1代表成功 0 代表失败
private String mes; // 响应信息 描述字符串
private Object date; // 返回的数据
public static Result success(){
return new Result(1, "success", null);
}
public static Result success(Object date){
return new Result(1, "success", date);
}
public static Result error(String msg){
return new Result(0, msg, null);
}
}
手撕原始代码:
1.Mapper(DAO)层:
@Mapper
public interface EmpMapper {
@Select("select count(*) from emp")
public long count();
@Select("select * from emp limit #{start},#{pageSize}")
public List<Emp> page(Integer start,Integer pageSize);
}
2. Service服务接口层:
public interface EmpService {
Pagebean page(Integer page, Integer pageSize);
}
实现Service服务类:
@Service
public class EmpServiceImpl implements EmpService{
@Autowired
private EmpMapper empMapper;
@Override
public Pagebean page(Integer page, Integer pageSize) {
//1.获取总记录数
Long count = empMapper.count();
//2.获取分页查询结果列表
Integer start = (page-1)*pageSize;
List<Emp> empList = empMapper.page(start,pageSize);
//3.封装pageBean对象
Pagebean pageBean = new Pagebean(count, empList);
return pageBean;
}
}
3.Controller层:
@Slf4j
@RestController
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping("/emps")
public Result page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
log.info("分页查询,参数: {},{}", page,pageSize);
//调用service分页查询
Pagebean pagebean = empService.page(page,pageSize);
return Result.success(pagebean);
}
}
完结,在postman测试成功!
使用pagehelper插件快速开发:
1.配置pox.xml,添加相关依赖:
<!-- PageHelper分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
2. Mapper(DAO)层:
@Mapper
public interface EmpMapper {
@Select("select * from emp")
public List<Emp> list();
}
3.Service接口层:
public interface EmpService {
Pagebean page(Integer page, Integer pageSize);
}
4.实现Service服务类:
@Service
public class EmpServiceImpl implements EmpService{
@Autowired
private EmpMapper empMapper;
@Override
public Pagebean page(Integer page, Integer pageSize) {
//1.设置分页参数
PageHelper.startPage(page, pageSize);
//2.执行查询
List<Emp> empList = empMapper.list();
Page<Emp> p = (Page<Emp>) empList;
//3.封装pageBean对象
Pagebean pageBean = new Pagebean(p.getTotal(), p.getResult());
return pageBean;
}
}
4.Controller层(不用改动,与上文一致):
@Slf4j
@RestController
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping("/emps")
public Result page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
// if(page == null){
// page = 1;
// }
// if(pageSize == null) {
// pageSize = 10;
// }
log.info("分页查询,参数: {},{}", page,pageSize);
//调用service分页查询
Pagebean pagebean = empService.page(page,pageSize);
return Result.success(pagebean);
}
}
5.启动程序:
自行进行测试,完结散花!