Quellcode durchsuchen

添加测试用例

Caratacus vor 7 Jahren
Ursprung
Commit
88dd697f56

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

@@ -138,8 +138,8 @@ public class EntityWrapperTest {
 
     @Test
     public void testNoTSQL1() {
-		/*
-		 * 非 T-SQL 无实体查询
+        /*
+         * 非 T-SQL 无实体查询
 		 */
         ew.addFilter("name={0}", "'123'").addFilterIfNeed(false, "status=?", "1");
         String sqlSegment = ew.originalSql();
@@ -149,8 +149,8 @@ public class EntityWrapperTest {
 
     @Test
     public void testTSQL11() {
-		/*
-		 * 实体带查询使用方法 输出看结果
+        /*
+         * 实体带查询使用方法 输出看结果
 		 */
         ew.setEntity(new User(1));
         ew.where("name=?", "'zhangsan'").and("id=1").orNew("status=?", "0").or("status=1").notLike("nlike", "notvalue")
@@ -325,8 +325,8 @@ public class EntityWrapperTest {
      */
     @Test
     public void testIsWhere() {
-		/*
-		 * 实体带where ifneed
+        /*
+         * 实体带where ifneed
 		 */
         ew.setEntity(new User(1));
         ew.setParamAlias("ceshi");
@@ -408,4 +408,26 @@ public class EntityWrapperTest {
         Assert.assertEquals("ORDER BY id desc", wrapper.getSqlSegment());
     }
 
+    /**
+     * 测试 Condition orderBy
+     */
+    @Test
+    public void testConditionOrderBys() {
+        //空集合测试
+        List<String> orders = null;
+        Wrapper wrapper = Condition.create();
+        wrapper.orderAsc(orders);
+        Assert.assertNull(wrapper.getSqlSegment());
+        orders = new ArrayList<>(3);
+        wrapper.orderAsc(orders);
+        Assert.assertNull(wrapper.getSqlSegment());
+        orders.add("id1");
+        orders.add("id2");
+        orders.add("id3");
+        wrapper.orderAsc(orders);
+        Assert.assertEquals("ORDER BY id1 ASC, id2 ASC, id3 ASC", wrapper.getSqlSegment());
+        wrapper.orderDesc(orders);
+        Assert.assertEquals("ORDER BY id1 ASC, id2 ASC, id3 ASC, id1 DESC, id2 DESC, id3 DESC", wrapper.getSqlSegment());
+    }
+
 }

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

@@ -754,7 +754,7 @@ public class AutoSqlInjector implements ISqlInjector {
      * @return
      */
     protected String convertIfTag(boolean ignored, TableFieldInfo fieldInfo, String prefix, boolean close) {
-		/* 忽略策略 */
+        /* 忽略策略 */
         FieldStrategy fieldStrategy = fieldInfo.getFieldStrategy();
         if (fieldStrategy == FieldStrategy.IGNORED) {
             if (ignored) {

+ 6 - 7
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/mapper/Wrapper.java

@@ -20,7 +20,6 @@ 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;
 
@@ -866,7 +865,7 @@ public abstract class Wrapper<T> implements Serializable {
      * @param columns 需要排序的集合
      * @return this
      */
-    public Wrapper<T> orderDesc(List<String> columns) {
+    public Wrapper<T> orderDesc(Collection<String> columns) {
         return orderBy(true, columns, false);
     }
 
@@ -1194,7 +1193,7 @@ public abstract class Wrapper<T> implements Serializable {
      *
      * @param condition 拼接的前置条件
      * @param column    字段名称
-     * @param value     匹配值 List集合
+     * @param value     匹配值 集合
      * @return this
      */
     public Wrapper<T> in(boolean condition, String column, Collection<?> value) {
@@ -1210,7 +1209,7 @@ public abstract class Wrapper<T> implements Serializable {
      * </p>
      *
      * @param column 字段名称
-     * @param value  匹配值 List集合
+     * @param value  匹配值 集合
      * @return this
      */
     public Wrapper<T> in(String column, Collection<?> value) {
@@ -1224,7 +1223,7 @@ public abstract class Wrapper<T> implements Serializable {
      *
      * @param condition 拼接的前置条件
      * @param column    字段名称
-     * @param value     匹配值 List集合
+     * @param value     匹配值 集合
      * @return this
      */
     public Wrapper<T> notIn(boolean condition, String column, Collection<?> value) {
@@ -1240,7 +1239,7 @@ public abstract class Wrapper<T> implements Serializable {
      * </p>
      *
      * @param column 字段名称
-     * @param value  匹配值 List集合
+     * @param value  匹配值 集合
      * @return this
      */
     public Wrapper<T> notIn(String column, Collection<?> value) {
@@ -1313,7 +1312,7 @@ public abstract class Wrapper<T> implements Serializable {
      * </p>
      *
      * @param column 字段名称
-     * @param value  集合List
+     * @param value  集合
      * @param isNot  是否为NOT IN操作
      */
     private String inExpression(String column, Collection<?> value, boolean isNot) {

+ 18 - 12
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/toolkit/SqlUtils.java

@@ -85,12 +85,13 @@ public class SqlUtils {
     public static String concatOrderBy(String originalSql, Pagination page, boolean orderBy) {
         if (orderBy && page.isOpenSort()) {
             StringBuilder buildSql = new StringBuilder(originalSql);
-            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);
+            String ascStr = concatOrderBuilder(page.getAsc(), " ASC");
+            String descStr = concatOrderBuilder(page.getDesc(), " DESC");
+            if (StringUtils.isNotEmpty(ascStr) && StringUtils.isNotEmpty(descStr)) {
+                ascStr += ", ";
+            }
+            if (StringUtils.isNotEmpty(ascStr) || StringUtils.isNotEmpty(descStr)) {
+                buildSql.append(" ORDER BY ").append(ascStr).append(descStr);
             }
             return buildSql.toString();
         }
@@ -101,18 +102,23 @@ public class SqlUtils {
      * 拼接多个排序方法
      *
      * @param columns
-     * @param orderBuilder
      * @param orderWord
      */
-    private static void concatOrderBuilder(List<String> columns, StringBuilder orderBuilder, String orderWord) {
+    private static String concatOrderBuilder(List<String> columns, String orderWord) {
         if (CollectionUtils.isNotEmpty(columns)) {
-            for (String str : columns) {
-                if (StringUtils.isNotEmpty(str)) {
-                    orderBuilder.append(str).append(orderWord);
+            StringBuilder builder = new StringBuilder(16);
+            for (int i = 0; i < columns.size(); ) {
+                String cs = columns.get(i);
+                if (StringUtils.isNotEmpty(cs)) {
+                    builder.append(cs).append(orderWord);
+                }
+                if (++i != columns.size()) {
+                    builder.append(", ");
                 }
-
             }
+            return builder.toString();
         }
+        return StringUtils.EMPTY;
     }
 
     /**