Mybatis分页查询


思考:为什么要分页?
减少数据的处理量
1、使用Limit分页
语法:

select * from user limit startIndex,pageSize;
select * from user limit 3;#[0,n]

使用Mybatis实现分页,核心SQL
接口

  /**
  * 分页查询
  * @return
  */
  List getUserByLimt(Map map);

Mapper.xml

    //测试
    public void getUserByLimt(){
        SqlSession sqlsession = MybatisUtil.getSqlsession();
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        HashMap map = new HashMap();
        map.put("startIndex",0);
        map.put("pageSize",3);
        List userByLimt = mapper.getUserByLimt(map);
        for (User user : userByLimt) {
            System.out.println(user);
        }
    }

2、RowBounds分页
不在使用SQL实现分页

接口

 /**
 *
 * @return
 */
 List getUserByRowBounds();
Mapper.xml

  //测试
public void getUserByRowBounds(){
    SqlSession sqlsession = MybatisUtil.getSqlsession();
    //RowBounds实现
   RowBounds rowBounds = new RowBounds(1, 2);
   //通过java层面实现分页
   List list = sqlsession.selectList("com.aostarit.dao.UserMapper.getUserByRowBounds",null,rowBounds);
   for (User user : list) {
       System.out.println(user);
    }
   //关闭sqlSession
   sqlsession.close();
}

你可能感兴趣的:(Mybatis分页查询)