【MyBatis】分页功能实现

1.sql语句limit直接分页

        //分页获得用户列表
        List<User> getUserListPage(Map<String,Integer> map);
  <select id="getUserListPage" parameterType="map" resultType="com.lei.pojo.User">
        select * from user limit #{startPage},#{pageSize}
    </select>

值得一提的是:Mysql的行是从0开始的,比如说 我写的limit 2,3表示数据库表结构里面的第3行和第4行

 //分页获得用户列表
    @Test
    public void testgetUserListPage()
    {
        SqlSession sqlSession=MybatisUtils.getSqlSession();

        Map<String,Integer> map=new HashMap<>();
        map.put("startPage",1);
        map.put("pageSize",2);
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        List<User> list=userMapper.getUserListPage(map);
        for(User tmp:list)
        {
            System.out.println(tmp);
        }
        sqlSession.close();
    }

如上图代码所示 我输入的是limit 1,2 我们看查询结果

 输出的是第2行和第3行

原文地址:https://www.cnblogs.com/cckong/p/14323007.html