7、分页
为什么分页
7.1、limit分页
SELECT *FROM USER LIMIT 2,2
接口UserMapper
public List<User> getUserByLimit(Map<String,Integer> map);
配置文件Mapper
<select id="getUserByLimit" resultType="User" parameterType="map">
select * from user limit #{begin},#{end};
</select>
方法测试
@Test
public void getUserByLimitTest(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("begin",1);
map.put("end",2);
List<User> userByLimit = mapper.getUserByLimit(map);
for (User user : userByLimit) {
System.out.println(user);
}
sqlSession.close();
}
7.2、RowBounds分页
不使用SQL实现分页
1.接口
public List<User> getUserByRowBounds();
2.Mapper.xml
<select id="getUserByRowBounds" resultType="User">
select *from user
</select>
3.测试
@Test
public void getUserByRowBounds(){
RowBounds rowBounds = new RowBounds(1,2);
SqlSession sqlSession = MybatisUtils.getSqlSession();
List<User> userList = sqlSession.selectList("com.lin.mapper.UserMapper.getUserByRowBounds", null, rowBounds);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
|