PageHelper自定义count

使用场景
web页面的查询功能太复杂,pageHelper自动生成的count语句相当于在查询语句外包一层count,查询速度比较慢。需要优化count语句,所以才想起来自定义count语句。

版本要求
5.0.4版本及以上

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.4</version>
</dependency>

使用方式
原有的代码不需要动,只需要在mybatis的xml文件里添加一个count查询
这里注意以下三点即可:

id和对应的查询语句保持一致,并且以 _COUNT 结尾
入参和对应的查询语句保持一致
出参为 resultType="Long"
查询语句

<select id="searchAllCondition" parameterType="com.demo.MyForm" resultMap="SearchResultMap">
select name,age,sex from student
</select>

count语句

<select id="searchAllCondition_COUNT" parameterType="com.demo.MyForm" resultType="Long">
select count(1) from student
</select>

注意以上demo两个语句的区别

count语句的id和查询语句的id
count语句的parameterType和查询语句的parameterType
count语句的出参一定是resultType="Long"
官方文档
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Changelog.md#504—2017-08-01

原文地址:https://www.cnblogs.com/651434092qq/p/15712568.html