{"id":18302,"date":"2020-02-05T15:49:20","date_gmt":"2020-02-05T07:49:20","guid":{"rendered":"http:\/\/4563.org\/?p=18302"},"modified":"2020-02-05T15:49:20","modified_gmt":"2020-02-05T07:49:20","slug":"%e6%94%af%e6%8c%81%e8%bf%9e%e8%a1%a8%e7%ad%89%e5%a4%8d%e6%9d%82%e6%9f%a5%e8%af%a2%e7%9a%84pagehelper-pagehelper5-1-4-mybatis3-4-6-vps%e8%b5%84%e8%ae%af","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=18302","title":{"rendered":"\u652f\u6301\u8fde\u8868\u7b49\u590d\u6742\u67e5\u8be2\u7684PageHelper\u2014\u2014PageHelper5.1.4 Mybatis3.4.6 &#8211; VPS\u8d44\u8baf"},"content":{"rendered":"<br \/>\n<h3>\u4e00\u3001PageHelper\u4e2d\u5206\u9875\u5b58\u5728\u7684\u95ee\u9898\uff1a<\/h3>\n<ul>\n<li>\u4e3b\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<br \/>provider_order\u8868\uff0c\u8ba2\u5355\u4e3b\u8868\u3002\u6211\u4eec\u8fd9\u91cc\u7b80\u5316\u5904\u7406\uff0c\u5b83\u53ea\u6709\u4e00\u4e2a\u5b57\u6bb5\uff0c\u5373id\u3002<\/li>\n<\/ul>\n<p>VARCHAR\u3000\u3000|\u3000\u3000po_id\u3000\u3000 | \u3000\u3000 \u8ba2\u5355\u8868id<\/p>\n<ul>\n<li>\u4ece\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<br \/>order_info\u8868\uff0c\u8ba2\u5355\u4ece\u8868\u3002\u5b83\u8d1f\u8d23\u8bb0\u5f55\u8ba2\u5355\u7684\u4e00\u4e9b\u4fe1\u606f\uff0c\u4e0e\u4e3b\u8868\u662f\u4e00\u5bf9\u591a\u7684\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<p>VARCHAR\u3000\u3000|\u3000\u3000oi_id\u3000\u3000|\u3000\u3000\u8ba2\u5355\u4fe1\u606fid VARCHAR\u3000\u3000|\u3000\u3000po_id\u3000\u3000 | \u3000\u3000 \u8ba2\u5355\u8868id TEXT\u3000\u3000\u3000\u3000|\u3000\u3000info\u3000\u3000\u3000|\u3000\u3000\u3000\u4fe1\u606f\u8be6\u60c5<\/p>\n<ul>\n<li>\u73b0\u5728\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u7684\u8fde\u8868\u67e5\u8be2\u64cd\u4f5c\uff0csql\u8bed\u53e5\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre><code>&lt;select id=&quot;getProviderOrderWithOrderInfoTest&quot; resultMap=&quot;ResultMapWithOrderInfo&quot; parameterType=&quot;java.lang.String&quot;&gt;         SELECT *         FROM (SELECT * FROM provider_order WHERE po_id LIKE #{po_id}) AS limitable             LEFT JOIN order_info ON limitable.po_id = order_info.provider_order_id     &lt;\/select&gt;<\/code><\/pre>\n<ul>\n<li>\u8fdb\u884cPageHelper\u7684\u5206\u9875\u64cd\u4f5c\uff0c\u67e5\u8be2\u524d\u5341\u6761\u8bb0\u5f55\uff1a<\/li>\n<\/ul>\n<pre><code> Page page = PageHelper.startPage(0, 10);         List&lt;ProviderOrder&gt; providerOrderList = testService.getProviderOrderWithOrderInfoTest(&quot;%-%&quot;);         PageInfo&lt;ProviderOrder&gt; pageInfo = new PageInfo&lt;&gt;(providerOrderList);          System.out.println(pageInfo); <\/code><\/pre>\n<ul>\n<li>\u76ee\u524d\u6211\u4eec\u7684\u4e3b\u8868\u6709\u4e24\u6761\u8bb0\u5f55\uff0c\u4ece\u8868\u6709\u4e8c\u5341\u6761\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p>\u6211\u4eec\u4f1a\u53d1\u73b0\uff0c\u67e5\u8be2\u51fa\u6765\u7684\u603b\u6570\u662f20\uff0c\u7136\u540e\u67e5\u8be2\u7ed3\u679c\u662f\u4e24\u4e2alist\uff08\u54045\u6761\u4ece\u8868\u4fe1\u606f\uff09\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u5206\u9875\u6570\u636e\u4e5f\u5f88\u5947\u602a\uff0c\u6211\u4eec\u53ea\u6709\u4e24\u6761\u4e3b\u8868\u8bb0\u5f55\uff0c\u4f46\u5374\u88ab\u5206\u6210\u4e86\u4e24\u9875\uff08pages = 2\uff09\uff0c\u5e76\u4e14hasNextPage = true\u3002\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u8981\u7684\u7ed3\u679c\u3002\u6211\u4eec\u60f3\u8981\u67e5\u8be2\u7684\u6548\u679c\u662f\uff1a<\/p>\n<p><strong>\u524d\u5341\u6761 \u8ba2\u5355 \u8bb0\u5f55\uff0c\u7136\u540e\u8fde\u8868\u67e5\u8be2\u51fa\u8be5\u5341\u6761\u8ba2\u5355\u8bb0\u5f55\u4e0b\u7684\u6240\u6709 \u8ba2\u5355\u4fe1\u606f<\/strong><br \/>\u4e5f\u5c31\u662f\u8bf4\uff0c\u7ed3\u679c\u96c6\u4e2d\uff0c\u9996\u5148\u6211\u4eec\u5e94\u8be5\u5f97\u5230\u4e24\u4e2alist\uff0clist\u4e2d\u540410\u6761\u4ece\u8868\u4fe1\u606f\uff0c\u800c\u4e14\u4e5f\u4e0d\u8be5\u6709\u4e0b\u4e00\u9875\uff0c\u603b\u9875\u6570\u5e94\u4e3a1\uff0c\u603b\u6570\u636e\u6570\u5e94\u4e3a2\u3002<\/p>\n<p><strong>\u67e5\u770bPageHelper\u7684\u6e90\u7801\u6211\u4eec\u6765\u770b\u770b\uff0c\u5b83\u7684count\uff0c\u4ee5\u53calimit\uff0c\u5230\u5e95\u662f\u600e\u4e48\u56de\u4e8b\u3002<\/strong><\/p>\n<h3>PageHelper\u7684count\u5b9e\u73b0<\/h3>\n<pre><code> private Long executeAutoCount(Executor executor, MappedStatement countMs, Object parameter, BoundSql boundSql, RowBounds rowBounds, ResultHandler resultHandler) throws IllegalAccessException,         SQLException {         Map&lt;String, Object&gt; additionalParameters = (Map) this.additionalParametersField.get(boundSql);         CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);         String countSql = this.dialect.getCountSql(countMs, boundSql, parameter, rowBounds, countKey);         BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);         Iterator var11 = additionalParameters.keySet()                                              .iterator();          while (var11.hasNext()) {             String key = (String) var11.next();             countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));         }          Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);         Long count = (Long) ((List) countResultList).get(0);          \/\/ \u83b7\u53d6count         return count;     }<\/code><\/pre>\n<p>\u6211\u4eec\u5f88\u5bb9\u6613\u770b\u51fa\u6765\uff0ccount\u7684sql\u8bed\u53e5\uff0c\u5728\u8fd9\u91cc\u7ecf\u8fc7\u4e86\u5904\u7406\uff0c\u8fd9\u91cc\u8c03\u7528\u4e86dialect\u7684getCountSql\u65b9\u6cd5\u3002 \u5b83\u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre><code> public String getSmartCountSql(String sql, String name) {         Statement stmt = null;         if (sql.indexOf(&quot;\/*keep orderby*\/&quot;) &gt;= 0) {             return this.getSimpleCountSql(sql);         } else {             try {                 stmt = CCJSqlParserUtil.parse(sql);             } catch (Throwable var8) {                 return this.getSimpleCountSql(sql);             }              Select select = (Select)stmt;             SelectBody selectBody = select.getSelectBody();              try {                 this.processSelectBody(selectBody);             } catch (Exception var7) {                 return this.getSimpleCountSql(sql);             }              this.processWithItemsList(select.getWithItemsList());             this.sqlToCount(select, name);             String result = select.toString();             return result;         }     }      public String getSimpleCountSql(String sql) {         return this.getSimpleCountSql(sql, &quot;0&quot;);     }      public String getSimpleCountSql(String sql, String name) {         StringBuilder stringBuilder = new StringBuilder(sql.length() + 40);         stringBuilder.append(&quot;select count(&quot;);         stringBuilder.append(name);         stringBuilder.append(&quot;) from (&quot;);         stringBuilder.append(sql);         stringBuilder.append(&quot;) tmp_count&quot;);         return stringBuilder.toString();     }<\/code><\/pre>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u5728sql\u5916\u9762\u5957\u4e00\u5c42select count\u8bed\u53e5\uff0c\u6211\u4eec\u7684sql\u8bed\u53e5\u53d8\u6210\u4e86 SELECT count(0) FROM (\u6211\u4eec\u7684sql\u8bed\u53e5)\uff08\u771f\u5b9e\u60c5\u51b5\u6ca1\u6709\u8fd9\u4e48\u7b80\u5355\uff0c\u8fd9\u53ea\u662f\u5176\u4e2d\u4e00\u79cd\u60c5\u51b5\u3002\uff09\u8fd9\u6837\u5c31\u5f88\u5bb9\u6613\u5f97\u77e5\uff0c\u4e3a\u4ec0\u4e48\u6700\u540e\u7684total\u662f20\uff08\u6240\u6709\u6570\u636e\u6570\uff09\uff0c\u800c\u4e0d\u662f2\uff08\u4e3b\u8868\u6570\u636e\u6570\uff09\u4e86\u3002<\/p>\n<h3>\u4e09\u3001 PageHelper\u7684\u5206\u9875\u5b9e\u73b0<\/h3>\n<p>\u4e0e\u4e0a\u9762\u76f8\u540c\uff0c\u8fd9\u91cc\u4ee5dialect\uff08\u4e3a\u4e86\u9002\u914d\u5404\u79cd\u6570\u636e\u5e93\uff09\u4e3amysql\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u5728sql\u8bed\u53e5\u91cc\u9762\u5199limit\uff0c\u5c31\u4f1a\u5728sql\u8bed\u53e5\u7684\u6700\u540e\u9762\uff0c\u6dfb\u52a0limit\u8bed\u53e5\u3002<\/p>\n<pre><code>public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {         StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);         sqlBuilder.append(sql);         if (rowBounds.getOffset() == 0) {             sqlBuilder.append(&quot; LIMIT &quot;);             sqlBuilder.append(rowBounds.getLimit());         } else {             sqlBuilder.append(&quot; LIMIT &quot;);             sqlBuilder.append(rowBounds.getOffset());             sqlBuilder.append(&quot;,&quot;);             sqlBuilder.append(rowBounds.getLimit());             pageKey.update(rowBounds.getOffset());         }          pageKey.update(rowBounds.getLimit());         return sqlBuilder.toString();     }<\/code><\/pre>\n<p>\u7a0b\u5e8f\u8fd0\u884c\u5f97\u5230sql\u8bed\u53e5\u5982\u4e0b\uff0cPageHelper\u5bf9\u67e5\u8be2\u7684\u603b\u7ed3\u679c\u96c6\uff08\u5305\u62ec\u4e3b\u8868\u6570\u636e\u4e0e\u4ece\u8868\u6570\u636e\uff09\uff0c\u8fdb\u884c\u4e86\u5206\u9875 <\/p>\n<p>\u5728\u5f97\u77e5PageHelper\u5728\u8fd9\u4e24\u4e2a\u6b65\u9aa4\u7684\u539f\u7406\u540e\uff0c\u6211\u4eec\u5f00\u59cb\u5bf9PageHelper\u8fdb\u884c\u6539\u9020\u3002<\/p>\n<h3>\u56db\u3001 count\u7ed3\u679c\u4fee\u6b63<\/h3>\n<p>\u5728\u4e0a\u9762\u7684count\u6e90\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u4e48\u4e00\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre><code>  public String getSmartCountSql(String sql, String name) {         Statement stmt = null;         if (sql.indexOf(&quot;\/*keep orderby*\/&quot;) &gt;= 0) {             return this.getSimpleCountSql(sql);         } else {             try {                 stmt = CCJSqlParserUtil.parse(sql);             } catch (Throwable var8) {                 return this.getSimpleCountSql(sql);             }              Select select = (Select)stmt;             SelectBody selectBody = select.getSelectBody();              try {                 this.processSelectBody(selectBody);             } catch (Exception var7) {                 return this.getSimpleCountSql(sql);             }              this.processWithItemsList(select.getWithItemsList());             this.sqlToCount(select, name);             String result = select.toString();             return result;         }     }       public void sqlToCount(Select select, String name) {         SelectBody selectBody = select.getSelectBody();         List&lt;SelectItem&gt; COUNT_ITEM = new ArrayList();         COUNT_ITEM.add(new SelectExpressionItem(new Column(&quot;count(&quot; + name + &quot;)&quot;)));         if (selectBody instanceof PlainSelect &amp;&amp; this.isSimpleCount((PlainSelect)selectBody)) {             ((PlainSelect)selectBody).setSelectItems(COUNT_ITEM);         } else {             PlainSelect plainSelect = new PlainSelect();             SubSelect subSelect = new SubSelect();             subSelect.setSelectBody(selectBody);             subSelect.setAlias(TABLE_ALIAS);             plainSelect.setFromItem(subSelect);             plainSelect.setSelectItems(COUNT_ITEM);             select.setSelectBody(plainSelect);         }      }<\/code><\/pre>\n<p>\u5b9e\u9645\u4e0aPageHelper\u662f\u6709\u63d0\u4f9b\u7ed9\u6211\u4eec\u4e00\u5957\u6307\u5b9acolum\u67e5\u8be2\u7684\u5165\u53e3\u7684\u3002\u6211\u4eec\u5c06\u67e5\u8be2\u4ee3\u7801\u505a\u5982\u4e0b\u6539\u9020\uff1a<\/p>\n<pre><code>   Page page = PageHelper.startPage(0, 10);         page.setCountColumn(&quot;DISTINCT po_id&quot;);         List&lt;ProviderOrder&gt; providerOrderList = testService.getProviderOrderWithOrderInfoTest(&quot;%-%&quot;);          PageInfo&lt;ProviderOrder&gt; pageInfo = new PageInfo&lt;&gt;(providerOrderList);          System.out.println(pageInfo); <\/code><\/pre>\n<p>\u6211\u4eec\u4f1a\u53d1\u73b0\uff0c\u6211\u4eec\u9700\u8981\u7684\u5206\u9875\u6570\u636e\u6b63\u5e38\u4e86\uff0c\u603b\u9875\u7801\uff08pages\uff1a1\uff09\uff0ctotal\uff1a2\u3002<\/p>\n<p>\u4e94\u3001 \u67e5\u8be2\u7ed3\u679c\u96c6\u4fee\u6b63<\/p>\n<p>\u6211\u4eec\u5728\u4e09\u4e2d\uff0c\u53d1\u73b0\u4e86PageHelper limit\u6dfb\u52a0\u7684\u4f4d\u7f6e\u4e0d\u6b63\u786e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5176\u653e\u5728\u6b63\u786e\u7684\u4f4d\u7f6e\u4e0b\u3002\u6240\u4ee5\u65b0\u5efa\u4e00\u4e2a\u7c7b\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/**  * Created by Anur IjuoKaruKas on 2018\/6\/8  *\/ public class PageSqlResolver {      public static final String SQL_SIGN = &quot;AS limitable&quot;;      public static final String LIMIT_SIGN = &quot;LIMIT ?&quot;;      public static final String LIMIT_SIGN_EX = &quot;LIMIT ?, ?&quot;;      public static String resolveLimit(String pageSql) {         if (pageSql == null) {             return null;         }          if (pageSql.contains(SQL_SIGN)) {\/\/ \u5982\u679c\u9700\u8981\u7279\u6b8a\u5206\u9875             String changer = &quot;&quot;;              if (pageSql.contains(LIMIT_SIGN_EX)) {                 changer = LIMIT_SIGN_EX;             } else if (pageSql.contains(LIMIT_SIGN)) {                 changer = LIMIT_SIGN;             }              pageSql = pageSql.replace(changer, &quot;&quot;);             StringBuilder sqlBuilder = new StringBuilder(pageSql);              StringBuilder mae = new StringBuilder(sqlBuilder.substring(0, sqlBuilder.indexOf(SQL_SIGN)));\/\/ mae \u622a\u6b62sql\u8bed\u53e5\u5230 limitable             StringBuilder uShiRo = new StringBuilder(sqlBuilder.substring(sqlBuilder.indexOf(SQL_SIGN), sqlBuilder.length()));\/\/ \u5269\u4f59\u7684              mae.insert(mae.lastIndexOf(&quot;)&quot;), String.format(&quot; %s&quot;, changer));              return mae.append(uShiRo)                       .toString();         } else {             return pageSql;         }     } }<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u542b\u4e49\u662f\uff1a\u5c06AS limitable\uff0c\u4e5f\u5c31\u662f\u4e3b\u8868\uff0c\u6807\u8bb0\u4e3a\u771f\u6b63\u5206\u9875\u7684\u5730\u65b9\uff0c\u5e76\u5411\u5176\u6dfb\u5982limit ? ,?<\/p>\n<pre><code>SELECT *         FROM (SELECT * FROM provider_order WHERE po_id LIKE ?) AS limitable             LEFT JOIN order_info ON limitable.po_id = order_info.provider_order_id LIMIT ? <\/code><\/pre>\n<p>sql\u8bed\u53e5\u4ece\u4e0a\u9762\u53d8\u6210\u4e86\uff1a<\/p>\n<pre><code>SELECT *         FROM (SELECT * FROM provider_order WHERE po_id LIKE ? LIMIT ?) AS limitable             LEFT JOIN order_info ON limitable.po_id = order_info.provider_order_id   <\/code><\/pre>\n<p>\u6211\u4eec\u53d1\u73b0\u8fd9\u5c31\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c\uff0c\u5b83\u7684\u5206\u9875\u662f\u9488\u5bf9\u4e3b\u8868\u8fdb\u884c\u7684\uff0c\u800c\u4e0d\u662f\u9488\u5bf9\u7ed3\u679c\u96c6\u3002<\/p>\n<p>\u89e3\u6790\u5230\u8fd9\u91cc\u5c31\u7ed3\u675f\u4e86<br \/><a href=\"https:\/\/github.com\/anurnomeru\/page-helper-ext\">https:\/\/github.com\/anurnomeru\/page-helper-ext<\/a> demo\u793a\u4f8b\u8bf7\u70b9\u51fb\u8fd9\u91cc<\/p>\n<h3>\u516d\u3001\u6539\u9020\u6b65\u9aa4\u8be6\u89e3<\/h3>\n<p>1\u3001\u5f15\u5165\u4f9d\u8d56 \u8fd9\u91cc\u4f7f\u7528\u4e86mybatis 3.4.6\uff08tk.mybatis 4.0.2\uff09\uff0cpagehelper5.1.4\uff08pagehelper-spring-boot-starter1.2.5\uff09\uff0c\u4f60\u53ef\u4ee5\u5f15\u5165\u539f\u4f9d\u8d56\uff0c\u4e5f\u53ef\u4ee5\u7528\u8fd9\u91cc\u4f7f\u7528\u7684tkMybatis\uff0c\u4ee5\u53capagehelper-spring-boot-starter\u3002<\/p>\n<pre><code> &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;          xsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&quot;&gt;     &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;      &lt;groupId&gt;com.anur&lt;\/groupId&gt;     &lt;artifactId&gt;page-helper&lt;\/artifactId&gt;     &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;     &lt;packaging&gt;jar&lt;\/packaging&gt;      &lt;name&gt;page-helper&lt;\/name&gt;     &lt;description&gt;Demo project for Spring Boot&lt;\/description&gt;      &lt;parent&gt;         &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;         &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;         &lt;version&gt;2.0.2.RELEASE&lt;\/version&gt;         &lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;     &lt;\/parent&gt;      &lt;properties&gt;         &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;         &lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt;         &lt;java.version&gt;1.8&lt;\/java.version&gt;     &lt;\/properties&gt;      &lt;dependencies&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;         &lt;\/dependency&gt;          &lt;!-- page - helper --&gt;         &lt;dependency&gt;             &lt;groupId&gt;com.github.pagehelper&lt;\/groupId&gt;             &lt;artifactId&gt;pagehelper-spring-boot-starter&lt;\/artifactId&gt;             &lt;version&gt;1.2.5&lt;\/version&gt;         &lt;\/dependency&gt;          &lt;dependency&gt;             &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;             &lt;artifactId&gt;lombok&lt;\/artifactId&gt;             &lt;optional&gt;true&lt;\/optional&gt;         &lt;\/dependency&gt;          &lt;dependency&gt;             &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;             &lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;             &lt;scope&gt;test&lt;\/scope&gt;         &lt;\/dependency&gt;          &lt;!-- codeGen --&gt;         &lt;dependency&gt;             &lt;groupId&gt;org.mybatis.generator&lt;\/groupId&gt;             &lt;artifactId&gt;mybatis-generator-core&lt;\/artifactId&gt;             &lt;version&gt;1.3.6&lt;\/version&gt;         &lt;\/dependency&gt;          &lt;!-- tk - mybatis --&gt;         &lt;dependency&gt;             &lt;groupId&gt;tk.mybatis&lt;\/groupId&gt;             &lt;artifactId&gt;mapper&lt;\/artifactId&gt;             &lt;version&gt;4.0.2&lt;\/version&gt;         &lt;\/dependency&gt;          &lt;!-- mysql --&gt;         &lt;dependency&gt;             &lt;groupId&gt;mysql&lt;\/groupId&gt;             &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;             &lt;scope&gt;runtime&lt;\/scope&gt;         &lt;\/dependency&gt;          &lt;!-- https:\/\/mvnrepository.com\/artifact\/com.alibaba\/fastjson --&gt;         &lt;dependency&gt;             &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;             &lt;artifactId&gt;fastjson&lt;\/artifactId&gt;             &lt;version&gt;1.2.47&lt;\/version&gt;         &lt;\/dependency&gt;      &lt;\/dependencies&gt;      &lt;build&gt;         &lt;plugins&gt;             &lt;plugin&gt;                 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;                 &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;             &lt;\/plugin&gt;             &lt;plugin&gt;                 &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;                 &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;                 &lt;configuration&gt;                     &lt;proc&gt;none&lt;\/proc&gt;                 &lt;\/configuration&gt;             &lt;\/plugin&gt;         &lt;\/plugins&gt;     &lt;\/build&gt;   &lt;\/project&gt;  <\/code><\/pre>\n<p>2\u3001\u65b0\u589e\u4e24\u4e2a\u7c7b\uff0c\u7528\u4e8e\u4fee\u6b63\u67e5\u8be2\u65f6sql\u8bed\u53e5<\/p>\n<pre><code>package com.anur.pagehelper.page;  \/\/ \/\/ Source code recreated from a .class file by IntelliJ IDEA \/\/ (powered by Fernflower decompiler) \/\/  import com.github.pagehelper.Dialect; import com.github.pagehelper.PageException; import com.github.pagehelper.cache.Cache; import com.github.pagehelper.cache.CacheFactory; import com.github.pagehelper.util.MSUtils; import com.github.pagehelper.util.StringUtil; import java.lang.reflect.Field; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds;  \/**  * Created by Anur IjuoKaruKas on 2018\/6\/8  *\/  @Intercepts({     @Signature(         type = Executor.class,         method = &quot;query&quot;,         args = {             MappedStatement.class,             Object.class,             RowBounds.class,             ResultHandler.class         }     ),     @Signature(         type = Executor.class,         method = &quot;query&quot;,         args = {             MappedStatement.class,             Object.class,             RowBounds.class,             ResultHandler.class,             CacheKey.class,             BoundSql.class         }     ) }) public class CustomPageInterceptor implements Interceptor {      protected Cache&lt;String, MappedStatement&gt; msCountMap = null;      private Dialect dialect;      private String default_dialect_class = &quot;com.github.pagehelper.PageHelper&quot;;      private Field additionalParametersField;      private String countSuffix = &quot;_COUNT&quot;;      public CustomPageInterceptor() {     }      \/**      * \u62e6\u622a      *\/     public Object intercept(Invocation invocation) throws Throwable {         try {             Object[] args = invocation.getArgs();              \/\/ \u5bf9\u5e94\u4e00\u4e2aMapper\u8282\u70b9\uff0c\u63cf\u8ff0\u4e00\u6761sql\u8bed\u53e5             MappedStatement ms = (MappedStatement) args[0];              \/\/ \u53c2\u6570             Object parameter = args[1];              \/\/ mybatis\u81ea\u5e26\u5206\u9875             RowBounds rowBounds = (RowBounds) args[2];              \/\/ \u7ed3\u679c\u96c6\u5904\u7406\u5668             ResultHandler resultHandler = (ResultHandler) args[3];              \/\/ \u6267\u884c\u5668             Executor executor = (Executor) invocation.getTarget();             CacheKey cacheKey;             BoundSql boundSql;              if (args.length == 4) {                 boundSql = ms.getBoundSql(parameter);                 cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);             } else {                 cacheKey = (CacheKey) args[4];                 boundSql = (BoundSql) args[5];             }              List resultList;             if (this.dialect.skip(ms, parameter, rowBounds)) {                 resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);             } else {                 String msId = ms.getId();                 Configuration configuration = ms.getConfiguration();                 Map&lt;String, Object&gt; additionalParameters = (Map) this.additionalParametersField.get(boundSql);                 if (this.dialect.beforeCount(ms, parameter, rowBounds)) {                     String countMsId = msId + this.countSuffix;                     MappedStatement countMs = this.getExistedMappedStatement(configuration, countMsId);                     Long count;                     if (countMs != null) {                         count = this.executeManualCount(executor, countMs, parameter, boundSql, resultHandler);                     } else {                         countMs = (MappedStatement) this.msCountMap.get(countMsId);                         if (countMs == null) {                             countMs = MSUtils.newCountMappedStatement(ms, countMsId);                             this.msCountMap.put(countMsId, countMs);                         }                          \/\/ \u8fd9\u91cc\u662fcount\uff0c\u53ef\u80fd\u8981\u6539 \/\/                        boundSql = PageSqlResolver.resolveCount(configuration, boundSql, parameter);                         count = this.executeAutoCount(executor, countMs, parameter, boundSql, rowBounds, resultHandler);                     }                      if (!this.dialect.afterCount(count, parameter, rowBounds)) {                         Object var24 = this.dialect.afterPage(new ArrayList(), parameter, rowBounds);                         return var24;                     }                 }                  if (!this.dialect.beforePage(ms, parameter, rowBounds)) {                     resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);                 } else {                     parameter = this.dialect.processParameterObject(ms, parameter, boundSql, cacheKey);                     String pageSql = this.dialect.getPageSql(ms, boundSql, parameter, rowBounds, cacheKey);                      \/\/ \u5bf9sql\u8fdb\u884c\u6539\u9020                     pageSql = PageSqlResolver.resolveLimit(pageSql);                      BoundSql pageBoundSql = new BoundSql(configuration, pageSql, boundSql.getParameterMappings(), parameter);                     Iterator var17 = additionalParameters.keySet()                                                          .iterator();                      while (true) {                         if (!var17.hasNext()) {                             resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, pageBoundSql);                             break;                         }                          String key = (String) var17.next();                         pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));                     }                 }             }              Object var22 = this.dialect.afterPage(resultList, parameter, rowBounds);             return var22;         } finally {             this.dialect.afterAll();         }     }      private Long executeManualCount(Executor executor, MappedStatement countMs, Object parameter, BoundSql boundSql, ResultHandler resultHandler) throws IllegalAccessException, SQLException {         CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);         BoundSql countBoundSql = countMs.getBoundSql(parameter);         Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);         Long count = ((Number) ((List) countResultList).get(0)).longValue();         return count;     }      private Long executeAutoCount(Executor executor, MappedStatement countMs, Object parameter, BoundSql boundSql, RowBounds rowBounds, ResultHandler resultHandler) throws IllegalAccessException,         SQLException {         Map&lt;String, Object&gt; additionalParameters = (Map) this.additionalParametersField.get(boundSql);         CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);         String countSql = this.dialect.getCountSql(countMs, boundSql, parameter, rowBounds, countKey);         BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);         Iterator var11 = additionalParameters.keySet()                                              .iterator();          while (var11.hasNext()) {             String key = (String) var11.next();             countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));         }          Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);         Long count = (Long) ((List) countResultList).get(0);          \/\/ \u83b7\u53d6count         return count;     }      private MappedStatement getExistedMappedStatement(Configuration configuration, String msId) {         MappedStatement mappedStatement = null;          try {             mappedStatement = configuration.getMappedStatement(msId, false);         } catch (Throwable var5) {             ;         }          return mappedStatement;     }      public Object plugin(Object target) {         return Plugin.wrap(target, this);     }      public void setProperties(Properties properties) {         this.msCountMap = CacheFactory.createCache(properties.getProperty(&quot;msCountCache&quot;), &quot;ms&quot;, properties);         String dialectClass = properties.getProperty(&quot;dialect&quot;);         if (StringUtil.isEmpty(dialectClass)) {             dialectClass = this.default_dialect_class;         }          try {             Class&lt;?&gt; aClass = Class.forName(dialectClass);             this.dialect = (Dialect) aClass.newInstance();         } catch (Exception var6) {             throw new PageException(var6);         }          this.dialect.setProperties(properties);         String countSuffix = properties.getProperty(&quot;countSuffix&quot;);         if (StringUtil.isNotEmpty(countSuffix)) {             this.countSuffix = countSuffix;         }          try {             this.additionalParametersField = BoundSql.class.getDeclaredField(&quot;additionalParameters&quot;);             this.additionalParametersField.setAccessible(true);         } catch (NoSuchFieldException var5) {             throw new PageException(var5);         }     } }  <\/code><\/pre>\n<pre><code>package com.anur.pagehelper.page;  \/**  * Created by Anur IjuoKaruKas on 2018\/6\/8  *\/ public class PageSqlResolver {      public static final String SQL_SIGN = &quot;AS limitable&quot;;      public static final String LIMIT_SIGN = &quot;LIMIT ?&quot;;      public static final String LIMIT_SIGN_EX = &quot;LIMIT ?, ?&quot;;      public static String resolveLimit(String pageSql) {         if (pageSql == null) {             return null;         }          if (pageSql.contains(SQL_SIGN)) {\/\/ \u5982\u679c\u9700\u8981\u7279\u6b8a\u5206\u9875             String changer = &quot;&quot;;              if (pageSql.contains(LIMIT_SIGN_EX)) {                 changer = LIMIT_SIGN_EX;             } else if (pageSql.contains(LIMIT_SIGN)) {                 changer = LIMIT_SIGN;             }              pageSql = pageSql.replace(changer, &quot;&quot;);             StringBuilder sqlBuilder = new StringBuilder(pageSql);              StringBuilder mae = new StringBuilder(sqlBuilder.substring(0, sqlBuilder.indexOf(SQL_SIGN)));\/\/ mae \u622a\u6b62sql\u8bed\u53e5\u5230 limitable             StringBuilder uShiRo = new StringBuilder(sqlBuilder.substring(sqlBuilder.indexOf(SQL_SIGN), sqlBuilder.length()));\/\/ \u5269\u4f59\u7684              mae.insert(mae.lastIndexOf(&quot;)&quot;), String.format(&quot; %s&quot;, changer));              return mae.append(uShiRo)                       .toString();         } else {             return pageSql;         }     } } <\/code><\/pre>\n<p>3\u3001\u65b0\u589e\u914d\u7f6e\u6587\u4ef6\u5982\u4e0b \u6ce8\u610f\uff0c\u8fd9\u91cc\u914d\u7f6e\u4e86\u6211\u4eec\u7684CustomPageInterceptor\uff082\u6b65\u9aa4\u91cc\u7684\u90a3\u4e2a\u7c7b\uff09\u4f5c\u4e3a\u63d2\u4ef6\uff0c\u5982\u679c\u4e0d\u8fdb\u884c\u6b64\u5904\u914d\u7f6e\uff0c\u6211\u4eec\u7684\u4fee\u6539\u662f\u4e0d\u8d77\u4f5c\u7528\u7684\u3002<\/p>\n<pre><code>\/**  * Created by Anur IjuoKaruKas on 2017\/12\/13.  *\/ @Configuration public class MybatisConfiguration {      @Bean     public SqlSessionFactory sqlSessionFactoryBean(@SuppressWarnings(&quot;SpringJavaInjectionPointsAutowiringInspection&quot;) DataSource dataSource) throws Exception {         SqlSessionFactoryBean factory = new SqlSessionFactoryBean();         factory.setDataSource(dataSource);         factory.setTypeAliasesPackage(Constant.MODEL_PACKAGE);          \/\/ \u914d\u7f6e\u5206\u9875\u63d2\u4ef6\uff0c\u8be6\u60c5\u8bf7\u67e5\u9605\u5b98\u65b9\u6587\u6863         PageHelperProperties properties = new PageHelperProperties();         properties.setPageSizeZero(&quot;true&quot;);\/\/ \u5206\u9875\u5c3a\u5bf8\u4e3a0\u65f6\u67e5\u8be2\u6240\u6709\u7eaa\u5f55\u4e0d\u518d\u6267\u884c\u5206\u9875         properties.setReasonable(&quot;true&quot;);\/\/ \u9875\u7801&lt;=0 \u67e5\u8be2\u7b2c\u4e00\u9875\uff0c\u9875\u7801&gt;=\u603b\u9875\u6570\u67e5\u8be2\u6700\u540e\u4e00\u9875         properties.setSupportMethodsArguments(&quot;true&quot;);\/\/ \u652f\u6301\u901a\u8fc7 Mapper \u63a5\u53e3\u53c2\u6570\u6765\u4f20\u9012\u5206\u9875\u53c2\u6570          CustomPageInterceptor pageInterceptor = new CustomPageInterceptor();         pageInterceptor.setProperties(properties.getProperties());         \/\/  \u6dfb\u52a0\u63d2\u4ef6         factory.setPlugins(new Interceptor[]{pageInterceptor});          \/\/ \u6dfb\u52a0XML\u76ee\u5f55         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();         factory.setMapperLocations(resolver.getResources(&quot;classpath:mapper\/*.xml&quot;));         return factory.getObject();     } }<\/code><\/pre>\n<p>4\u3001sql\u8bed\u53e5\u6ce8\u610f\u4e8b\u9879 \u5728\u9700\u8981\u88ab\u67e5\u8be2\u7684\u4e3b\u8868\u4e0a\uff0c\u4f60\u9700\u8981\u624b\u52a8\u5305\u88c5\u4e00\u5c42select\uff0c\u5e76\u5c06\u5176\u522b\u540d\u8bbe\u7f6e\u4e3alimitable\u3002AS\u5fc5\u987b\u5927\u5199\u3002<\/p>\n<pre><code>SELECT * FROM (SELECT *       FROM provider_order) AS limitable   LEFT JOIN order_info ON limitable.po_id = order_info.provider_order_id<\/code><\/pre>\n<p>\u8fd9\u662f\u539f\u8bed\u53e5\uff1a<\/p>\n<pre><code>SELECT *       FROM provider_order limitable   LEFT JOIN order_info ON limitable.po_id = order_info.provider_order_id<\/code><\/pre>\n<p>5\u3001\u67e5\u8be2\u65f6\u6ce8\u610f\u4e8b\u9879 \u5728\u67e5\u8be2\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4e3b\u8868\u7684\u4e3b\u952e\uff08\u5fc5\u987b\u552f\u4e00\uff09\u8bbe\u7f6e\u5230countColumn\u4e2d\u3002<\/p>\n<pre><code> Page page = PageHelper.startPage(0, 10);         page.setCountColumn(&quot;DISTINCT po_id&quot;);         List&lt;ProviderOrder&gt; providerOrderList = testService.getProviderOrderWithOrderInfoTest(&quot;%-%&quot;); <\/code><\/pre>\n<h3>\u5982\u679c\u5df2\u7ecf\u786e\u4fdd\u5b8c\u6210\u4e86\u4e0a\u9762\u7684\u4e94\u4e2a\u6b65\u9aa4\uff0c\u6267\u884c\u67e5\u8be2\u5373\u53ef\uff0c\u6211\u4eec\u5c06\u5f97\u5230\u6b63\u786e\u7684\u8fde\u8868\u67e5\u8be2\u7ed3\u679c\u3002<\/h3>\n<div style=\"padding: 10px 0; margin: 20px auto; width: 100%; font-size:16px; text-align: center;\">      <button disable=\"enable\" onclick=\"var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}\">          <span>\u6253\u8d4f<\/span><\/button>      <\/p>\n<div style=\"display: none;\">\n<div style=\"display: inline-block\">              <a rel=\"group\">                  <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/02\/20200205_5e3a73841f3ef.jpg\" alt=\"\u652f\u6301\u8fde\u8868\u7b49\u590d\u6742\u67e5\u8be2\u7684PageHelper\u2014\u2014PageHelper5.1.4 Mybatis3.4.6 - VPS\u8d44\u8baf\"><\/a>              <\/p>\n<p>\u5fae\u4fe1\u6253\u8d4f<\/p>\n<\/p><\/div>\n<div style=\"display: inline-block\">              <a rel=\"group\">                  <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/02\/20200205_5e3a73896da2a.jpg\" alt=\"\u652f\u6301\u8fde\u8868\u7b49\u590d\u6742\u67e5\u8be2\u7684PageHelper\u2014\u2014PageHelper5.1.4 Mybatis3.4.6 - VPS\u8d44\u8baf\"><\/a>              <\/p>\n<p>\u652f\u4ed8\u5b9d\u6253\u8d4f<\/p>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001PageHelper\u4e2d\u5206\u9875\u5b58\u5728&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/18302"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18302"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/18302\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18302"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}