Mybatis logical page and physical page

When we use Mybatis implement paging operations, we need to pay attention to logical page and physical page Mybatis difference between the two

Logical page: the one-time data from the database into memory isolated, the paging logic in memory

Physical Page: direct specific SQL statements need only check out the data from the database

mybatis comes with pagination RowBounds: // logical page

Business logic code

public List<User> findUserByPage() {
//
TODO Auto-generated method stub
SqlSession sqlSession =
ssf.openSession();
int offSet = 0;
int limit = 2;
User user =
new User();
user.setName("qing");
RowBounds rowRounds =
new RowBounds(offSet, limit);
List<User> listUsers = sqlSession.selectList("select UserByPage", user, rowRounds);
return listUsers;
}
<select id="selectUserByPage" resultType="test_ibatis.User"
resultSetType="SCROLL_SENSITIVE"parameterType="test_ibatis.User">
select * from t_user where name like '%'||#{name}||'%'
</select>

mybatis from or by writing sql pagination plug PageHelper: // physical page

Pagination plug PageHelper

Service Layer:

PageHelper.startPage(pageNum,pageSize);//pageNum 页数  pageSize 数量           

List<Student> stu=studentDao.findStudent();  //studentDao @Autowried注解获取; 在执行查询数据时,就会自动执行2个sql;执行上述Mapper下的ID为findStudent的sql 自动执行分页,通过PageHelper进行识别是何数据库拼接分页语句,若是mysql,自动通过limit分页,若是oracle自动通过rownum进行分页,另一个会自动拼接Mapper下不存在的ID为findStudent_COUNT,查询的总数;可以通过打印的日志进行跟踪;          

 PageInfo<Student> page = new PageInfo<Student>(stu); //自动封装总数count以及分页,数据返回页面           
 
 return page;//返回分页之后的数据 

Dao layer -StudentDao:
List FindStudent ();

Mapper:

 <select id="findStudent" resultType="Student">               
  select * from Student          
   </select>

Mybatis write their own SQL pagination

。。。。。。。。。

to sum up:

1: logical page memory overhead is relatively large, high efficiency in the case where a small amount of data than the physical page; in the case of large amount of data, the memory overhead is too large, easily out of memory, is not recommended

2: physical page memory overhead is relatively small, the data amount is small in the case where efficiency is still lower than the logical page, in the case where large amount of data, recommended physical page

Published 45 original articles · won praise 3 · Views 2316

Guess you like

Origin blog.csdn.net/weixin_44046437/article/details/99674223