Mybatis之分页(Pagination of mybatis)

Mybatis

7、分页

7.1、思考:为什么要分页?

  • 减少数据的处理量

7.2、使用limit分页

  • 语法:select * from user limit startIndex,pageSize;
    select * from user limit 2,2; #[0,n]

  • 接口

    //分页
    List getUserByLimit(Map map);

  • //分页
    List getUserByLimit(Map map);

  • Mapper.xml

  • 测试

    static Logger logger = Logger.getLogger(UserMapperTest.class);
    @Test
    public void testGetUserByLimit(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    HashMap map = new HashMap();
    map.put(“startIndex”,1);
    map.put(“pageSize”,2);
    List userByLimit = mapper.getUserByLimit(map);
    for (User user : userByLimit) {
    System.out.println(user);
    }
    sqlSession.close();
    }

  • static Logger logger = Logger.getLogger(UserMapperTest.class);
    @Test
    public void testGetUserByLimit(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    HashMap map = new HashMap();
    map.put(“startIndex”,1);
    map.put(“pageSize”,2);
    List userByLimit = mapper.getUserByLimit(map);
    for (User user : userByLimit) {
    System.out.println(user);
    }
    sqlSession.close();
    }

7.3、RowBounds分页

  • 接口

    //分页2
    List getUserByRowBounds();

  • //分页2
    List getUserByRowBounds();

  • mapper.xml



  • 测试

    @Test
    public void getuserByRowBounds() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //RowBounds实现
    RowBounds rowBounds = new RowBounds(1, 2);
    // 通过Java代码层面实现分页
    List userList = sqlSession.selectList(“com.jcooling.mapper.UserMapper.getUserByRowBounds”,null,rowBounds);
    for (User user : userList) {
    System.out.println(user);
    }
    sqlSession.close();
    }

  • @Test
    public void getuserByRowBounds() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //RowBounds实现
    RowBounds rowBounds = new RowBounds(1, 2);
    // 通过Java代码层面实现分页
    List userList = sqlSession.selectList(“com.jcooling.mapper.UserMapper.getUserByRowBounds”,null,rowBounds);
    for (User user : userList) {
    System.out.println(user);
    }
    sqlSession.close();
    }

7.4、分页插件

  • pagehelper
————————

Mybatis

7. Pagination

7.1. Thinking: why pagination?

  • Reduce data processing

7.2. Use limit paging

  • 语法:select * from user limit startIndex,pageSize;
    select * from user limit 2,2; #[0,n]

  • 接口

    //分页
    List getUserByLimit(Map map);

  • //分页
    List getUserByLimit(Map map);

  • Mapper.xml

  • 测试

    static Logger logger = Logger.getLogger(UserMapperTest.class);
    @Test
    public void testGetUserByLimit(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    HashMap map = new HashMap();
    map.put(“startIndex”,1);
    map.put(“pageSize”,2);
    List userByLimit = mapper.getUserByLimit(map);
    for (User user : userByLimit) {
    System.out.println(user);
    }
    sqlSession.close();
    }

  • static Logger logger = Logger.getLogger(UserMapperTest.class);
    @Test
    public void testGetUserByLimit(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    HashMap map = new HashMap();
    map.put(“startIndex”,1);
    map.put(“pageSize”,2);
    List userByLimit = mapper.getUserByLimit(map);
    for (User user : userByLimit) {
    System.out.println(user);
    }
    sqlSession.close();
    }

7.3、RowBounds分页

  • 接口

    //分页2
    List getUserByRowBounds();

  • //分页2
    List getUserByRowBounds();

  • mapper.xml



  • 测试

    @Test
    public void getuserByRowBounds() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //RowBounds实现
    RowBounds rowBounds = new RowBounds(1, 2);
    // 通过Java代码层面实现分页
    List userList = sqlSession.selectList(“com.jcooling.mapper.UserMapper.getUserByRowBounds”,null,rowBounds);
    for (User user : userList) {
    System.out.println(user);
    }
    sqlSession.close();
    }

  • @Test
    public void getuserByRowBounds() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //RowBounds实现
    RowBounds rowBounds = new RowBounds(1, 2);
    // 通过Java代码层面实现分页
    List userList = sqlSession.selectList(“com.jcooling.mapper.UserMapper.getUserByRowBounds”,null,rowBounds);
    for (User user : userList) {
    System.out.println(user);
    }
    sqlSession.close();
    }

7.4 paging plug-in

  • pagehelper