mybatisplus PageHelper

https://blog.csdn.net/yangjiechao945/article/details/80707804

PageHelper用于查询语句分页,让分页更简单、代码更优雅。如果硬是要纠结效率与资源,那您倒是直接手写BaseDao哇....

集成mybatis-plus,代码中添加分页相关配置


     
     
     
     
  1. /**
  2. * mybatis-plus分页插件
  3. * 文档:http://mp.baomidou.com
  4. */
  5. @Bean
  6. public PaginationInterceptor paginationInterceptor() {
  7. PaginationInterceptor page = new PaginationInterceptor();
  8. page.setDialectType( "mysql");
  9.         // 打开PageHelper localPage 模式
  10.         page.setLocalPage( true);
  11. return page;
  12. }

这样就能使用PageHelper来分页了,当然要问了,为什么必须要手动设置打开呢?那就直接看源码咯~

直贴关键代码,具体源码请查阅:PaginationInterceptor.java


     
     
     
     
  1. package com.baomidou.mybatisplus.plugins;
  2. /**
  3. *

  4. * 分页拦截器
  5. *

  6. *
  7. * @author hubin
  8. * @Date 2016-01-23
  9. */
  10. @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
  11. public class PaginationInterceptor implements Interceptor {
  12. /* 是否开启 PageHelper localPage 模式 */
  13. private boolean localPage = false;
  14. /**
  15. * Physical Pagination Interceptor for all the queries with parameter {@link org.apache.ibatis.session.RowBounds}
  16. */
  17. public Object intercept(Invocation invocation) throws Throwable {
  18. StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
  19. MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
  20. // 先判断是不是SELECT操作
  21. MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue( "delegate.mappedStatement");
  22. if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
  23. return invocation.proceed();
  24. }
  25. RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue( "delegate.rowBounds");
  26. /* 不需要分页的场合 */
  27. if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
  28. // 本地线程分页
  29. if ( localPage) {
  30. // 采用ThreadLocal变量处理的分页
  31. rowBounds = PageHelper.getPagination();
  32. if (rowBounds == null) {
  33. return invocation.proceed();
  34. }
  35. } else {
  36. // 无需分页
  37. return invocation.proceed();
  38. }
  39. }
  40. // 针对定义了rowBounds,做为mapper接口方法的参数
  41. BoundSql boundSql = (BoundSql) metaStatementHandler.getValue( "delegate.boundSql");
  42. String originalSql = boundSql.getSql();
  43. Connection connection = (Connection) invocation.getArgs()[ 0];
  44. DBType dbType = StringUtils.isNotEmpty(dialectType) ? DBType.getDBType(dialectType) : JdbcUtils.getDbType(connection.getMetaData().getURL());
  45. if (rowBounds instanceof Pagination) {
  46. Pagination page = (Pagination) rowBounds;
  47. boolean orderBy = true;
  48. if (page.isSearchCount()) {
  49. SqlInfo sqlInfo = SqlUtils.getCountOptimize(sqlParser, originalSql);
  50. orderBy = sqlInfo.isOrderBy();
  51. this.queryTotal(overflowCurrent, sqlInfo.getSql(), mappedStatement, boundSql, page, connection);
  52. if (page.getTotal() <= 0) {
  53. return invocation.proceed();
  54. }
  55. }
  56. String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
  57. originalSql = DialectFactory.buildPaginationSql(page, buildSql, dbType, dialectClazz);
  58. } else {
  59. // support physical Pagination for RowBounds
  60. originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dbType, dialectClazz);
  61. }
  62. /*
  63. *

    禁用内存分页

  64. *

    内存分页会查询所有结果出来处理(这个很吓人的),如果结果变化频繁这个数据还会不准。

  65. */
  66. metaStatementHandler.setValue( "delegate.boundSql.sql", originalSql);
  67. metaStatementHandler.setValue( "delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
  68. metaStatementHandler.setValue( "delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
  69. return invocation.proceed();
  70. }
  71. public void setLocalPage(boolean localPage) {
  72. this.localPage = localPage;
  73. }
  74. }

     
     
     
     
关键地方已标红,在PaginationInterceptor中,是否开启 PageHelper localPage 模式,默认是关闭的,需要手动打开

      
      
      
      
  1. /* 是否开启 PageHelper localPage 模式 */
  2. private boolean localPage = false;
这货为啥不是默认打开呢?查看PageHelper类可以发现,是基于ThreadLocal实现,了然。

     
     
     
     
  1. public class PageHelper {
  2. // 分页本地线程变量
  3. private static final ThreadLocal LOCAL_PAGE = new ThreadLocal<>();
  4. /**
  5. *

  6. * 获取分页
  7. *

  8. *
  9. * @return
  10. */
  11. public static Pagination getPagination() {
  12. return LOCAL_PAGE.get();
  13. }
  14. /**
  15. *

  16. * 设置分页
  17. *

  18. *
  19. * @param page
  20. */
  21. public static void setPagination(Pagination page) {
  22. LOCAL_PAGE.set(page);
  23. }

好,接下来使用就方便了


     
     
     
     
  1. // 组装查询条件
  2. EntityWrapper wrapper = new EntityWrapper<>();
  3. if ( null != status) {
  4. wrapper.eq( "status", status);
  5. }
  6. // 设置分页参数
  7. PageHelper.startPage(pageIndex, pageSize);
  8. // 执行查询
  9. List platfProjects = UserMapper.selectList(wrapper);
  10. // 返回的分页参数
  11. Pagination pagination = PageHelper.getPagination();

就是这样~

你可能感兴趣的:(sql)