赞
踩
实现两个通用的类,PageResult<T>用来封装查询结果,QueryObject用来为Mybatis(-plus)的SQL语句中LIMIT赋值,实现分页查询。
- @Data
- public class PageResult<T> {
- // 用户输入的两个参数
- private int currentPage; // 当前页码
- private int pageSize; // 每页显示的条数
- // 两条 SQL 语句执行的结果
- private int totalCount; // 总条数
- private List<T> data; // 当前页结果集数据
- // 三个程序计算的数据
- private int prevPage; // 上一页页码
- private int nextPage; // 下一页页码
- private int totalPage; // 总页数/末页页码
- // 分页数据通过下面构造器封装好
- public PageResult(int currentPage, int pageSize, int totalCount, List<T>
- data) {
- this.currentPage = currentPage;
- this.pageSize = pageSize;
- this.totalCount = totalCount;
- this.data = data;
- // 计算三个数据的算法
- this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize :
- totalCount / pageSize + 1;
- this.prevPage = currentPage - 1 >= 1 ? currentPage - 1 : 1;
- this.nextPage = currentPage + 1 <= this.totalPage ? currentPage + 1 :
- this.totalPage;
- }
- }

//当前的所有数据能按页面大小分几页,如果totalCount % pageSize能整除(如4 % 2)那就是分两页;不能整除那就加一再除,所以肯定有一种能整除
this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize :totalCount / pageSize + 1;
//前一页是否还存在
this.prevPage = currentPage - 1 >= 1 ? currentPage - 1 : 1;//后一页是否还存在
this.nextPage = currentPage + 1 <= this.totalPage ? currentPage + 1 :this.totalPage;
- @Data
- public class QueryObject {
- private int currentPage = 1; // 当前页码,要跳转到哪一页的页码
- private int pageSize = 3; // 每页显示条数
- private int start = 0; //SQL中LIMIT {start},{pageSize}
-
- public QueryObject(int currentPage,int pageSize){
- this.currentPage = currentPage;
- this.pageSize = pageSize;
- this.start = (this.currentPage - 1) * pageSize;
- }
-
- }
数据库中有四条测试数据:

用LIMIT来模拟一下:
LIMIT 0,2 ==>从第一页开始【start = (currentPage - 1) * pageSize,start = 0,currentPage =1】,每业数据SIZE大小为2

LIMIT 2,2 ==>跳转到第二页

1)UserDao.java
跟分页查询相关的参数主要是这两个@Param("start") int start,@Param("pageSize") int pageSize,前面的其他参数是通用查询条件,可以不加。
- @Mapper
- public interface UserDao {
-
- ......
-
- /**
- * 分页查询
- * @return
- */
- List<SysUser> getUserListByPage(@Param("userName")String userName,@Param("phone")String phone,@Param("email")String email, @Param("start") int start,@Param("pageSize") int pageSize);
- }
-
- ......
- }
2)UserMapper.xml
<where>就是其他参数的查询条件,LIMIT才是跟分页查询相关的,所以<where>部分跟DAO文件对应起来,可以一起加也可以一起不加。
- <?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="com.lhc.lhcprojectdemo.dao.UserDao">
-
- ......
-
- <select id="getUserListByPage" resultType="com.lhc.lhcprojectdemo.util.entity.user.SysUser">
- SELECT user_name,nick_name,phone,email,status,create_time,update_time
- FROM sys_user
- <where>
- <if test="userName != null and userName != ''">
- user_name like concat('%', #{userName}, '%')
- </if>
- <if test="phone != null and phone != ''">
- AND
- phone like concat('%', #{phone}, '%')
- </if>
- <if test="email != null and email != ''">
- AND
- email like concat('%', #{email}, '%')
- </if>
- </where>
- LIMIT #{start}, #{pageSize}
- </select>
-
- ......
- </mapper>

service类主要是接收control端传来的数据,调用model层去查询数据。
- @Service
- public class UserServiceImpl implements UserService {
- @Override
- public PageResult<SysUser> getUserListByPage(String userName, String phone, String email,QueryObject qo){
- // 调用 DAO 查询数据数量
- int totalCount = userDao.getUserCount();
- // 为了性能加入判断,若查询的数据数量为 0,说明没有数据,返回返回空集合
- if(totalCount == 0){
- return new PageResult(qo.getCurrentPage(), qo.getPageSize(),
- totalCount, Collections.emptyList());
- }
- // 执行到这里代表有数据,查询当前页的结果数据
- List<SysUser> users = userDao.getUserListByPage(userName,phone,email,qo.getStart(), qo.getPageSize());
- return new PageResult(qo.getCurrentPage(), qo.getPageSize(), totalCount, users);
- }
- }

control层简单实现一下,获取参数并处理。
- @GetMapping("/list")
- @ResponseBody
- @PreAuthorize("hasAnyAuthority('user:list')")
- public String userList( @RequestParam(value = "query",required = false)String query, @RequestParam("pagenum") int pagenum,@RequestParam("pagesize") int pagesize){
-
- QueryObject qo = new QueryObject(pagenum,pagesize);
-
- //其他请求参数处理,偷个懒一个query解决
- String usernmae = "";
- String phone = "";
- String email = "";
-
- ......
-
- // 调用业务层方法来处理请求查询某一页数据
- PageResult<SysUser> pageResult = userService.getUserListByPage(usernmae,phone,email,qo);
-
- Map<String, Object> map = new HashMap<>(2);
- map.put("data",pageResult);
- map.put("meta", ResultUtil.ok().message("获取成功").status(200));
- return JSON.toJSONString(map);
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。