首页 > 其他 > 详细

MyBatis(6)——分页的实现

时间:2020-02-16 15:37:19      阅读:66      评论:0      收藏:0      [点我收藏+]

分页的实现

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;
  }

MyBatis(6)——分页的实现

原文:https://www.cnblogs.com/inkqx/p/12316498.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!