导入依赖
- 导入依赖
- 增加配置文件
- 使用
<!-- 分页插件依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependency>
增加配置文件
@Configuration public class MyBatisConfig { @Bean public PageHelper pageHelper(){ PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); // 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 p.setProperty("offsetAsPageNum","true"); //设置为true时,使用RowBounds分页会进行count查询 p.setProperty("rowBoundsWithCount","true"); p.setProperty("reasonable","true"); pageHelper.setProperties(p); return pageHelper; } }
使用
@GetMapping("page") public Object pageVideo(@RequestParam(value = "page",defaultValue = "1")int page, @RequestParam(value = "size",defaultValue = "10")int size){ PageHelper.startPage(page,size); List<User> list = userServiceImpl.findAll(); PageInfo<User> pageInfo = new PageInfo<>(list); Map<String,Object> data = new HashMap<>(); data.put("total_size",pageInfo.getTotal());//总条数 data.put("total_page",pageInfo.getPages());//总页数 data.put("current_page",page);//当前页 data.put("data",pageInfo.getList());//数据 return data; } }
在进行使用是显示:dialect无法获得值所以在配置中加入类mysql方言
properties.setProperty("dialect","mysql");
pageHelper中的方法:
/**
* Mybatis - 通用分页拦截器
*
* @author liuzh/abel533/isea533
* @version 3.3.0
* 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
public class PageHelper implements Interceptor {
//sql工具类
private SqlUtil sqlUtil;
/**
* 开始分页
*
* @param pageNum 页码
* @param pageSize 每页显示数量
*/
public static Page startPage(int pageNum, int pageSize) {
return startPage(pageNum, pageSize, true);
}
/**
* 开始分页
*
* @param pageNum 页码
* @param pageSize 每页显示数量
* @param count 是否进行count查询
*/
public static Page startPage(int pageNum, int pageSize, boolean count) {
return startPage(pageNum, pageSize, count, null);
}
/**
* 开始分页
*
* @param pageNum 页码
* @param pageSize 每页显示数量
* @param count 是否进行count查询
* @param reasonable 分页合理化,null时用默认配置
*/
public static Page startPage(int pageNum, int pageSize, boolean count, Boolean reasonable) {
return startPage(pageNum, pageSize, count, reasonable, null);
}
/**
* 开始分页
*
* @param pageNum 页码
* @param pageSize 每页显示数量
* @param count 是否进行count查询
* @param reasonable 分页合理化,null时用默认配置
* @param pageSizeZero true且pageSize=0时返回全部结果,false时分页,null时用默认配置
*/
public static Page startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) {
Page page = new Page(pageNum, pageSize, count);
page.setReasonable(reasonable);
page.setPageSizeZero(pageSizeZero);
SqlUtil.setLocalPage(page);
return page;
}
/**
* 开始分页
*
* @param params
*/
public static Page startPage(Object params) {
Page page = SqlUtil.getPageFromObject(params);
SqlUtil.setLocalPage(page);
return page;
}
/**
* Mybatis拦截器方法
*
* @param invocation 拦截器入参
* @return 返回执行结果
* @throws Throwable 抛出异常
*/
public Object intercept(Invocation invocation) throws Throwable {
return sqlUtil.processPage(invocation);
}
/**
* 只拦截Executor
*
* @param target
* @return
*/
public Object plugin(Object target) {
if (target instanceof Executor) {
return Plugin.wrap(target, this);
} else {
return target;
}
}
/**
* 设置属性值
*
* @param p 属性值
*/
public void setProperties(Properties p) {
//数据库方言
String dialect = p.getProperty("dialect");
sqlUtil = new SqlUtil(dialect);
sqlUtil.setProperties(p);
}
}
sqlsessionFactory -> sqlSession-> executor -> mybatis sql statement
通过mybatis plugin 增加拦截器,然后拼装分页
org.apache.ibatis.plugin.Interceptor
/**
* @author Clinton Begin
*/
public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable;
default Object plugin(Object target) {
return Plugin.wrap(target, this);
}
default void setProperties(Properties properties) {
// NOP
}
mysql资料:
深度分页常用案例:
https://www.cnblogs.com/lpfuture/p/5772055.html
https://blog.csdn.net/li772030428/article/details/52839987
推荐书籍:
https://book.douban.com/subject/23008813/