SpringBoot+MyBatis+Mybatis分页插件PageHelper实现分页查询
1.引入依赖
SpringBoot整合Mybatis分页插件PageHelper是很方便的:
只需要在pom.xml文件中引入pagehelper依赖包就可以了
<!-- 引入Mybatis 分页插件pageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
引入后down下来的依赖的jar包:
2.分页实现
在前一篇SpingBoot整合Mybatis开发的Demo的基础上实现一个分页功能:
根据 编码+名称 查询客户信息 查询结果以列表形式展示并支持分页
CustomerController.java中代码:
/**
* 功能描述:根据 编码+名称查询客户信息 查询结果以列表形式展示并支持分页
* postman接口测试:
* http://localhost:8080/api/selectCustomerListByCodeAndName?name=张三&code=zhangsan
* @param code
* @param name
* @param pageSize
* @param pageNum
* @return
*/
@RequestMapping(value="/api/selectCustomerListByCodeAndName")
public Object selectCustomerListByCodeAndName(@RequestParam(value="pageNum",defaultValue="1") int pageNum,
@RequestParam(value="pageSize",defaultValue="5") int pageSize,
String code,
String name){
PageInfo<Customer> pageInfo = customerService.selectCustomerListByCodeAndName(pageNum,pageSize,code,name);
return pageInfo;
}
CustomerService.java中代码:
/**
* 功能描述:根据 编码+名称查询客户信息 查询结果以列表形式展示并支持分页
* @param pageSize
* @param pageNum
* @param code
* @param name
* @return
*/
public PageInfo<Customer> selectCustomerListByCodeAndName(int pageNum, int pageSize, String code, String name);
CustomerServiceImpl.java中代码:
分页的处理是注解第3步来完成的,不是mapper.xml中实现的
/**
* 功能描述:根据 编码+名称查询客户信息 查询结果以列表形式展示并支持分页
*/
@Override
public PageInfo<Customer> selectCustomerListByCodeAndName(int pageNum, int pageSize, String code, String name) {
//1.startPage--start,设置当前页和每页显示条数
PageHelper.startPage(pageNum,pageSize);
//2.填充自己的sql查询逻辑
List<Customer> customerList = customerMapper.selectCustomerListByCodeAndName(code, name);
//3.pageHelper收尾,将返回结果信息进行分页处理
PageInfo<Customer> pageResult = new PageInfo<Customer>(customerList);
return pageResult;
}
CustomerMapper.java中代码:
/**
* 功能描述:根据 编码+名称查询客户信息 查询结果以列表形式展示并支持分页
* @param name
* @param code
* @return
*/
public List<Customer> selectCustomerListByCodeAndName(@Param(value="code") String code, @Param(value="name")String name);
customer.xml中增加的代码:
<resultMap id="BaseResultMap" type="com.springboot20.xdclass.springboot.demo2.domain.Customer">
<id column="id" property="id" />
<result column="code" property="code" />
<result column="name" property="name" />
<result column="reg_time" property="regTime" />
<result column="update_time" property="updateTime" />
</resultMap>
<select id="selectCustomerListByCodeAndName" resultMap="BaseResultMap">
select * from t_customer
<where>
<if test="code != null and code != ''">
and code like concat(concat('%'),#{code},'%')
</if>
<if test="name != null and name != ''">
and name like concat(concat('%'),#{name},'%')
</if>
</where>
order by id asc
</select>
3.postman接口测试:
返回结果:
{
"pageNum": 1,
"pageSize": 5,
"size": 5,
"startRow": 1,
"endRow": 5,
"total": 9,
"pages": 2,
"list": [
{
"id": 1,
"code": "zhangsan",
"name": "张三",
"regTime": "2019-08-13T05:15:04.000+0000",
"updateTime": "2019-08-13T05:15:04.000+0000"
},
{
"id": 2,
"code": "zhangsan",
"name": "张三",
"regTime": "2019-08-13T05:15:21.000+0000",
"updateTime": "2019-08-13T05:15:21.000+0000"
},
{
"id": 3,
"code": "zhangsan",
"name": "张三",
"regTime": "2019-08-13T05:15:22.000+0000",
"updateTime": "2019-08-13T05:15:22.000+0000"
},
{
"id": 4,
"code": "zhangsan",
"name": "张三",
"regTime": "2019-08-13T05:15:23.000+0000",
"updateTime": "2019-08-13T05:15:23.000+0000"
},
{
"id": 5,
"code": "zhangsan",
"name": "张三",
"regTime": "2019-08-13T05:15:24.000+0000",
"updateTime": "2019-08-13T05:15:24.000+0000"
}
],
"prePage": 0,
"nextPage": 2,
"isFirstPage": true,
"isLastPage": false,
"hasPreviousPage": false,
"hasNextPage": true,
"navigatePages": 8,
"navigatepageNums": [
1,
2
],
"navigateFirstPage": 1,
"navigateLastPage": 2,
"firstPage": 1,
"lastPage": 2
}