Mybatis-6-分页

Mybatis --> 6. 分页

6.1 使用Limit实现分页

SELECT * FROM user LIMIT startIndex, pageSize
  • Mybatis实现分页核心:sql实现
  1. 接口UserMapper.java

    /**
     * 分页实现查询
     * @param map
     * @return
     */
    List<User> getUserByLimit(Map<String,Integer> map);
    
  2. UserMapper.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);
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("startIndex",1);
        map.put("pageSize",2);
        List<User> userList = mapper.getUserByLimit(map);
        for (User user: userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }
    

6.2 使用RowRounds分页

  1. 接口UserMapper.java

    /**
     * 分页实现查询
     * @return
     */
    List<User> getUserByRowRounds();
    
  2. UserMapper.xml

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

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

6.3 分页插件

上一篇:MapReduce原理深入理解3----WordCount程序流程图解、combiner(合并)程序示例、InputSplit切片详解


下一篇:aop的bug_SpringBoot使用AOP,内部方法失效的问题