开发者学堂课程【MyBatis持久层框架入门:分页的实现】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/21/detail/445
分页的实现
内容介绍
一.分析 mvsal.的分页语句:1imit startIndex,pageNum
二.通过 RowBounds,来实现分页
1、分析 mvsal.的分页语句:1imit startIndex,pageNum
l mapper 映射文件resultType="User">select * from user limit#{startIndex},#{pageSize}
l Dao 中的写法
//分页查询publicList getAll(int currentPage,int pageSize)throws I0Exception(SqlSession session=MyBatisUtil.getSession();Map map = new HashMap();
map. put("startIndex", (currentPage-1)*pageSize);map. put("pageSize", pageSize);List list =session.selectlist("cn.sxt.entity.UserMapper.selectAll",map);session.close();return list;
Ø 注意:不用为参数设置类,可以采用map结构来解决这个问题。
2、通过 RowBounds,来实现分页
l Mapper 文件不用做任何改变select * from user
Dao.中需要新建 RowBounds.对象
RowBounds rowBounds-new RowBounds(index,size);ndex是下标,size 数据
//分页查询public ListgetAll(int currentPage, intpageSize) throws
IOExceptionSqlSession session=MyBatisUtil getSession();RowBounds rowBounds = newRowBounds((currentPage -1)*pageSize,pageSize) ;List list =session.selectlist("cn.sxt.entity.UserMapper.getAll",null,rowBounds);session.close( );return list;
}
</div>