分页——三种方式实现

分页

  • 分页展示,减少数据的处理量
使用limit分页

语法

select * from users limit startIndex,pageSize;

使用Mybatis实现分页,核心仍为SQL

  1. 接口
List<User> getUserByLimit(Map<String,Integer> map);
  1. Mapper.xml
<select id="getUserByLimit" parameterType="map" resultType="user">
    select * from mybatis.users limit #{startIndex},#{pageSize}
</select>
  1. 测试
    //分页
    @Test
    public void getUserByLimit(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("startIndex",0);//从第0个开始
        map.put("pageSize",10);//每页10个

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

在这里插入图片描述

RowBounds分页

使用代码实现分页,不用SQL

  1. 接口
//RowBounds分页
List<User> getUserByRowBounds();
  1. mapper.xml
<!--RowBounds分页-->
<select id="getUserByRowBounds" resultType="user">
    select * from mybatis.users
</select>
  1. 测试
//RowBounds分页
@Test
public void getUserByRowBounds(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();

    //RowBounds实现
    RowBounds rowBounds = new RowBounds(1,20);
    
    //通过Java代码层面实现分页
    List<User> userList = sqlSession.selectList("net.cqwu.dao.UserMapper.getUserByRowBounds",null,rowBounds);
    for (User user : userList) {
        System.out.println(user);
    }

    sqlSession.close();
}
分页插件pageHelper
  1. 导依赖
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.3.0</version>
</dependency>
  1. 配置文件【注意标签位置顺序在environments之前】
<!--插件注册-->
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>
  1. 用RowBounds方式调用
@Test
public void getUserByRowBounds(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();

    //RowBounds实现
    RowBounds rowBounds = new RowBounds(1,20);

    //通过Java代码层面实现分页
    List<User> userList = sqlSession.selectList("net.cqwu.dao.UserMapper.getUserByRowBounds",null,new RowBounds(0,10));
    for (User user : userList) {
        System.out.println(user);
    }

    sqlSession.close();
}
  1. 结果是这个B样?
[org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[com.github.pagehelper.PageInterceptor]-

,------.                           ,--.  ,--.         ,--.                         
|  .--. '  ,--,--.  ,---.   ,---.  |  '--'  |  ,---.  |  |  ,---.   ,---.  ,--.--. 
|  '--' | ' ,-.  | | .-. | | .-. : |  .--.  | | .-. : |  | | .-. | | .-. : |  .--' 
|  | --'  \ '-'  | ' '-' ' \   --. |  |  |  | \   --. |  | | '-' ' \   --. |  |    
`--'       `--`--' .`-  /   `----' `--'  `--'  `----' `--' |  |-'   `----' `--'    
                   `---'                                   `--'                        is intercepting.

[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 146370526.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 146370526 to pool.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Checked out connection 146370526 from pool.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@8b96fde]
[net.cqwu.dao.UserMapper.getUserByRowBounds]-==>  Preparing: select * from mybatis.users LIMIT ?
[net.cqwu.dao.UserMapper.getUserByRowBounds]-==> Parameters: 10(Integer)
[net.cqwu.dao.UserMapper.getUserByRowBounds]-<==      Total: 10
User{sid=1, sname='feliks', sex='男', pwd='123456'}
User{sid=2, sname='MaxPark', sex='女', pwd='456456'}
User{sid=4, sname='slugger', sex='女', pwd='654321'}
User{sid=5, sname='feliks0', sex='男', pwd='654321'}
User{sid=6, sname='feliks1', sex='男', pwd='654321'}
User{sid=7, sname='feliks2', sex='男', pwd='654321'}
User{sid=8, sname='feliks3', sex='男', pwd='654321'}
User{sid=9, sname='feliks4', sex='男', pwd='654321'}
User{sid=10, sname='feliks5', sex='男', pwd='654321'}
User{sid=11, sname='feliks6', sex='男', pwd='654321'}
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@8b96fde]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@8b96fde]
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 146370526 to pool.

Process finished with exit code 0
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值