pinked

导航

分页

分页

使用limit分页

select * from user limit startIndex, pageSize

使用mybatis实现分页,核心SQL

  1. 接口

    List<User> getUserByLimit(HashMap<String, Integer> map);
    
  2. Mapper.xml

    <select id="getUserByLimit" parameterType="map" resultType="user">
        select * from mybatis.user limit #{startIndex}, #{pageSize}
    </select>
    
  3. 测试

    @Test
    public void getUserByLimit() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("startIndex", 0);
        map.put("pageSize", 2);
        List<User> userList = mapper.getUserByLimit(map);
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    

使用RowBounds分页

不再使用SQL实现分页

  1. 接口

    List<User> getUserByRowBounds();
    
  2. Mapper.xml

    <select id="getUserByRowBounds" resultType="user">
        select * from mybatis.user
    </select>
    
  3. 测试

    @Test
    public void getUserRowBounds() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        RowBounds rowBounds = new RowBounds(1, 2);
        List<User> userlist = sqlSession.selectList("cn.pinked.dao.UserMapper.getUserByRowBounds", null, rowBounds);
        for (User user : userlist) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    

posted on 2020-01-09 20:43  pinked  阅读(105)  评论(0编辑  收藏  举报