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
加配置
pagehelper.reasonable=true
合理化分页,如果是pageNum<=0 时会查询第一页,如果大于等于总页数显示最后一页
pagehelper.page-size-zero=true
如果把pageSize设置成0,会查询所有数据
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.测试:
日志:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/188569.html