14.分页查询
14.1 准备分页查询数据
xml
CREATE TABLE `student` (
`stu_id` int(11) NOT NULL AUTO_INCREMENT,
`stu_name` varchar(500) COLLATE utf8mb4_bin DEFAULT NULL,
`stu_no` varchar(500) COLLATE utf8mb4_bin DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
`address` varchar(500) COLLATE utf8mb4_bin DEFAULT NULL,
`face` varchar(500) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`stu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
/*Data for the table `student` */
insert into `student`(`stu_id`,`stu_name`,`stu_no`,`birthday`,`address`,`face`) values (1,'哈利波特','17040001','2021-03-24 14:06:40','伦敦','1'),(2,'郭靖','17040002','2021-03-24 14:06:43','大宋','2'),(3,'奥特曼','17040003','2021-03-16 14:06:44','日本','3'),(4,'成吉思汗','17040004','2021-03-10 14:06:46','蒙古','4'),(5,'特朗普','17040005','2021-03-24 14:06:48','美国','5'),(6,'分页查询0','17040005','2021-03-24 14:06:48','美国','5'),(7,'分页查询1','17040005','2021-03-24 14:06:48','美国','5'),(8,'分页查询2','17040005','2021-03-24 14:06:48','美国','5'),(9,'分页查询3','17040005','2021-03-24 14:06:48','美国','5'),(10,'分页查询4','17040005','2021-03-24 14:06:48','美国','5'),(11,'分页查询5','17040005','2021-03-24 14:06:48','美国','5'),(12,'分页查询6','17040005','2021-03-24 14:06:48','美国','5'),(13,'分页查询7','17040005','2021-03-24 14:06:48','美国','5'),(14,'分页查询8','17040005','2021-03-24 14:06:48','美国','5'),(15,'分页查询9','17040005','2021-03-24 14:06:48','美国','5'),(16,'分页查询10','17040005','2021-03-24 14:06:48','美国','5'),(17,'分页查询11','17040005','2021-03-24 14:06:48','美国','5'),(18,'分页查询12','17040005','2021-03-24 14:06:48','美国','5'),(19,'分页查询13','17040005','2021-03-24 14:06:48','美国','5'),(20,'分页查询14','17040005','2021-03-24 14:06:48','美国','5'),(21,'分页查询15','17040005','2021-03-24 14:06:48','美国','5'),(22,'分页查询16','17040005','2021-03-24 14:06:48','美国','5'),(23,'分页查询17','17040005','2021-03-24 14:06:48','美国','5'),(24,'分页查询18','17040005','2021-03-24 14:06:48','美国','5'),(25,'分页查询19','17040005','2021-03-24 14:06:48','美国','5'),(26,'分页查询20','17040005','2021-03-24 14:06:48','美国','5'),(27,'分页查询21','17040005','2021-03-24 14:06:48','美国','5'),(28,'分页查询22','17040005','2021-03-24 14:06:48','美国','5'),(29,'分页查询23','17040005','2021-03-24 14:06:48','美国','5'),(30,'分页查询24','17040005','2021-03-24 14:06:48','美国','5'),(31,'分页查询25','17040005','2021-03-24 14:06:48','美国','5'),(32,'分页查询26','17040005','2021-03-24 14:06:48','美国','5'),(33,'分页查询27','17040005','2021-03-24 14:06:48','美国','5'),(34,'分页查询28','17040005','2021-03-24 14:06:48','美国','5'),(35,'分页查询29','17040005','2021-03-24 14:06:48','美国','5');
使用步骤:
14.2 使用PageHelper分页查询
- 添加依赖
xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
- 配置PageHelper插件(SqlMapConfig.xml中配置插件)
xml
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
<property name="reasonable" value="true"/>
</plugin>
</plugins>
使用
在需要分页查询的sql 之前设置分页条件
- startPage(页号,每页显示条数)
java
public Page list(){
SqlSession session = factory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Page<Object> page = PageHelper.startPage(2, 10);
List<Student> list = mapper.selectAll();
for (Student student : list) {
System.out.println(student);
}
// page: 当前线程的分页结果
System.out.println("page = " + page);
return page;
}
- offsetPage
其他使用pagehelper的方式
java
//第一种,RowBounds方式的调用
List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(0, 10));
//第二种,Mapper接口方式的调用,推荐这种使用方式。
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectIf(1);
//第三种,Mapper接口方式的调用,推荐这种使用方式。
PageHelper.offsetPage(1, 10);
List<Country> list = countryMapper.selectIf(1);
//第四种,参数方法调用
//存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
public interface CountryMapper {
List<Country> selectByPageNumSize(
@Param("user") User user,
@Param("pageNum") int pageNum,
@Param("pageSize") int pageSize);
}
//配置supportMethodsArguments=true
//在代码中直接调用:
List<Country> list = countryMapper.selectByPageNumSize(user, 1, 10);
//第五种,参数对象
//如果 pageNum 和 pageSize 存在于 User 对象中,只要参数有值,也会被分页
//有如下 User 对象
public class User {
//其他fields
//下面两个参数名和 params 配置的名字一致
private Integer pageNum;
private Integer pageSize;
}
//存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
public interface CountryMapper {
List<Country> selectByPageNumSize(User user);
}
//当 user 中的 pageNum!= null && pageSize!= null 时,会自动分页
List<Country> list = countryMapper.selectByPageNumSize(user);
//第六种,ISelect 接口方式
//jdk6,7用法,创建接口
Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(new ISelect() {
@Override
public void doSelect() {
countryMapper.selectGroupBy();
}
});
//jdk8 lambda用法
Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(()-> countryMapper.selectGroupBy());
//也可以直接返回PageInfo,注意doSelectPageInfo方法和doSelectPage
pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(new ISelect() {
@Override
public void doSelect() {
countryMapper.selectGroupBy();
}
});
//对应的lambda用法
pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(() -> countryMapper.selectGroupBy());
//count查询,返回一个查询语句的count数
long total = PageHelper.count(new ISelect() {
@Override
public void doSelect() {
countryMapper.selectLike(country);
}
});
//lambda
total = PageHelper.count(()->countryMapper.selectLike(country));
源码阅读
以查询为例:
- DefaultSqlSession 查询集合或者是查询一个的时候都会走 selectList
- CachingExecutor 带缓存的 执行器
BaseExecutor
