永康物流网站,蒙牛企业网站建设规划书,台州建设局网站,深圳宝安什么时候解封文章目录 mybatis 中#xff0c;使用 RowBounds 进行分页#xff0c;非常方便#xff0c;不需要在 sql 语句中写 limit#xff0c;即可完成分页功能。但是由于它是在 sql 查询出所有结果的基础上截取数据的#xff0c;所以在数据量大的sql中并不适用#xff0c;它更适合在… 文章目录 mybatis 中使用 RowBounds 进行分页非常方便不需要在 sql 语句中写 limit即可完成分页功能。但是由于它是在 sql 查询出所有结果的基础上截取数据的所以在数据量大的sql中并不适用它更适合在返回数据结果较少的查询中使用。 由于 java 允许的最大整数为 2147483647所以 limit 能使用的最大整数也是 2147483647一次性取出大量数据可能引起内存溢出所以在大数据查询场合慎重使用。
下面看下RowBounds分页用法
假设数据库表如下所示 对应的mapper文件如下所示
resultMap idresultMap1 typecom.lzj.bean.Personresult columnPERSON_ID propertyid/resultresult columnPERSON_NAME propertyname/resultresult columnPERSON_AGE propertyage/result
/resultMap
select idselect1 resultMapresultMap1select * from PERSON where PERSON_ID #{personId}
/select对应的接口如下所示
public interface PersonDao {public ListPerson select1(int personId);
}执行如下测试代码
public void sqlSessionTest5(){SqlSessionFactory factory mybatisUtil.getFactory();SqlSession sqlSession factory.openSession(true); //true表示自动提交ListPerson 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_ageRow: 6, Bob, 25Row: 7, Jimi, 24Row: 8, Dobu, 40
输出结果[Person{id7, nameJimi, age24}, Person{id8, nameDobu, age40}]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl2f236de0]
Returned connection 790851040 to pool.从结果中可以看出RowBounds(12)只获取了从第2条数据开始的2条数据。但是底层SQL查询的时候还是从第1条开始查然后从查询结果中过滤掉第1条数据所以SQL性能也比较慢因为查询查询量比较大。尤其是当大数据量分页时会比较慢因为会查询出所有的数据量然后过滤掉前面的数据。
参考https://cloud.tencent.com/developer/article/2130987