- 传统的使用limit分页
select * from user limit 0,5
使用mybatis实现分页(核心其实就是sql):
- 接口
List<User> getuserbyLimit(Map<String,Integer> map);
- mapper.xml
<select id="getuserbyLimit" parameterType="map" resultMap="UserMap">
select * from user limit #{startIndex},#{pageSize}
</select>
- 测试
@Test //获取用户分页列表
public void getuserbyLimit(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String,Integer> map =new HashMap<String,Integer>();
map.put("startIndex",0);
map.put("pageSize",5);
List<User> users = mapper.getuserbyLimit(map);
for (User user: users){
System.out.println(user);
}
sqlSession.close();
}