day05-SSM整合
目录:
第三章 分页技术
1.1 MyBatis分页插件
1.2 分页插件相关API
第三章 分页技术
1.1 MyBatis分页插件
(1)分页插件依赖错标
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.1.2version>
dependency>
(2)配置My分页插件,配置在SqlSessionFactoryBean中
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysqlprop>
props>
property>
bean>
array>
property>
1.2 分页插件相关API
(1)PageHelper类:开启分页功能
静态方法:
startPage(int currentPage,int pageSize):开启分页功能,就在SQL语句后面附加LIMIT子句查询公的记录数;不开启就还是按照原样查询。
currentPage:当前页数
pageSize:每页条数
(2)PageInfo类:封装分页结果集数据
PageInfo类相关属性
| firstPage | 第一页 | lastPage | 最后一页 | ||
| pageNum | 当前页 | nextPage | 下一页 | prePage | 上一页 |
| hasPreviousPage | 是否有上一页 | hasNextPage | 是否有下一页 | ||
| isFirstPage | 是否是首页 | isLastPage | 是否是尾页 | ||
| startRow | 当前页的第一行 | endRow | 当前页最后一行数 | ||
| pageSize | 每页条数 | size | 当夜数据条数 | ||
| pages | 总页数 | total | 数据总条数 | list | 当页数据列表 |
:
(3)service层实现
//分页查询用户数据
@Transactional(readOnly = true)
@Override
public PageInfo queryAccountByPage(Integer currentPage, Integer pageSize) {
//开启分页查询功能
PageHelper.startPage(currentPage, pageSize);
List accountList = accountMapper.listAccount();
//封装分页结果数据集
return new PageInfo(accountList);
}
(4)controller层实现
@RequestMapping("queryUserByPage")
public @ResponseBody PageInfo queryAccountByPage(Integer currentPage){
PageInfo accountPageInfo = accountService.queryAccountByPage(currentPage, 4);
List list = accountPageInfo.getList();
for (Account account : list) {
System.out.println("account = " + account);
}
int pages = accountPageInfo.getPages();
System.out.println("pages = " + pages);
long total = accountPageInfo.getTotal();
System.out.println("total = " + total);
return accountPageInfo;
}