一、分析mysql的分页语句:limit startIndex, pageSize
mapper映射文件:
<!-- 查询所有用户 -->
<select
id="selectAll"
parameterType="Map"
resultType="User">
select * from user
limit #{startIndex}, #{pageSize}
</select>
dao中的写法:
//分页查询
public
List<User> getAll(int
currentPage,
int
pageSize)
throws
IOException {
SqlSession
session
= MyBatisUtil.getSqlSession();
Map<String, Integer>
map
= new
HashMap<String, Integer>();
map.put("startIndex",
(currentPage
- 1) *
pageSize);
map.put("pageSize",
pageSize);
List<User>
list
= session.selectList("com.liujie.model.UserMapper.selectAll",
map);
session.close();
return
list;
}
| 注意:不用为参数设置类,可以采用Map结构来解决这个问题 |
二、通过RowBounds来实现分页
mapper映射文件不需要做任何改变:
<!-- 查询所有用户 -->
<select
id="selectAll"
resultType="User">
select * from user
</select>
dao中需要新建RowBounds对象:
|
RowBounds
rowBounds
=
new
RowBounds(pageIndex,
pageSize);
|
|
public
RowBounds(int
offset,
int
limit)
|
//分页查询
public
List<User> getAll(int
currentPage,
int
pageSize)
throws IOException {
SqlSession
session
= MyBatisUtil.getSqlSession();
RowBounds
rowBounds
= new
RowBounds((currentPage-1)*pageSize,
pageSize);
List<User>
list
= session.selectList("com.liujie.model.UserMapper.selectAll",
null,
rowBounds);
session.close();
return
list;
}
767

被折叠的 条评论
为什么被折叠?



