소스 검색

优化 orderBy 注解逻辑

hubin 4 년 전
부모
커밋
40bb594851

+ 12 - 17
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.core.injector;
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.Constants;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
@@ -237,26 +238,20 @@ public abstract class AbstractMethod implements Constants {
         }
     }
 
-    protected String sqlOrderBy(TableInfo tableInfo){
-        //如果不存在排序字段,直接返回空
-        if(!tableInfo.isExistOrderBy()){
+    protected String sqlOrderBy(TableInfo tableInfo) {
+        /* 不存在排序字段,直接返回空 */
+        List<TableFieldInfo> orderByFields = tableInfo.getOrderByFields();
+        if (CollectionUtils.isEmpty(orderByFields)) {
             return StringPool.EMPTY;
         }
-        List<TableFieldInfo> orderByFields = tableInfo.getOrderByFields();
         orderByFields.sort(Comparator.comparingInt(TableFieldInfo::getOrderBySort));
-        StringBuilder sql = new StringBuilder(NEWLINE + " order by ");
-        orderByFields.forEach(
-            tableFieldInfo -> {
-                sql.append(tableFieldInfo.getColumn());
-                sql.append(" ");
-                sql.append(tableFieldInfo.getOrderByType());
-                sql.append(",");
-            }
-        );
-        //删除最后一个
-        sql.deleteCharAt(sql.length()-1);
-        //当wrapper中传递了orderBy属性,@orderBy注解失效
-        return SqlScriptUtils.convertIf(sql.toString(),String.format("%s == null or %s == null or %s == null or %s.size() == 0",WRAPPER,WRAPPER_EXPRESSION,WRAPPER_EXPRESSION_ORDER,WRAPPER_EXPRESSION_ORDER), true);
+        StringBuilder sql = new StringBuilder();
+        sql.append(NEWLINE).append(" ORDER BY ");
+        sql.append(orderByFields.stream().map(tfi -> String.format("%s %s", tfi.getColumn(),
+            tfi.getOrderByType())).collect(joining(",")));
+        /* 当wrapper中传递了orderBy属性,@orderBy注解失效 */
+        return SqlScriptUtils.convertIf(sql.toString(), String.format("%s == null or %s == null or %s == null or %s.size() == 0",
+            WRAPPER, WRAPPER_EXPRESSION, WRAPPER_EXPRESSION_ORDER, WRAPPER_EXPRESSION_ORDER), true);
     }
 
     /**

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectList.java

@@ -33,7 +33,7 @@ public class SelectList extends AbstractMethod {
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_LIST;
         String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true), tableInfo.getTableName(),
-            sqlWhereEntityWrapper(true, tableInfo),sqlOrderBy(tableInfo),sqlComment());
+            sqlWhereEntityWrapper(true, tableInfo), sqlOrderBy(tableInfo), sqlComment());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
     }

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMapsPage.java

@@ -35,7 +35,7 @@ public class SelectMapsPage extends AbstractMethod {
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_MAPS_PAGE;
         String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
-            tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),sqlOrderBy(tableInfo), sqlComment());
+            tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlOrderBy(tableInfo), sqlComment());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatementForOther(mapperClass, getMethod(sqlMethod), sqlSource, Map.class);
     }

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectPage.java

@@ -33,7 +33,7 @@ public class SelectPage extends AbstractMethod {
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_PAGE;
         String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
-            tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),sqlOrderBy(tableInfo), sqlComment());
+            tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlOrderBy(tableInfo), sqlComment());
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
     }

+ 5 - 10
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -171,19 +171,12 @@ public class TableInfo implements Constants {
     @Setter(AccessLevel.NONE)
     private TableFieldInfo versionFieldInfo;
 
-    /**
-     * 是否存在排序
-     */
-    @Getter
-    @Setter
-    private boolean isExistOrderBy;
-
     /**
      * 排序列表
      */
     @Getter
     @Setter
-    public List<TableFieldInfo> orderByFields = new LinkedList<>();
+    public List<TableFieldInfo> orderByFields;
 
     public TableInfo(Class<?> entityType) {
         this.entityType = entityType;
@@ -459,8 +452,10 @@ public class TableInfo implements Constants {
             if (i.isWithUpdateFill()) {
                 this.withUpdateFill = true;
             }
-            if(i.isOrderBy()){
-                this.isExistOrderBy = true;
+            if (i.isOrderBy()) {
+                if (null == this.orderByFields) {
+                    this.orderByFields = new LinkedList<>();
+                }
                 this.orderByFields.add(i);
             }
             if (i.isVersion()) {