大致结构如下:
一、定义返回数据结构
创建返回数据结构主要是为了统一方便,和分页查询的关系并不大,也可以忽略这一步。
package boc.ljh.config;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("返回数据结构")
public class Result<T> {
@ApiModelProperty("返回状态")
private Integer status;
@ApiModelProperty("返回信息")
private String message;
@ApiModelProperty("返回数据")
private T date;
public String getMessage() {
return message;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public void setMessage(String message) {
this.message = message;
}
public T getDate() {
return date;
}
public void setDate(T date) {
this.date = date;
}
}
二、封装分页查询类
package boc.ljh.config;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
@ApiModel("分页与查询")
public class PaginationHelper<T,O> {
@ApiModelProperty("开始记录索引")
private int start;
@ApiModelProperty("当前页码")
private int pageNum;
@ApiModelProperty("每页条数")
private int pageSize;
@ApiModelProperty("数据总条数")
private int totalSize;
@ApiModelProperty("总页数")
private int totalPages;
@ApiModelProperty("查询参数")
private O options;
@ApiModelProperty("返回的数据")
private List<T> data;
public int getStart() {
if(pageNum==0){
pageNum=1;
}
start = this.getPageSize() * (pageNum-1);
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public void setStart(int start) {
this.start = start;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public O getOptions() {
return options;
}
public void setOptions(O options) {
this.options = options;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}
三、dao、service、controller层
dao:
package boc.ljh.dao;
import boc.ljh.config.PaginationHelper;
import boc.ljh.pojo.User;
import javax.jws.soap.SOAPBinding;
import java.util.List;
public interface UserDao {
List<User> loadAllUserInfo(PaginationHelper paginationHelper);
Integer loadAllUserCount();
}
service:
package boc.ljh.service;
import boc.ljh.config.PaginationHelper;
import boc.ljh.pojo.User;
import org.apache.ibatis.annotations.Select;
import org.omg.CORBA.INTERNAL;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface UserService {
List<User> loadAllUserInfo(PaginationHelper paginationHelper);
Integer loadAllUserCount();
}
serviceImpl:
package boc.ljh.service;
import boc.ljh.config.PaginationHelper;
import boc.ljh.dao.UserDao;
import boc.ljh.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;
@Override
public List<User> loadAllUserInfo(PaginationHelper paginationHelper) {
return userDao.loadAllUserInfo(paginationHelper);
}
@Override
public Integer loadAllUserCount() {
return userDao.loadAllUserCount();
}
}
xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="boc.ljh.dao.UserDao">
<resultMap id="BaseResultMap" type="boc.ljh.pojo.User">
<id column="user_id" jdbcType="INTEGER" property="userId"/>
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
<result column="user_age" jdbcType="INTEGER" property="userAge"/>
</resultMap>
<select id="loadAllUserInfo" parameterType="boc.ljh.config.PaginationHelper" resultMap="BaseResultMap">
select * from user order by user_id ASC limit #{start},#{pageSize}
</select>
<select id="loadAllUserCount" resultType="java.lang.Integer">
select count(*) from user
</select>
</mapper>
controller:
package boc.ljh.controller;
import boc.ljh.config.AppCode;
import boc.ljh.config.PaginationHelper;
import boc.ljh.config.Result;
import boc.ljh.pojo.User;
import boc.ljh.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@ApiOperation("获取用户列表")
@PostMapping("loadAllUserList")
@ApiOperationSupport(ignoreParameters = {
"data",
"options",
"totalSize",
"totalPages",
"start"
})
public Result<PaginationHelper> loadAllUserInfo(@RequestBody PaginationHelper paginationHelper){
Result<PaginationHelper> result = new Result();
if(paginationHelper == null){
paginationHelper = new PaginationHelper<>();
}
if(paginationHelper.getOptions() == null){
paginationHelper.setOptions(new User());
}
paginationHelper.setData(userService.loadAllUserInfo(paginationHelper));
paginationHelper.setTotalSize(userService.loadAllUserCount());
result.setDate(paginationHelper);
result.setStatus(200);
result.setMessage("查询成功");
return result;
}
}
四、测试
重点
首先要知道sql中的limit不是直接分页查询的。比如limit 1,3,。并不是查询第一页每页三条数据的意思,而从第二条数据起查询三条数据,查询的是表中的2-5之间的数据。
select * from user limit 0,5。查询第0-5条之间的数据
select * from user limit 1,5。查询第1-6条之间的数据
select * from user limit 5,5。查询第6-11条之间的数据
可以看封装的分页查询类中有start(开始记录索引)、pageNum(当前页码)、pageSize(每页条数)这个三个参数。 sql需要写成下面这种形式传start(开始记录索引)、pageNum(当前页码)。而不是pageNum(当前页码)、pageSize(每页条数)。
select * from user limit #{start},#{pageSize}
sql不能改变就需要改变其他东西实现页面输入pageNum和pageSize参数实现分页查询功能。
改变封装的分页查询类中start参数的get方法,如下所示:
当页面输入pageNum=1,pageSize=10时,通过下面的改动,上面的sql就会变成 select * from user limit 0,10 当页面输入pageNum=2,pageSize=10时,通过下面的改动,上面的sql就会变成 select * from user limit 10,10 当页面输入pageNum=3,pageSize=10时,通过下面的改动,上面的sql就会变成 select * from user limit 20,10 这样就可以实现分页查询。
public int getStart() {
if(pageNum==0){
pageNum=1;
}
start = this.getPageSize() * (pageNum-1);
return start;
}
|