为何要分页
使用Limit分页
语法结构
SELECT * FROM user LIMIT startIndex,pageSize;
SELECT * FROM user LIMIT 0,5;
## 从头查询三个
SELECT * FROM user LIMIT 3;
结合Mybatis实现分页,核心SQL(LIMIT)
// 用户分页查询
// 通过Map灵活传参
List<User> getUserByLimit(Map<String, Integer> map);
<!--用户分页查询-->
<select id="getUserByLimit" resultMap="UserMap">
SELECT * FROM user LIMIT #{startIndex},#{pageSize};
</select>
@Test
public void getUserByLimitTest() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("startIndex",0);
hashMap.put("pageSize",5);
List<User> userList = mapper.getUserByLimit(hashMap);
for ( User user : userList) {
System.out.println(user);
}
sqlSession.close();
}