添加依赖
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
封装分页实体类
package com.pactera.pagehelper.common;
import com.github.pagehelper.PageInfo;
import java.util.List;
public class CommonPage<T> {
private Integer pageNum;
private Integer pageSize;
private Integer totalPage;
private Long total;
private List<T> list;
/**
* 将PageHelper分页后的list转为分页信息
*/
public static <T> CommonPage<T> restPage(List<T> list) {
CommonPage<T> result = new CommonPage<T>();
PageInfo<T> pageInfo = new PageInfo<T>(list);
result.setTotalPage(pageInfo.getPages());
result.setPageNum(pageInfo.getPageNum());
result.setPageSize(pageInfo.getPageSize());
result.setTotal(pageInfo.getTotal());
result.setList(pageInfo.getList());
return result;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
封装响应实体
package com.pactera.pagehelper.common;
public class ResultBean {
private String resCode;
private String resBean;
private Object data;
public ResultBean(String resCode, String resBean, Object data) {
this.resCode = resCode;
this.resBean = resBean;
this.data = data;
}
public ResultBean(String resCode, String resBean) {
this.resCode = resCode;
this.resBean = resBean;
}
public String getResCode() {
return resCode;
}
public void setResCode(String resCode) {
this.resCode = resCode;
}
public String getResBean() {
return resBean;
}
public void setResBean(String resBean) {
this.resBean = resBean;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
@Override
public String toString() {
return "ResultBean{" +
"resCode='" + resCode + '\'' +
", resBean='" + resBean + '\'' +
", data=" + data +
'}';
}
}
controller
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = "/list")
public ResultBean list(@RequestParam(value = "name",required = false) String name,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
List<User> userList = userService.list(name,pageSize,pageNum);
return new ResultBean("2000","成功", CommonPage.restPage(userList));
}
}
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public List<User> list(String name, Integer pageSize, Integer pageNum) {
PageHelper.startPage(pageNum,pageSize);
return userDao.selectUserListAndLikeName(name);
}
}