分页的实现
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;
}