分页的实现

1、分析mysql的分页语句:limit startIndex,pageNum

mapper映射文件

  1. <!-- 查询所有用户 -->
  2. <select id="selectAll" parameterType="Map" resultType="User">
  3. select * from user limit #{startIndex},#{pageSize}
  4. </select>

DAO中的写法

  1. //分页查询
  2. public List<User> getAll(int currentPage,int pageSize) throws IOException{
  3. SqlSession session=MyBatisUtil.getSession();
  4. Map<String,Integer> map = new HashMap<String,Integer>();
  5. map.put("startIndex", (currentPage-1)*pageSize);
  6. map.put("pageSize", pageSize);
  7. List<User> list = session.selectList("cn.sxt.entity.UserMapper.selectAll",map);
  8. session.close();
  9. return list;
  10. }

注意:不用为参数设置类,可以采用map结构来解决这个问题。

2、通过RowBounds来实现分页

Mapper文件不用做任何改变

  1. <select id="getAll" resultType="User">
  2. select * from user
  3. </select>

Dao中需要新建RowBounds对象

RowBounds rowBounds=new RowBounds(index,size);index是下标,size数据

  1. //分页查询
  2. public List<User> getAll(int currentPage,int pageSize) throws IOException{
  3. SqlSession session=MyBatisUtil.getSession();
  4. RowBounds rowBounds = new RowBounds((currentPage-1)*pageSize,pageSize);
  5. List<User> list = session.selectList("cn.sxt.entity.UserMapper.getAll",null,rowBounds);
  6. session.close();
  7. return list;
  8. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值