1.引入依赖?
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.7</version>
</dependency>
2.application.yml 配置
# PageHelper分页插件
pagehelper:
helperDialect: mysql
supportMethodsArguments: true
params: count=countSql
3.PageUtil
/**
* 分页工具类
* @author: jzm
*/
public class PageUtil
{
private static <T> Page<T> _startPage(int pageNum, int pageSize, ISelect select, String orderBy)
{
Page<T> page = PageHelper.startPage(pageNum, pageSize).doSelectPage(select);
if (orderBy == null)
{
return page;
}
page.setOrderBy(orderBy);
return page;
}
/**
* 分页
*
* @param select : () -> userMapper.list()
*/
public static <T> PageInfo<T> startPage(int pageNum, int pageSize, ISelect select)
{
return new PageInfo<>(_startPage(pageNum, pageSize, select, null));
}
/**
* 降序 + 分页
*/
public static <T> PageInfo<T> orderByDesc(int pageNum, int pageSize, String filed, ISelect select)
{
return new PageInfo<>(_startPage(pageNum, pageSize, select, filed + " desc"));
}
/**
* 升序 + 分页
*/
public static <T> PageInfo<T> orderByAsc(int pageNum, int pageSize, String filed, ISelect select)
{
return new PageInfo<>(_startPage(pageNum, pageSize, select, filed + " asc"));
}
/**
* 清理分页的线程变量
*/
public static void clearPage()
{
PageHelper.clearPage();
}
}
?4. 测试
@Test
public void mapperTest(){
PageInfo<Object> pageInfo = PageUtil.startPage(1, 10, () ->
{
service.findUserList(); // 要执行的service、mappere查询操作都可以
});
System.out.println(12);
}
调试看一波。嗯...?