SpringBoot 使用 MyBatis 分页插件
MyBatis PageHelper 是一个 MyBatis 分页插件,能够比较方便的实现 MyBatis 的分页
添加依赖
1
| compile('com.github.pagehelper:pagehelper-spring-boot-starter:1.2.3')
|
添加配置
1 2 3 4 5 6 7 8
| pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql pagehelper.row-bounds-with-count=true pagehelper.offset-as-page-num=true pagehelper.page-size-zero=true
|
pagehelper.helper-dialect : 指定分页插件使用哪种语言
pagehelper.offset-as-page-num : 默认为 false, 该参数对使用RowBounds作为分页参数时有效,当为true时,会将RowBounds的offset参数当成pageNum使用
pagehelper.row-bounds-with-count : 默认为false,该参数对使用RowBounds作为分页参数时有效,当该参数值为true时,使用RowBounds分页会进行count查询
pagehelper.page-size-zero : 默认为false,当该参数为true时,如果pageSize=0或者RowBounds.limit=0就会查询所有结果
pagehelper.reasonable : 分页合理化参数,默认为false,当该值为true,pageNum<=0默认查询第一页,pageNum>pages时会查询最后一页,false时直接根据参数进行查询
pagehelper.params : 为了支持startPage(Object params)方法,增加该参数来配置参数映射,用于从对象中根据属性名取值,可以配置pageNum,pageSize,pageSizeZero, reasonable, 不配置映射是使用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
pagehelper.support-methods-arguments : 支持通过 Mapper接口参数来传递分页参数,默认为false, 分页插件会从查询方法的参数中根据params配置的字段中取值,查找到合适的就进行分页
pagehelper.auto-runtime-dialect : 默认为false, 为true时允许在运行时根据多数据源自动识别对应的方言进行分页
pagehelper.close-conn : 默认为true, 当使用运行是动态数据源或者没有设置helperDialect属性自动获取数据库类型时,会自动获取一个数据库连接,通过该属性来设置是否关闭获取的这个连接,默认为true关闭,false不会自动关闭
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Product implements Serializable {
private long id;
private String name;
private long price;
private Integer pageNum;
private Integer pageSize;
}
|
1 2 3 4 5 6
| <resultMap id="baseResultMap" type="cn.com.hellowood.mybatis.modal.Product"> <id column="id" property="id" javaType="java.lang.Long" jdbcType="INTEGER"></id> <result column="name" property="name" javaType="java.lang.String" jdbcType="VARCHAR"></result> <result column="price" property="price" javaType="java.lang.Long" jdbcType="BIGINT"></result> </resultMap>
|
使用PageHelper.startPage()或 PageHelper.offsetPage()
1 2 3 4 5 6 7
| public PageInfo<Product> getByPageHelper() { PageHelper.startPage(request); return new PageInfo<>(productDao.getByPageHelper()); }
|
1 2 3 4
| <select id="getByPageHelper" resultMap="baseResultMap"> SELECT * FROM product </select>
|
使用RowBounds传递分页参数
1 2 3
| public PageInfo<Product> getByRowBounds(Integer pageNum, Integer pageSize) { return new PageInfo<>(productDao.getByRowBounds(new RowBounds(pageNum, pageSize))); }
|
1 2 3 4
| <select id="getByRowBounds" resultMap="baseResultMap"> SELECT * FROM product </select>
|
Dao 接口直接传递分页参数
1 2 3
| public PageInfo<Product> getByInterfaceArgs(Integer pageNum, Integer pageSize) { return new PageInfo<>(productDao.getByInterfaceArgs(pageNum, pageSize)); }
|
1 2 3 4
| <select id="getByInterfaceArgs" resultMap="baseResultMap"> SELECT * FROM product </select>
|
通过Modal传递分页参数
- 需要
Product中的pageSize和pageNum都有效
1 2 3
| public PageInfo<Product> getByModalArgs(Product product) { return new PageInfo<>(productDao.getByModalArgs(product)); }
|
1 2 3 4
| <select id="getByModalArgs" resultMap="baseResultMap" parameterType="cn.com.hellowood.mybatis.modal.Product"> SELECT * FROM product </select>
|