Mybatis 分页插件 PageHelper
使用步骤:(不咋好用)
1.导入依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2.修改配置文件 application.properties;加入以下配置:
pagehelper.reasonable=true
pagehelper.page-size-zero=true
3.开启IDEA控制台日志:
# 打印 SQL 日志
logging.level.cn.js.mapper=trace #-------->对mapper层开启日志
logging.level.root=info #-------->日志等级
4.定义一个公共接收分页参数的公共类:
// 封装分页查询参数的
@Setter
@Getter
public class QueryObject {
private int currentPage = 1; // 封装查询第几页
private int pageSize = 5; // 封装查询每页多少条
}
5.编写Controller层:
@RestController
@RequestMapping("/test")
public class PageTestController {
@Autowired
private DepartmentService departmentService;
@PostMapping("/page")
public PageInfo<Department> pageI(@RequestBody QueryObject qo){
PageInfo<Department> pageInfo=departmentService.queryt(qo);
return pageInfo;
}
}
6.编写Service 层:
public interface DepartmentService {
//分页测试
PageInfo<Department> queryt(QueryObject qo);
}
7.编写ServiceImpl 实现层:
@Service
@Slf4j //打上日志注解开启日志
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentMapper departmentMapper;
//分页测试
@Override
public PageInfo<Department> queryt(QueryObject qo) {
// 使用分页插件传入当前页,每页显示数量。告诉PageHelper。
PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
List<Department> seleall = departmentMapper.seleall(qo); //查询数据
PageInfo<Department> paget = new PageInfo<>(seleall);
return paget;
}
}
8.编写Mapper 层:
public interface DepartmentMapper {
List<Department> seleall(QueryObject qo);
}
9.编写Mapper.XmL 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.js.mapper.DepartmentMapper">
<select id="seleall" resultType="cn.js.domain.Department">
select * from department
</select>
</mapper>
10.测试: