SQL分页查询

1、分页

思考:为什么要分页?

  • 减少数据的处理量

使用Limit分页

#语法:
SELECT * FROM `user` LIMIT startIndex,pageSize;

# [0,3]
SELECT * FROM `user` LIMIT 3;

使用Mybatis实现分页,核心SQL
1.接口

//分页
List getUserByLimit(Map map);

2.Mapper.xml


    
    
    
    

3.测试

@Test
public void getUserByLimit(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserDao.class);

    Map map = new HashMap<>();
    map.put("startIndex",0);
    map.put("pageSize",2);

    List userList = mapper.getUserByLimit(map);
    for(User user:userList){
        System.out.println(user);
    }

    //关闭
    sqlSession.close();
    
}

你可能感兴趣的:(mybatissql)