程序员社区

MyBatis中实现分页查询

文章目录

        • 1.使用limit分页
        • 2.MyBatis中limit实现分页
        • 3.MyBatis中RowBounds实现分页(了解)

1.使用limit分页

select * from user limit startIndex,pageSize;

2.MyBatis中limit实现分页

1.编写接口:

 List<User> getUserByLimit(Map<String,Object> map);

2.编写实现类:

<!--分页-->
    <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, Object> map = new HashMap<String, Object>();
        map.put("startIndex",1);
        map.put("pageSize",2);
        List<User> userByLimit = mapper.getUserByLimit(map);
        for(User user:userByLimit){
            System.out.println(user);
        }
    }

3.MyBatis中RowBounds实现分页(了解)

1.编写接口:

List<User> getUserByRowBounds();

2.编写实现类:

<select id="getUserByRowBounds"  resultType="User">
  select * from mybatis.user
</select>

3.编写测试类:

@Test
public void getUserByRowBounds(){
   //RowBounds实现
   RowBounds rowBounds = new RowBounds(1, 2);

   SqlSession sqlSession = MyBatisUtils.getSqlSession();
   //通过java代码方面实现分页
   List<User> userList = sqlSession.selectList("com.kuang.dao.UserMapper.getUserByRowBounds",null,rowBounds);
   for(User user:userList){
       System.out.println(user);
   }
   sqlSession.close();
}

赞(0) 打赏
未经允许不得转载:IDEA激活码 » MyBatis中实现分页查询

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区