JAVA基于内存进行数据结果分页

为什么要写这篇文章呢?

主要是因为使用通用广泛的开源框架,大多数是基于mybatis的pageHelper在数据库层面进行的分页。如果结果是通过代码进行组装出来的,那么就会失效。

所以花了几个小时时间做了这件事:通过AOP+注解的方式进行了对结果集处理的一些代码。

希望可以帮助更多人~~~~~~~~~~~~~~~~~

 

第一步:首先我们创建一个统一响应或者已经有统一

  1 /**
  2  * @deion : 统一响应信息主体
  3  * @author : 依亭赏雨
  4  * @Date : 2024/11/26
  5  */
  6 public class SystemResponse<T> implements Serializable {
  7 
  8     private static final long serialVersionUID = 1L;
  9 
 10     private static final String success = HttpStatus.SUCCESS;
 11 
 12     private static final String error = HttpStatus.ERROR;
 13 
 14     /**
 15      * 响应代码
 16      */
 17     private String code;
 18 
 19     /**
 20      * 响应消息
 21      */
 22     private String msg;
 23 
 24     /**
 25      * 响应结果
 26      */
 27     private T data;
 28 
 29     /**
 30      * 无参构造函数
 31      */
 32     public SystemResponse() {
 33 
 34     }
 35 
 36     /**
 37      * 构造方法
 38      */
 39     private SystemResponse(String code, String msg, T data) {
 40         this.code = code;
 41         this.msg = msg;
 42         this.data = data;
 43     }
 44 
 45     /**
 46      * 构造方法
 47      */
 48     private SystemResponse(String code, T data) {
 49         this.code = code;
 50         this.data = data;
 51     }
 52 
 53     /**
 54      * 构造方法
 55      */
 56     private SystemResponse(String code, String msg) {
 57         this.code = code;
 58         this.msg = msg;
 59     }
 60 
 61     /**
 62      * 构造方法
 63      */
 64     private SystemResponse(String code) {
 65         this.code = code;
 66     }
 67 
 68     /**
 69      * 请求成功
 70      *
 71      * @param msg  返回信息
 72      * @param data 泛型数据
 73      * @param <T>  返回数据,可以不填
 74      * @return 1.状态码(默认) 2.返回信息 3.泛型数据
 75      */
 76     public static <T> SystemResponse<T> createSuccess(String msg, T data) {
 77         return new SystemResponse<>(success, msg, data);
 78     }
 79 
 80     public static <T> SystemResponse<T> createSuccess(T data) {
 81         return new SystemResponse<>(success, "请求成功", data);
 82     }
 83 
 84     public static <T> SystemResponse<T> createSuccess(String msg) {
 85         return new SystemResponse<>(success, msg);
 86     }
 87 
 88     public static <T> SystemResponse<T> createSuccess() {
 89         return createSuccess("操作成功");
 90     }
 91 
 92     /**
 93      * 请求失败
 94      *
 95      * @param code
 96      * @param msg
 97      * @return 1.状态码(自定义) 2.返回信息(自定义)
 98      */
 99     public static <T> SystemResponse<T> createError(String code, String msg) {
100         return new SystemResponse<>(code, msg);
101     }
102 
103     public static <T> SystemResponse<T> createError() {
104         return new SystemResponse<>(error, "服务器异常");
105     }
106 
107     public static <T> SystemResponse<T> createError(String msg) {
108         return new SystemResponse<T>(error, msg);
109     }
110 
111     public String getCode() {
112         return code;
113     }
114 
115     public void setCode(String code) {
116         this.code = code;
117     }
118 
119     public String getMsg() {
120         return msg;
121     }
122 
123     public void setMsg(String msg) {
124         this.msg = msg;
125     }
126 
127     public T getData() {
128         return data;
129     }
130 
131     public void setData(T data) {
132         this.data = data;
133     }
134 
135 }
统一响应类

