Springboot项目配置PageHelper分页插件


PageHelper简介:MyBatis分页插件
PageHelper GitHub托管地址
中文文档
目前支持:

Oracle
Mysql
MariaDB
SQLite
Hsqldb
PostgreSQL
DB2
SqlServer(2005,2008)
Informix
H2
SqlServer2012
Derby
Phoenix
达梦数据库(dm)
阿里云PPAS数据库
神通数据库
HerdDB

一、pom.xml

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.11</version>
</dependency>
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
    <version>1.2.13</version>
</dependency>
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.13</version>
</dependency>

二、yml配置文件

pagehelper:
  helperDialect: mysql
  # 分页合理化
  reasonable: false
  supportMethodsArguments: true
  # 是否执行count参数名
  params: count=countSql

三、代码示例

service层:

PageInfo<UserVO> pageInfo = PageHelper.startPage(pageNum, pageSize)
        .doSelectPageInfo(() -> userMapper.selectList(vo));

mapper层:

public interface UserMapper {

    List<UserVO> selectList(@Param("vo") ParamVO vo);

    /**
     * 配合selectList分页使用,接口方法可不写,xml中写具体实现SQL即可
     * @param vo
     * @return 记录总条数
     * @see com.github.pagehelper.PageInterceptor
     */
    Long selectList_COUNT(@Param("vo") ParamVO vo);

}

xml:

 	<!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ${tbl}.id, ${tbl}.name, ${tbl}.create_time, ${tbl}.update_time
    </sql>

	<sql id="Condition">
        where 1=1
        <if test="vo.id != null">
            and ${tbl}.id = #{vo.id}
        </if>
        <if test="vo.name != null and vo.name != '' ">
            and ${tbl}.name = #{vo.name}
        </if>
        <if test="vo.create_time != null and vo.create_time != '' ">
            and ${tbl}.create_time >= STR_TO_DATE(#{vo.create_time}, '%Y-%c-%e')
        </if>
        <if test="vo.update_time != null and vo.update_time != '' ">
            and ${tbl}.update_time &lt; DATE_ADD(STR_TO_DATE(#{vo.update_time}, '%Y-%c-%e'),INTERVAL 1 DAY)
        </if>
    </sql>

    <select id="selectList" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"><property name="tbl" value="t1"/></include>   
        from tbl_user t1
        <include refid="Condition"><property name="tbl" value="t1"/></include>
    </select>


	<!-- 多表关联自定义COUNT_SQL,提升查询效率,若是单表查询可不写,此处仅作为示例 -->
    <select id="selectList_COUNT" resultType="java.lang.Long">
        select count(0) from tbl_user t1
        <include refid="Condition"><property name="tbl" value="t1"/></include>
    </select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬山境KL攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值