7、分页
思考:为什么分页?
-
减少数据的处理量
7.1 使用Limit分页
SELECT * from user limit startIndex,pageSize
1
1
使用MyBatis实现分页,核心SQL
-
接口
//分页
List<User> getUserByLimit(Map<String,Integer> map);
12 -
Mapper.xml
<!--分页查询-->
<select id="getUserByLimit" parameterType="map" resultMap="UserMap">
select * from user limit #{startIndex},#{pageSize}
</select>
1234 -
测试
@Test
public void getUserByLimit(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",1);
map.put("pageSize",2);
List<User> list = mapper.getUserByLimit(map);
for (User user : list) {
System.out.println(user);
}
}
123456789101112
7.2 RowBounds分页
不再使用SQL实现分页
-
接口
//分页2
List<User> getUserByRowBounds();
12 -
mapper.xml
<!--分页查询2-->
<select id="getUserByRowBounds">
select * from user limit #{startIndex},#{pageSize}
</select>
1234 -
测试
public void getUserByRowBounds(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
//RowBounds实现
RowBounds rowBounds = new RowBounds(1, 2);
//通过Java代码层面实现分页
List<User> userList = sqlSession.selectList("com.kaung.dao.UserMapper.getUserByRowBounds", null, rowBounds);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
1234567891011