Mybatis-分页

为什么要分页?

  • 减少数据的处理

使用Limit分页

  • 语法
select * from mybatis.student limit startIndex,pageSize
select * from mybatis.student limit 3
//查询0-3号的记录

使用Mybatis实现分页

  • 接口
List<User> getuserbylimit(Map<String,Integer> map);
  • 接口实现类(mapper.xml)
<select id="getuserbylimit" parameterType="map"  resultMap="userMap">
        select * from mybatis.student limit #{startindex},#{pagesize}
    select>
  • 测试
    @Test
    public void testgetuserbylimit(){
     
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        HashMap<String, Integer> map = new HashMap<>();
        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实现分页

  • 接口
//使用RowBounds
    List<User> getuserbyrowbounds();
  • mapper.xml
<select id="getuserbyrowbounds"  resultMap="userMap">
        select * from mybatis.student
    </select>
  • 测试类
    @Test
    public void testgetuserbyrowbounds(){
     
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        RowBounds rowBounds = new RowBounds(1,2);
        List<User> userList = sqlSession.selectList("com.qk.dao.UserDao.getuserbyrowbounds",null,rowBounds);
        for (User user : userList) {
     
            System.out.println(user);
        }
        sqlSession.close();
    }

分页插件实现分页

Mybatis-分页_第1张图片
链接: 官网.

你可能感兴趣的:(Mybatis学习)