瀏覽代碼

添加多个排序字段支持

Caratacus 7 年之前
父節點
當前提交
929505bd1d

+ 1 - 1
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/test/EntityWrapperTest.java

@@ -127,7 +127,7 @@ public class EntityWrapperTest {
     @Test
     public void testNoTSQL() {
         /*
-		 * 实体 filter orderby
+         * 实体 filter orderby
 		 */
         ew.setEntity(new User(1));
         ew.addFilter("name={0}", "'123'").orderBy("id,name");

+ 1 - 1
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -670,7 +670,7 @@ public class AutoSqlInjector implements ISqlInjector {
     protected String sqlSelectObjsColumns(TableInfo table) {
         StringBuilder columns = new StringBuilder();
         /*
-		 * 普通查询
+         * 普通查询
 		 */
         columns.append("<choose><when test=\"ew != null and ew.sqlSelect != null\">${ew.sqlSelect}</when><otherwise>");
         // 主键处理

+ 3 - 3
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/mapper/SqlHelper.java

@@ -30,7 +30,6 @@ import com.baomidou.mybatisplus.plugins.Page;
 import com.baomidou.mybatisplus.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.toolkit.MapUtils;
-import com.baomidou.mybatisplus.toolkit.StringUtils;
 import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
 
 /**
@@ -194,8 +193,9 @@ public class SqlHelper {
             wrapper = Condition.create();
         }
         // 排序
-        if (page.isOpenSort() && StringUtils.isNotEmpty(page.getOrderByField())) {
-            wrapper.orderBy(page.getOrderByField(), page.isAsc());
+        if (page.isOpenSort()) {
+            wrapper.orderAsc(page.getAsc());
+            wrapper.orderDesc(page.getDesc());
         }
         // MAP 参数查询
         if (MapUtils.isNotEmpty(page.getCondition())) {

+ 44 - 0
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/mapper/Wrapper.java

@@ -20,6 +20,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -813,6 +814,25 @@ public abstract class Wrapper<T> implements Serializable {
         return this;
     }
 
+    /**
+     * <p>
+     * SQL中orderby关键字跟的条件语句,可根据变更动态排序
+     * </p>
+     *
+     * @param condition 拼接的前置条件
+     * @param columns   SQL 中的 order by 语句,无需输入 Order By 关键字
+     * @param isAsc     是否为升序
+     * @return this
+     */
+    public Wrapper<T> orderBy(boolean condition, Collection<String> columns, boolean isAsc) {
+        if (condition && CollectionUtils.isNotEmpty(columns)) {
+            for (String column : columns) {
+                orderBy(condition, column, isAsc);
+            }
+        }
+        return this;
+    }
+
     /**
      * <p>
      * SQL中orderby关键字跟的条件语句,可根据变更动态排序
@@ -826,6 +846,30 @@ public abstract class Wrapper<T> implements Serializable {
         return orderBy(true, columns, isAsc);
     }
 
+    /**
+     * <p>
+     * 批量根据ASC排序
+     * </p>
+     *
+     * @param columns 需要排序的集合
+     * @return this
+     */
+    public Wrapper<T> orderAsc(Collection<String> columns) {
+        return orderBy(true, columns, true);
+    }
+
+    /**
+     * <p>
+     * 批量根据DESC排序
+     * </p>
+     *
+     * @param columns 需要排序的集合
+     * @return this
+     */
+    public Wrapper<T> orderDesc(List<String> columns) {
+        return orderBy(true, columns, false);
+    }
+
     /**
      * <p>
      * LIKE条件语句,value中无需前后%

+ 73 - 5
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/plugins/pagination/Pagination.java

@@ -16,6 +16,8 @@
 package com.baomidou.mybatisplus.plugins.pagination;
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.ibatis.session.RowBounds;
 
@@ -75,26 +77,46 @@ public class Pagination extends RowBounds implements Serializable {
     /**
      * 开启排序(默认 true) 只在代码逻辑判断 并不截取sql分析
      *
-     * @see com.baomidou.mybatisplus.mapper.SqlHelper fillWrapper
+     * @see com.baomidou.mybatisplus.mapper.SqlHelper#fillWrapper
      **/
     private transient boolean openSort = true;
 
+
     /**
      * <p>
-     * SQL 排序 ORDER BY 字段,例如: id DESC(根据id倒序查询)
+     * SQL 排序 ASC 集合
      * </p>
+     */
+    private transient List<String> asc;
+    /**
      * <p>
-     * DESC 表示按倒序排序(即:从大到小排序)<br>
-     * ASC 表示按正序排序(即:从小到大排序)
+     * SQL 排序 DESC 集合
      * </p>
      */
-    private transient String orderByField;
+    private transient List<String> desc;
 
     /**
      * 是否为升序 ASC( 默认: true )
+     *
+     * @see #asc
+     * @see #desc
      */
     private transient boolean isAsc = true;
 
+    /**
+     * <p>
+     * SQL 排序 ORDER BY 字段,例如: id DESC(根据id倒序查询)
+     * </p>
+     * <p>
+     * DESC 表示按倒序排序(即:从大到小排序)<br>
+     * ASC 表示按正序排序(即:从小到大排序)
+     *
+     * @see #asc
+     * @see #desc
+     * </p>
+     */
+    private transient String orderByField;
+
     public Pagination() {
         super();
     }
@@ -191,10 +213,19 @@ public class Pagination extends RowBounds implements Serializable {
         return this;
     }
 
+    /**
+     * @see #asc
+     * @see #desc
+     */
+    @Deprecated
     public String getOrderByField() {
         return orderByField;
     }
 
+    /**
+     * @see #asc
+     * @see #desc
+     */
     public Pagination setOrderByField(String orderByField) {
         if (StringUtils.isNotEmpty(orderByField)) {
             this.orderByField = orderByField;
@@ -211,10 +242,47 @@ public class Pagination extends RowBounds implements Serializable {
         return this;
     }
 
+    public List<String> getAsc() {
+        return orders(isAsc, asc);
+    }
+
+    private List<String> orders(boolean condition, List<String> columns) {
+        if (condition && StringUtils.isNotEmpty(orderByField)) {
+            if (columns == null) {
+                columns = new ArrayList<>();
+            }
+            if (!columns.contains(orderByField)) {
+                columns.add(orderByField);
+            }
+        }
+        return columns;
+    }
+
+    public void setAsc(List<String> asc) {
+        this.asc = asc;
+    }
+
+    public List<String> getDesc() {
+        return orders(!isAsc(), desc);
+    }
+
+    public void setDesc(List<String> desc) {
+        this.desc = desc;
+    }
+
+    /**
+     * @see #asc
+     * @see #desc
+     */
+    @Deprecated
     public boolean isAsc() {
         return isAsc;
     }
 
+    /**
+     * @see #asc
+     * @see #desc
+     */
     public Pagination setAsc(boolean isAsc) {
         this.isAsc = isAsc;
         return this;

+ 28 - 3
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/toolkit/SqlUtils.java

@@ -15,6 +15,8 @@
  */
 package com.baomidou.mybatisplus.toolkit;
 
+import java.util.List;
+
 import com.baomidou.mybatisplus.enums.SqlLike;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.plugins.pagination.Pagination;
@@ -81,15 +83,38 @@ public class SqlUtils {
      * @return
      */
     public static String concatOrderBy(String originalSql, Pagination page, boolean orderBy) {
-        if (orderBy && StringUtils.isNotEmpty(page.getOrderByField()) && page.isOpenSort()) {
+        if (orderBy && page.isOpenSort()) {
             StringBuilder buildSql = new StringBuilder(originalSql);
-            buildSql.append(" ORDER BY ").append(page.getOrderByField());
-            buildSql.append(page.isAsc() ? " ASC " : " DESC ");
+            StringBuilder orderBuilder = new StringBuilder();
+            concatOrderBuilder(page.getAsc(), orderBuilder, " ASC ");
+            concatOrderBuilder(page.getDesc(), orderBuilder, " DESC ");
+            String orderStr = orderBuilder.toString();
+            if (StringUtils.isNotEmpty(orderStr)) {
+                buildSql.append(" ORDER BY ").append(orderStr);
+            }
             return buildSql.toString();
         }
         return originalSql;
     }
 
+    /**
+     * 拼接多个排序方法
+     *
+     * @param columns
+     * @param orderBuilder
+     * @param orderWord
+     */
+    private static void concatOrderBuilder(List<String> columns, StringBuilder orderBuilder, String orderWord) {
+        if (CollectionUtils.isNotEmpty(columns)) {
+            for (String str : columns) {
+                if (StringUtils.isNotEmpty(str)) {
+                    orderBuilder.append(str).append(orderWord);
+                }
+
+            }
+        }
+    }
+
     /**
      * 格式sql
      *