소스 검색

分页排序优化

bin.xie 4 년 전
부모
커밋
ae27af2ee5

+ 6 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/PaginationInnerInterceptor.java

@@ -402,6 +402,9 @@ public class PaginationInnerInterceptor implements InnerInterceptor {
     }
 
     protected List<OrderByElement> addOrderByElements(List<OrderItem> orderList, List<OrderByElement> orderByElements) {
+        if (CollectionUtils.isEmpty(orderList)) {
+            return orderByElements;
+        }
         List<OrderByElement> additionalOrderBy = orderList.stream()
             .filter(item -> StringUtils.isNotBlank(item.getColumn()))
             .map(item -> {
@@ -414,8 +417,9 @@ public class PaginationInnerInterceptor implements InnerInterceptor {
         if (CollectionUtils.isEmpty(orderByElements)) {
             return additionalOrderBy;
         }
-        orderByElements.addAll(additionalOrderBy);
-        return orderByElements;
+        // 建议前端传了排序字段,优先使用前端的排序,比如:默认按id排序,前端传了name排序,建议先按name排序,再按id排序
+        additionalOrderBy.addAll(orderByElements);
+        return additionalOrderBy;
     }
 
     /**