一、为什么要分页
数据特别多的时候,单次请求返回大量的数据接口会非常慢。
对于数据量特别大的查询,我们都会采用分页查询
二、怎么设计分页
- 每页有多少个
- 当前是在第几页
- 数据的总数
- 数据列表
基于这些属性设计分页的实体类
@Datapublic class PageInfo<T> {/*** 每页有多少个*/private int pageSize;/*** 当前是在第几页*/private int currentPage;/*** 数据的总数*/private int total;/*** 数据列表*/private List<T> list;// 获取偏移量public int getOffset() {return (this.currentPage - 1) * this.pageSize;}}
三、实现分页功能
创建分页查询的方法
/*** 分页查询** @param user 查询条件* @param offset 起始位置* @param pageSize 每页容量* @return 用户列表*/List<User> page(@Param("user") User user, @Param("offset") int offset, @Param("pageSize") int pageSize);/*** 统计总数** @param user 查询条件* @return 总数*/int count(@Param("user") User user);
<select id="page" resultType="User">select *from t_user<where><if test="user.nickname != null and user.nickname != ''">and nickname like concat('%',#{user.nickname},'%')</if><if test="user.username != null and user.username != ''">and username = #{user.username}</if></where>limit #{offset},#{pageSize};</select><select id="count" resultType="int">select count(*)from t_user<where><if test="user.nickname != null and user.nickname != ''">and nickname like concat('%',#{user.nickname},'%')</if><if test="user.username != null and user.username != ''">and username = #{user.username}</if></where></select>
测试
@Test
public void page(){
PageInfo<User> pageInfo = new PageInfo<User>();
pageInfo.setCurrentPage(1);
pageInfo.setPageSize(10);
User user = new User();
user.setNickname("尚云");
// 加上筛选条件,根据nickname 或 username进行筛选
List<User> list = userMapper.page(user,pageInfo.getOffset(),pageInfo.getPageSize());
pageInfo.setList(list);
pageInfo.setTotal(userMapper.count(user));
System.out.println(pageInfo);
}
四、分页插件
1. 引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
2. 配置拦截器
在mybatis的配置文件中增加插件
<!--
plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
properties?, settings?,
typeAliases?, typeHandlers?,
objectFactory?,objectWrapperFactory?,
plugins?,
environments?, databaseIdProvider?, mappers?
-->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
3. 配置插件
4. 使用插件
@Test
public void testList() throws IOException {
SqlSession session = MybatisUtils.openSession();
User condition = new User();
// 插件里提供的分页工具,在要查询之前,执行一下PageHelper.startPage(当前页数,每页的容量), 当使用工具时候,会导致懒加载失败
// 加了这个操作,插件就会在sql语句中拼接limit限制,并且还会统计总个数
PageHelper.startPage(1,5);
List<User> users = session.getMapper(IUserMapper.class).list(condition);
// 拿到结果之后通过PageInfo.of() 的方法,获得pageInfo
com.github.pagehelper.PageInfo<User> list = com.github.pagehelper.PageInfo.of(users);
System.out.println(users);
}
五、在springboot中分页
1. 引入依赖
别的版本可能导致无法分页
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2.配置controller
@GetMapping("/list")
public String pro(ModelMap modelMap,Integer pageNum,Integer pageSize) {
PageHelper.startPage(pageNum,pageSize);
List<Technology> technologiesList = technologyService.list();
PageInfo<Technology> pageInfo=PageInfo.of(technologiesList);
modelMap.addAttribute("pageInfo",pageInfo);
return "technology/technology-list";
}
3.修改list页面
<tr th:each="technology:${pageInfo.list}" th:object="${technology}">
引入分页按钮
<!-- 分页按钮-->
<div class="layui-card-body ">
<div class="page" id="page">
</div>
</div>

修改body中layui操作
layui.use(['laydate','form','laypage'], function(){
var laydate = layui.laydate;
var form = layui.form;
var basePath = [[${#httpServletRequest.getContextPath()}]];
var laypage = layui.laypage;
//执行一个laydate实例
laydate.render({
elem: '#start' //指定元素
});
//执行一个laydate实例
laydate.render({
elem: '#end' //指定元素
});
laypage.render({
elem: 'page', //注意,这里的 test1 是 ID,不用加 # 号
//数据总数,从服务端得到
count: [[${pageInfo.total}]],
limit: [[${pageInfo.pageSize}]],
curr: [[${pageInfo.pageNum}]],
jump(obj, first) {
//首次不执行
if (!first) {
// 做一个界面跳转
window.location.href = basePath + "/product/list?pageNum=" + obj.curr + "&pageSize=" + obj.limit;
}
}
});
});
修改index链接
<li>
<a th:onclick="|xadmin.add_tab('工艺管理','@{/technology/list?pageNum=1&pageSize=5}')|">
<i class="iconfont"></i>
<cite>工艺管理</cite></a>
</li>
