分页
- 分页展示,减少数据的处理量
使用limit分页
语法
select * from users limit startIndex,pageSize;
使用Mybatis实现分页,核心仍为SQL
- 接口
List<User> getUserByLimit(Map<String,Integer> map);
- Mapper.xml
<select id="getUserByLimit" parameterType="map" resultType="user">
select * from mybatis.users limit #{startIndex},#{pageSize}
</select>
- 测试
//分页
@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
- 接口
//RowBounds分页
List<User> getUserByRowBounds();
- mapper.xml
<!--RowBounds分页-->
<select id="getUserByRowBounds" resultType="user">
select * from mybatis.users
</select>
- 测试
//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
- 导依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
- 配置文件【注意标签位置顺序在environments之前】
<!--插件注册-->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="param1" value="value1"/>
</plugin>
</plugins>
- 用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();
}
- 结果是这个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