Spring数据jpa有分页查询,还有很多需要分页的场景,为什么mybatis只提供selectList
?如何在mybatis中实现分页?谁能给我一个帮助?
答案 0 :(得分:1)
您必须传递RowBounds
的实例以进行分页,例如:
mybatisTemplate.selectList("query", params, new RowBounds(offset, limit));
答案 1 :(得分:0)
使用Mybatis-PageHelper
在pom.xml中添加
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>x.x.x</version>
</dependency>
在mybatis-config.xml中配置 数据库支持的是oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby
<!--
In the configuration file,
plugins location must meet the requirements as the following order:
properties?, settings?,
typeAliases?, typeHandlers?,
objectFactory?,objectWrapperFactory?,
plugins?,
environments?, databaseIdProvider?, mappers?
-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- config params as the following -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
用法示例:
mybatisTemplate.selectList("query", params, new RowBounds(offset, limit));
结果: 1)你不必在你的SQL中实现分页 2)mybatis-pagehelper将生成所需的分页sql
希望以上帮助