1.在pom.xml中添加如下:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
如果导入jar包遇到问题,参考:[https://blog.csdn.net/qq_33624284/article/details/72821811]
2.在spring-mybatis.xml中添加如下:
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/draeger/mapping/*.xml"></property>
<!--pageHelper-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
3.在controller中添加如下:
//分页查询
@RequestMapping("/pageList")
public PageInfo<DeviceFun> pageList(int pageNo,int pageSize,String status){
return applicationService.queryPageList(pageNo, pageSize, status);
}
4.在Service中添加如下:
//分页查询
public ArrayList<DeviceFun> queryPageList(int pageNo,int pageSize);
5.在ServiceImpl中添加如下:
@Override
public PageInfo<DeviceFun> queryPageList(Integer pageNo,Integer pageSize,String status){
//页码pageNo,每页显示记录数pageSize
pageNo = pageNo == null?1:pageNo;
pageSize = pageSize == null?10:pageSize;
//测试输出
System.out.println("pageNo:"+pageNo+";pageSize:"+pageSize);
//设置页码和每页显示记录数
PageHelper.startPage(pageNo, pageSize);
//创建List,调用查询接口
ArrayList<DeviceFun> deviceFun = new ArrayList<DeviceFun>();
deviceFun = applicationDao.pageList();//获取所有设备信息
//用PageInfo对结果进行包装
PageInfo<DeviceFun> page = new PageInfo<DeviceFun>(deviceFun);
return page;
}
6.在Dao中添加如下:
//分页查询
public ArrayList<DeviceFun> pageList();
7.在Mapper中添加如下:
<!-- 查询表单接口 -->
<select id="pageList" resultMap="com.×××项目名.model.DeviceFun">
select * from [DeviceFun]
</select>
建议:
(1)PageHelper5.x版本和4.×以及之前版本配置是有区别,例如:5.x版本在spring-mybatis.xml里面不需要配置数据库方言dialect。
(2)多表查询的分页,如果分页出现异常或重复数据,可能是SQL语句编写错误,建议先在数据库跑一遍sql语句,验证一下。
(3)如果分页异常,可以试着在sql语句追加:ORDER BY D_Id DESC。