关于mybatis的分页实现

一、为什么要分页

数据特别多的时候,单次请求返回大量的数据接口会非常慢。

对于数据量特别大的查询,我们都会采用分页查询

二、怎么设计分页

  • 每页有多少个

  • 当前是在第几页

  • 数据的总数

  • 数据列表

基于这些属性设计分页的实体类

public class PageInfo<T> {

    /**

     * 每页有多少个

     */

    private int pageSize;

    /**

     * 当前是在第几页

     */

    private int currentPage;

    /**

     * 数据的总数

     */

    private int total;

    /**

     * 数据列表

     */

    private List<T> list;

    // 获取偏移量

    public int getOffset() {

        return (this.currentPage - 1) * this.pageSize;

    }

}

三、实现分页功能

创建分页查询的方法

    /**

     * 分页查询

     * @param user     查询条件

     * @param offset   起始位置

     * @param pageSize 每页容量

     * @return 用户列表

     */

    List<User> page(@Param("user") User user, @Param("offset") int offset, @Param("pageSize") int pageSize);

    /**

     * 统计总数

     *

     * @param user 查询条件

     * @return 总数

     */

    int count(@Param("user") User user);

    <select id="page" resultType="User">

        select *

        from t_user

        <where>

            <if test="user.nickname != null and user.nickname != ''">

                and nickname like concat('%',#{user.nickname},'%')

            </if>

            <if test="user.username != null and user.username != ''">

                and username = #{user.username}

            </if>

        </where>

        limit #{offset},#{pageSize};

    </select>

    <select id="count" resultType="int">

        select count(*)

        from t_user

        <where>

            <if test="user.nickname != null and user.nickname != ''">

                and nickname like concat('%',#{user.nickname},'%')

            </if>

            <if test="user.username != null and user.username != ''">

                and username = #{user.username}

            </if>

        </where>

    </select>

测试

    @Test

    public void page(){

        PageInfo<User> pageInfo = new PageInfo<User>();

        pageInfo.setCurrentPage(1);

        pageInfo.setPageSize(10);

        User user = new User();

        user.setNickname("阿萨");

        // 加上筛选条件,根据nickname 或 username进行筛选

        List<User> list = userMapper.page(user,pageInfo.getOffset(),pageInfo.getPageSize());

        pageInfo.setList(list);

        pageInfo.setTotal(userMapper.count(user));

        System.out.println(pageInfo);

    }

四、分页插件

MyBatis 分页插件 PageHelper

1. 引入依赖

 <dependency>

            <groupId>com.github.pagehelper</groupId>

            <artifactId>pagehelper</artifactId>

            <version>5.2.0</version>

        </dependency>

2. 配置拦截器

在mybatis的配置文件中增加插件

<!--

    plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:

    properties?, settings?,

    typeAliases?, typeHandlers?,

    objectFactory?,objectWrapperFactory?,

    plugins?,

    environments?, databaseIdProvider?, mappers?

-->

<plugins>

    <!-- com.github.pagehelper为PageHelper类所在包名 -->

    <plugin interceptor="com.github.pagehelper.PageInterceptor">

        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->

        <property name="param1" value="value1"/>

    </plugin>

</plugins>

3. 配置插件

https://pagehelper.github.io/docs/howtouse/#2-%E9%85%8D%E7%BD%AE%E6%8B%A6%E6%88%AA%E5%99%A8%E6%8F%92%E4%BB%B6

4. 使用插件

    @Test

    public void testList() throws IOException {

        SqlSession session = MybatisUtils.openSession();

        User condition = new User();

        // 插件里提供的分页工具,在要查询之前,执行一下PageHelper.startPage(当前页数,每页的容量), 当使用工具时候,会导致懒加载失败

        // 加了这个操作,插件就会在sql语句中拼接limit限制,并且还会统计总个数

        PageHelper.startPage(1,5);

        List<User> users = session.getMapper(IUserMapper.class).list(condition);

        // 拿到结果之后通过PageInfo.of() 的方法,获得pageInfo

        com.github.pagehelper.PageInfo<User> list = com.github.pagehelper.PageInfo.of(users);

        System.out.println(users);

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值