首先说明一下,这种涉及了在MyBatis(二)中说的那个第二种老方法,所以一般不推荐使用。
上一篇我们利用SQL的limit实现了分页,是在SQL层面的,那么这次我们利用java代码RowBounds来实现。直接上操作。
一、RowBounds实现分页
1.在UserMapper接口中声明一个新的方法- //利用RowBounds进行分页
- List<User> getUserbyRowBounds();
复制代码 2.在UserMapper.xml中实现这个方法- <select id="getUserbyRowBounds" resultMap="UserMap">
- select * from mybaties.user
- </select>
复制代码 3.junit测试- @Test
- public void RowBoundstest() {
- //利用工具类获取SqlSession
- SqlSession sqlSession = MyBatisUtil.getSqlSession();
- RowBounds rowBounds = new RowBounds(5, 10);
- List<User> userList = sqlSession.selectList("com.jms.dao.UserMapper.getUserbyRowBounds",
- null, rowBounds);
- for (User user : userList) {
- System.out.println(user);
- }
- }
复制代码 测试结果:

二、MyBatis分页插件pageHelper
官方地址:https://pagehelper.github.io/
三、自己实现一个分页的类
1.建立一个工具类PaginationUtil.class- package com.jms.utils;
- import java.util.List;
- public class PaginationUtil {
- public static <T> List<T> Pagination(List<T> list, int offset , int limit) {
- while (offset > 0) {
- list.remove(0);
- offset --;
- }
- while (list.size() > limit) {
- list.remove(limit);
- }
- return list;
- }
- }
复制代码 2.junit测试- @Test
- public void test2() {
- SqlSession sqlSession = MyBatisUtil.getSqlSession();
- UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
- List<User> userList = userMapper.getUserbyRowBounds();
- PaginationUtil.Pagination(userList, 5, 10);
- for (User user : userList) {
- System.out.println(user);
- }
- }
复制代码 测试结果:

(本文仅作个人学习记录用,如有纰漏敬请指正)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |