5.6 mybatis之RowBounds分页用法

文章目录


mybatis 中,使用 RowBounds 进行分页,非常方便,不需要在 sql 语句中写 limit,即可完成分页功能。但是由于它是在 sql 查询出所有结果的基础上截取数据的,所以在数据量大的sql中并不适用,它更适合在返回数据结果较少的查询中使用。

由于 java 允许的最大整数为 2147483647,所以 limit 能使用的最大整数也是 2147483647,一次性取出大量数据可能引起内存溢出,所以在大数据查询场合慎重使用。

下面看下RowBounds分页用法

假设数据库表如下所示
在这里插入图片描述
对应的mapper文件如下所示

<resultMap id="resultMap1" type="com.lzj.bean.Person">
    <result column="PERSON_ID" property="id"></result>
    <result column="PERSON_NAME" property="name"></result>
    <result column="PERSON_AGE" property="age"></result>
</resultMap>
<select id="select1" resultMap="resultMap1">
    select * from PERSON where PERSON_ID > #{personId}
</select>

对应的接口如下所示

public interface PersonDao {
    public List<Person> select1(int personId);
}

执行如下测试代码

public void sqlSessionTest5(){
    SqlSessionFactory factory = mybatisUtil.getFactory();
    SqlSession sqlSession = factory.openSession(true);  //true表示自动提交
    List<Person> list = sqlSession.selectList("com.lzj.dao.PersonDao.select1", 5, new RowBounds(1, 2));
    System.out.println("输出结果:" + list);
    sqlSession.close();
}

输出结果如下所示

Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 790851040.
==>  Preparing: select * from PERSON where PERSON_ID > ? 
==> Parameters: 5(Integer)
<==    Columns: person_id, person_name, person_age
<==        Row: 6, Bob, 25
<==        Row: 7, Jimi, 24
<==        Row: 8, Dobu, 40
输出结果:[Person{id=7, name='Jimi', age=24}, Person{id=8, name='Dobu', age=40}]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2f236de0]
Returned connection 790851040 to pool.

从结果中可以看出RowBounds(1,2)只获取了从第2条数据开始的2条数据。但是底层SQL查询的时候还是从第1条开始查,然后从查询结果中过滤掉第1条数据,所以SQL性能也比较慢,因为查询查询量比较大。尤其是当大数据量分页时会比较慢,因为会查询出所有的数据量,然后过滤掉前面的数据。

参考:https://cloud.tencent.com/developer/article/2130987

相关推荐

  1. 带你学习Mybatismybatis实现

    2024-04-12 16:40:02       27 阅读
  2. Mybatis

    2024-04-12 16:40:02       50 阅读
  3. MyBatis-Plus】查询

    2024-04-12 16:40:02       65 阅读
  4. mybatis&Mysql 查询

    2024-04-12 16:40:02       62 阅读
  5. MyBatis-Plus 实现

    2024-04-12 16:40:02       48 阅读
  6. MyBatis如何实现

    2024-04-12 16:40:02       27 阅读
  7. SQL

    2024-04-12 16:40:02       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 16:40:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 16:40:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 16:40:02       87 阅读
  4. Python语言-面向对象

    2024-04-12 16:40:02       96 阅读

热门阅读

  1. mysql定时任务-事件调度器(Event Scheduler)

    2024-04-12 16:40:02       41 阅读
  2. linux环境openfile限制

    2024-04-12 16:40:02       42 阅读
  3. 数组、数组对象去重

    2024-04-12 16:40:02       38 阅读
  4. python 最简单的网页爬虫

    2024-04-12 16:40:02       32 阅读
  5. Python 大麦抢票脚本

    2024-04-12 16:40:02       43 阅读
  6. AcWing-5:多重背包问题 II

    2024-04-12 16:40:02       37 阅读
  7. C++| QT定时器QTimer

    2024-04-12 16:40:02       35 阅读