未使用SpringBoot
第⼀步:引⼊依赖
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.3.1</version>
</dependency>
第⼆步:在mybatis-config.xml⽂件中配置插件
typeAliases标签下⾯进⾏配置:
<plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
第三步:编写Java代码
CarMapper接⼝
List<Car> selectAll();
CarMapper.xml
<select id="selectAll" resultType="Car">select * from t_car
</select>
关键点:
在查询语句之前开启分⻚功能。
在查询语句之后封装PageInfo对象。(PageInfo对象将来会存储到request域当中。在⻚⾯上展
示。)
@Test
public void testPageHelper() throws Exception{SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().b
uild(Resources.getResourceAsStream("mybatis-config.xml"));SqlSession sqlSession = sqlSessionFactory.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);// 开启分⻚PageHelper.startPage(2, 2);// 执⾏查询语句List<Car> cars = mapper.selectAll();// 获取分⻚信息对象PageInfo<Car> pageInfo = new PageInfo<>(cars, 5);System.out.println(pageInfo);
}
执⾏结果:
PageInfo{pageNum=2, pageSize=2, size=2, startRow=3, endRow=4, total=6, pages=3,
list=Page{count=true, pageNum=2, pageSize=2, startRow=2, endRow=4, total=6, pages=3,
reasonable=false, pageSizeZero=false}[Car{id=86, carNum='1234', brand='丰⽥霸道',
guidePrice=50.5, produceTime='2020-10-11', carType='燃油⻋'}, Car{id=87, carNum='1234',
brand='丰⽥霸道', guidePrice=50.5, produceTime='2020-10-11', carType='燃油⻋'}], prePage=1,
nextPage=3, isFirstPage=false, isLastPage=false, hasPreviousPage=true, hasNextPage=true,
navigatePages=5, navigateFirstPage=1, navigateLastPage=3, navigatepageNums=[1, 2, 3]}
SpringBoot中
PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数,我们设置每页展示5条数据。PageInfo用来封装页面信息,返回给前台界面。PageInfo中的一些我们需要用到的参数如下表:
PageInfo.list 结果集
PageInfo.pageNum 当前页码
PageInfo.pageSize 当前页面显示的数据条目
PageInfo.pages 总页数
PageInfo.total 数据的总条目数
PageInfo.prePage 上一页
PageInfo.nextPage 下一页
PageInfo.isFirstPage 是否为第一页
PageInfo.isLastPage 是否为最后一页
PageInfo.hasPreviousPage 是否有上一页
PageHelper.hasNextPage 是否有下一页
(1)引入pom.xml
<!-- 分页插件 -->
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.5</version>
</dependency>
(2)打开application.properties,添加如下几行配置信息
#分页插件
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
创建controller层
Mapper文件中, SQL不用增加 limit分页指令,需要什么直接查就可以,Pagehelper 可以自动实现分页!
package com.ithuang.demo.controller;import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.service.OrderInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;@RestController
public class OrderInfoController {@Resourceprivate OrderInfoService orderInfoService;@GetMapping("/getOrderInfoList")public List<OrderInfo> getOrderInfoList(@RequestParam(value = "pageNow",defaultValue = "1") int pageNow,@RequestParam(value = "pageSize",defaultValue = "3") int pageSize){return orderInfoService.getOrderInfoList(pageNow,pageSize);}
}
2. 创建Service层
package com.ithuang.demo.service;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ithuang.demo.bean.OrderInfo;
import com.ithuang.demo.mapper.OrderInfoMapper;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.List;@Service
public class OrderInfoService {@Resourceprivate OrderInfoMapper orderInfoMapper;public List<OrderInfo> getOrderInfoList(int pageNow, int pageSize) {PageHelper.startPage(pageNow,pageSize);List<OrderInfo> orderInfoList = orderInfoMapper.getOrderInfoList();PageInfo<OrderInfo> userPageInfo = new PageInfo<>(orderInfoList);return userPageInfo.getList();}
}
3. 创建Mapper层
package com.ithuang.demo.mapper;import com.ithuang.demo.bean.OrderInfo;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface OrderInfoMapper {List<OrderInfo> getOrderInfoList();
}
5. 编写xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ithuang.demo.mapper.OrderInfoMapper"><select id="getOrderInfoList" parameterType="int" resultType="com.ithuang.demo.bean.OrderInfo">SELECT * FROM order_info</select>
</mapper>