第二步:创建一个分页注解

 1 /**
 2  * @deion : 分页注解
 3  * @author : 依亭赏雨
 4  * @Date : 2024/11/26
 5  */
 6 @Retention(RetentionPolicy.RUNTIME)
 7 @Target(ElementType.METHOD)
 8 public @interface PageQuery {
 9 
10     /**
11      * 页码,默认为0
12      * @return
13      */
14     int page() default  0;
15 
16     /**
17      * 每页显示的数量,默认为10
18      * @return
19      */
20     int size() default  10;
21     
22 }
分页注解

第三步:创建AOP

 1 /**
 2  * @deion : AOP切面,用于处理分页注解
 3  * @author : 依亭赏雨
 4  * @Date : 2024/11/26
 5  */
 6 @Aspect
 7 @Component
 8 public class PaginationAspect {
 9 
10     /**
11      * 根据页码和每页大小获取分页数据
12      * 如果你已经从数据库查询了完整的结果集,可以通过 Java 代码来实现分页。比如说,如果你有一个 List<Result>,可以通过以下方式对结果集进行分页:
13      *
14      * @param list 原结果
15      * @param page 页数
16      * @param size 行数
17      * @return 分页后结果
18      */
19     private List<?> getPagedResults(List<?> list, int page, int size) {
20         int startIndex = (page - 1) * size;
21         int endIndex = Math.min(startIndex + size, list.size());
22         if (startIndex >= list.size() || startIndex < 0) {
23             return Collections.emptyList(); // 页码超出范围
24         }
25         return list.subList(startIndex, endIndex);
26     }
27 
28     @Pointcut("@annotation(love.ytsy.common.core.annotation.PageQuery)")
29     public void point() {
30     }
31 
32     @Around("@annotation(pageQuery)")
33     public Object aroundPageQuery(ProceedingJoinPoint joinPoint, PageQuery pageQuery) throws Throwable {
34         HttpServletRequest request = ServletUtils.getRequest();
35 
36         int page = pageQuery.page();//页码
37         int size = pageQuery.size();//行数
38         if (StringUtils.isNotNull(request.getParameter("page"))) page = Integer.parseInt(request.getParameter("page"));
39         if (StringUtils.isNotNull(request.getParameter("size"))) size = Integer.parseInt(request.getParameter("size"));
40 
41         Object proceed = joinPoint.proceed();
42 
43         if (proceed instanceof SystemResponse<?>) {
44             SystemResponse<?> response = (SystemResponse<?>) proceed;
45             Object responseData = response.getData();
46 
47             // 如果 data 是 List 类型
48             if (responseData instanceof List) {
49                 List<?> tempList = (List<?>) responseData;
50                 List<?> pagedResults = getPagedResults(tempList, page, size);
51                 // 确保分页结果与泛型 T 一致
52                 if (pagedResults instanceof List<?>) {
53                     // 强制转换为 SystemResponse<List<T>>,确保类型一致
54                     @SuppressWarnings("unchecked") SystemResponse<List<?>> typedResponse = (SystemResponse<List<?>>) response;
55                     typedResponse.setData(pagedResults);
56                 }
57             }
58         }
59         return proceed;
60     }
61 
62 }
分页切面类
/**
* 这里的响应体中的范型根据自身实际情况更改
*/

 

使用方法:

创建一个xxxController的请求类,创建一个可以请求的方法

/**
 * @deion : 账号请求类
 * @author : 依亭赏雨
 * @Date : 2024/11/26
 */
@RestController
@RequestMapping(value = "/acc/account")
public class AccAccountController extends BaseController {

    @Resource
    private AccAccountService accAccountService;

    @PageQuery
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public SystemResponse<?> test(@RequestBody AccAccountDomain accAccountDto) {
        List<AccAccountEntity> accAccountDtos = accAccountService.selectAccAccountList(accAccountDto);
        return success(accAccountDtos);
    }
}

@Service @Mapper 忽略自行补充

 

希望帮助到更多用得到的人

 

posted @ 2024-11-26 20:03  才知几何  阅读(200)  评论(0)    收藏  举报