分页

一、sql中limit使用来进行分页

select * from user limit startIndex,pagesize;

startIndex:从哪里开始查询
pazesize:每一页显示多少个
只输入一个参数,startIndex默认为0,输入参数为pagesize

1.Mapper接口
分页2.Mapper.xml配置

    <select id="getUserByLimit" parameterType="map" resultType="hello">
        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",2);
    map.put("pageSize",3);

    List<User> userList=mapper.getUserByLimit(map);
    for (User user : userList) {
        System.out.println(user);
    }
    sqlSession.close();
}

二、RowBounds(java代码层面实现)
1.接口

  List<User> getUserByRowBounds();

2.xml配置

 <select id="getUserByRowBounds" resultMap="UserMap">
        select * from mybatis.user
    </select>

3.测试

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

注意获取mapper的方式发生了改变

三、 插件 PageHelper

上一篇:查找一段字符串中相同元素出现的次数


下一篇:pa