一、导入依赖
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.5</version>
</dependency>
二、在applicationContext.xml中配置
主要是引用文件mybatis-config.xml
<!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 扫描model包 -->
<property name="typeAliasesPackage" value="com.chuji.model"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 配置分页插件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
三、新建一个mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
四、在controller里面使用
/**
* 获取分页数据
* @param userVO
* @return
*/
@RequestMapping("/getUserList")
public @ResponseBody
CommonResponse<DataGrid<UserPO>> getUserList(@RequestBody UserVO userVO){
// 分页数据
Page<UserPO> page = PageHelper.startPage(userVO.getPageNum(), userVO.getPageSize(), true);
// 列表
List<UserPO> userPOList = userPOMapper.getUserList();
DataGrid<UserPO> purchaseOrderVOData = DataGridUtils.buildDataGrid(page, userPOList);
return new CommonResponse<DataGrid<UserPO>>(ResponseTypeEnums.SUCCESS, null, null, purchaseOrderVOData);
}
五、其中有自己定义的返回类
DataGrid
package com.chuji.util.DataGrid;
import java.util.List;
public class DataGrid<T> {
private Integer pageSize;// 每页行数
private long total;// 总行数
private List<T> data;
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}
好了这样就大功告成了!!!