为什么要分页?
减少数据的处理量使用Mybatis实现分页,核心SQL
接口
//分页查询 List<User> getUserByLimit(Map(String, Integer) map);Mapper.xml
<!--分页查询--> <select id="getUserByLimit" parameterType="map" resultMap="UserMap"> select * from mybatis.user limit #{startIndes},#{pageSize} </select>测试
//分页查询 @Test public void getUserByLimit(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("startIndes",0); map.put("pageSize",3); List<User> userList = mapper.getUserByLimit(map); for (User user : userList) { System.out.println(user); } sqlSession.close(); }不再使用SQL实现分页
接口
//分页查询2 List<User> getUserByRowBounds();mapper.xml
<!--分页查询2--> <select id="getUserByRowBounds" resultMap="UserMap"> select * from mybatis.user </select>测试
//分页查询2 @Test public void getUserByRowBounds(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); //RowBounds实现 RowBounds rowBounds = new RowBounds(1,2); //通过java代码实现分页 List<User> list = sqlSession.selectList("com.cfeng.dao.UserMapper.getUserByRowBounds", null, rowBounds); for (User user : list) { System.out.println(user); } sqlSession.close(); }了解即可!