重写MySqlDialect类下的getPageSql方法
pagehelper靠这个来插入limit位置的
public class MySqlDialectCustom extends MySqlDialect {
@Override
public String getPageSql(String sql, Page page, CacheKey pageKey) {
Pattern compileFixed = Pattern.compile("/\\*fixed\\*/");
String compileLimit = "/\\*limit\\*/";
// System.out.println("替换前的sql=" + sql);
if (compileFixed.matcher(sql).find()) {
if (page.getStartRow() == 0) {
sql = sql.replaceFirst(compileLimit, " LIMIT ? ");
} else {
sql = sql.replaceFirst(compileLimit, " LIMIT ?, ? ");
}
// System.out.println("替换后的sql=" + sql);
return sql;
}
return super.getPageSql(sql, page, pageKey);
}
}
覆盖后需配置
pagehelper.helperDialect=com.test.config.MySqlDialectCustom
sql写法
<!-- /*fixed*/ 代表将按指定位置插入limit
/*limit*/ 代表具体位置
MySqlDialectCustom
-->
<select id="queryAll" parameterType = "xxx" resultMap="xxx">
/*fixed*/ SELECT
xxx
FROM
xxx
WHERE
id IN ( SELECT id FROM ( SELECT id FROM `test` ORDER BY SWEEP_TIME DESC, SORT_NUMBER /*limit*/ ) a )
</select>
只需注意/*fixed*/ 和/*limit*/ 这两个注释 fixed代表开启 limit代表插入位置
这样就可以既可自定义 也可用原来的pageheper
最好也自定义count,原生的conut在有些情况下效率太低