MyBatis(6)——分页的实现

我们两清 提交于 2020-02-16 13:43:23

分页的实现

a)通过mysql的分页查询语句:

说明:sql的分页语句格式为select * from aaa limit #{startIndex},#{pageSize}

//------------映射文件------------//
//*设置传入参数类型为Map,parameterType="Map"


<!--查询语句,分页查询-->
<!-- 因为字段名不一致,此处的resultType换成结果集映射resultMap -->
<select id="selectAll" parameterType="Map" resultMap="UserMap">
select * from users limit #{startIndex},#{pageSize}
</select>
//------------实体逻辑处理类:dao------------//
//*新建Map参数并传入


public List<User> getAll(int currentPage,int pageSize) throws IOException
{
  SqlSession session=MyBatisUtil.getSession();
  Map<String, Integer> map=new HashMap<String, Integer>();
  map.put("startIndex", (currentPage-1)*pageSize);
  map.put("pageSize", pageSize);
  List<User> list=session.selectList("cn.lxy.entity.UserMapper.selectAll",map);
  session.close();
  return list;
}

注:不需要通过新建实体类

b)通过RowBounds:

//------------映射文件------------//

<!-- 利用rowbounds实现分页查询 -->
<select id="getAll" resultType="User">
select * from users
</select>

dao中需新建rowBouns对象,构建格式为rowBounds(index,pageSize)

//------------dao类------------//

//分页查询所有的值2,利用rowbounds(原理同上)
  public List<User> getAll(int currentPage,int pageSize) throws IOException
  {
    SqlSession session=MyBatisUtil.getSession();
    RowBounds rowBounds=new RowBounds((currentPage-1)*pageSize, pageSize);
    List<User> list=session.selectList("cn.lxy.entity.UserMapper.getAll",null,rowBounds);
    session.close();
    return list;
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!