Mybatis学习笔记-分页

为何要分页

  • 减少数据处理量
  • 便于前端展示数据

使用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);
  • Mapper.xml
<!--用户分页查询-->
<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();
}
posted @ 2021-08-06 17:25  菜鸢爱敲bug  阅读(56)  评论(0编辑  收藏  举报