使用場景
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