1:原生方式
(1):建立分页工具类:内含总记录数,总页码数,当前页码数,每页最大容量。
package cn.smbms.utils;
/**
* @author Louis
* @date 2023/6/6 18:44
* @Description 分页工具类
*/
public class PageSupport {
//当前页码数
private int currentPageNo = 1;
//总容量
private int totalCount = 0;
//页面容量
private int pageSize = 0;
//总页数
private int totalPageCount = 1;
/**流程如下:我们去数据库内得到总记录数后,传入到totalCount属性的set方法内,保存总记录数的同时,一并换算出总页数*/
public int getCurrentPageNo() {
return currentPageNo;
}
public void setCurrentPageNo(int currentPageNo) {
if (currentPageNo>0){
this.currentPageNo = currentPageNo;
}else{
this.currentPageNo = 1;
}
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if (totalCount>0){
this.totalCount = totalCount;
//一并计算出总页数
}
setTotalPageCount();
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if (pageSize>0){
this.pageSize = pageSize;
}else{
this.pageSize = 2;
}
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount() {
if(this.totalCount%this.pageSize==0){
this.totalPageCount = this.totalCount/this.pageSize;
}else{
this.totalPageCount = (this.totalCount/this.pageSize)+1;
}
}
}
(2):建立接口方法(入参)
public List<User> getUserListByLimit(@Param("pageStart") Integer pageStart,@Param("pageSize")Integer pageSize);
(3):对应Mapper.xml文件建立sql语句
<!--分页查询用户信息-->
<select id="getUserListByLimit" resultType="User">
SELECT * FROM smbms_user u
LIMIT #{pageStart},#{pageSize}
</select>
(4):test测试
@Test
public void getUserListByLimitTest(){
try{
//获取
PageSupport pageSupport = new PageSupport();
sqlSession = MybatisUtil.getSqlSession();
//执行
int totalCount = sqlSession.getMapper(UserMapper.class).findUserCount();
pageSupport.setPageSize(pageSupport.getPageSize());
pageSupport.setTotalCount(totalCount);
logger.info("当前记录一共有: "+pageSupport.getTotalCount()+"条");
logger.info("每页显示: "+pageSupport.getPageSize()+"行");
logger.info("一共有: "+pageSupport.getTotalPageCount()+"页");
pageSupport.setCurrentPageNo(4);
logger.info("查询第: "+pageSupport.getCurrentPageNo()+"页,结果如下: ");
List<User> userList = sqlSession.getMapper(UserMapper.class).getUserListByLimit((pageSupport.getCurrentPageNo()-1)* pageSupport.getPageSize(),pageSupport.getPageSize());
//解析
if (userList.size()>0){
for (User user : userList){
System.out.println(user.toString());
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
MybatisUtil.closeSqlSession(sqlSession);
}
}
2:插件方式
(1):插件导入(Mybatis配置文件中声明)
<!--4:插件标签-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
(2):建立接口方法
(3):对应Mapper.xml文件建立sql语句(同上)
(4):test测试
@Test
public void getUserListByLimitUtilTest(){
SqlSession sqlSession = null;
List<User> userList = new ArrayList<User>();
Page<User> objects = null;
try{
sqlSession = MybatisUtil.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//核心代码
objects = PageHelper.startPage(1,3);
userList = userMapper.getUserList();
//查看数据
logger.info("当前记录一共有: "+objects.getTotal()+"条");
logger.info("每页显示: "+ objects.getPageSize()+"行");
logger.info("一共有: "+objects.getPages()+"页");
logger.info("查询第: "+objects.getPageNum()+"页,结果如下: ");
if(userList.size()>0){
userList.forEach(System.out::println);
}
}catch (Exception e){
e.printStackTrace();
}finally {
MybatisUtil.closeSqlSession(sqlSession);
}
}