7.分页
1.使用limit进行分页
核心sql语句 : select * from tableName limit startIndex,pageSize;
使用MyBatis分页
(1)接口
//分页
List<User> getUserLimit(Map<String,Integer> map);
(2)mapper.xml
<select id="getUserLimit" parameterType="map" resultMap="rsMap">
select * from mybatis.user limit #{startIndex},#{pageSize}
</select>
(3)测试
//分页
@Test
public void getUserLimit(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//定义map
Map<String, Integer> map = new HashMap<>();
map.put("startIndex",0);
map.put("pageSize",3);
List<User> userList = mapper.getUserLimit(map);
for(User user:userList){
System.out.println(user);
}
sqlSession.close();
}
使用RowBounds分页
(1)接口
//使用rowbounds,查询全部用户
List<User> getUserLimit2();
(2)mapper.xml
<select id="getUserLimit2" resultMap="rsMap">
select * from mybatis.user
</select>
(3)测试
@Test
public void getUserLimit2(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
//创建RowBounds对象,参数offset limit
RowBounds rowBounds = new RowBounds(0, 2);
//不推荐使用此方式
List<User> userList = sqlSession.selectList("com.com.zuo.dao.UserMapper.getUserLimit2",null,rowBounds);
for (User user : userList){
System.out.println(user);
}
sqlSession.close();
}
插件分页 mybatis pageHeader