Browse Source

扩展一下

miemie 4 năm trước cách đây
mục cha
commit
fe794916c4

+ 5 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractLambdaWrapper.java

@@ -59,7 +59,8 @@ public abstract class AbstractLambdaWrapper<T, Children extends AbstractLambdaWr
     }
 
     protected String columnToString(SFunction<T, ?> column, boolean onlyColumn) {
-        return getColumn(LambdaUtils.resolve(column), onlyColumn);
+        ColumnCache cache = getColumnCache(column);
+        return onlyColumn ? cache.getColumn() : cache.getColumnSelect();
     }
 
     /**
@@ -67,19 +68,17 @@ public abstract class AbstractLambdaWrapper<T, Children extends AbstractLambdaWr
      * <p>
      * 如果获取不到列信息,那么本次条件组装将会失败
      *
-     * @param lambda     lambda 表达式
-     * @param onlyColumn 如果是,结果: "name", 如果否: "name" as "name"
      * @return 列
      * @throws com.baomidou.mybatisplus.core.exceptions.MybatisPlusException 获取不到列信息时抛出异常
      * @see SerializedLambda#getImplClass()
      * @see SerializedLambda#getImplMethodName()
      */
-    private String getColumn(SerializedLambda lambda, boolean onlyColumn) {
+    protected ColumnCache getColumnCache(SFunction<T, ?> column) {
+        SerializedLambda lambda = LambdaUtils.resolve(column);
         Class<?> aClass = lambda.getInstantiatedType();
         tryInitCache(aClass);
         String fieldName = PropertyNamer.methodToProperty(lambda.getImplMethodName());
-        ColumnCache columnCache = getColumnCache(fieldName, aClass);
-        return onlyColumn ? columnCache.getColumn() : columnCache.getColumnSelect();
+        return getColumnCache(fieldName, aClass);
     }
 
     private void tryInitCache(Class<?> lambdaClass) {

+ 1 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/update/LambdaUpdateWrapper.java

@@ -21,6 +21,7 @@ import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.Constants;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
 import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 
 import java.util.ArrayList;

+ 7 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -59,6 +59,10 @@ public class TableFieldInfo implements Constants {
      * 属性表达式#{property}, 可以指定jdbcType, typeHandler等
      */
     private final String el;
+    /**
+     * jdbcType, typeHandler等部分
+     */
+    private final String mapping;
     /**
      * 属性类型
      */
@@ -206,6 +210,8 @@ public class TableFieldInfo implements Constants {
             el += (COMMA + SqlScriptUtils.mappingNumericScale(Integer.valueOf(numericScale)));
         }
         this.el = el;
+        int index = el.indexOf(COMMA);
+        this.mapping = index > 0 ? el.substring(++index) : null;
         this.initLogicDelete(dbConfig, field, existTableLogic);
 
         String column = tableField.value();
@@ -271,6 +277,7 @@ public class TableFieldInfo implements Constants {
         this.isPrimitive = this.propertyType.isPrimitive();
         this.isCharSequence = StringUtils.isCharSequence(this.propertyType);
         this.el = this.property;
+        this.mapping = null;
         this.insertStrategy = dbConfig.getInsertStrategy();
         this.updateStrategy = dbConfig.getUpdateStrategy();
         this.whereStrategy = dbConfig.getSelectStrategy();

+ 7 - 7
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java

@@ -59,12 +59,12 @@ public final class LambdaUtils {
         Class<?> clazz = func.getClass();
         String name = clazz.getName();
         return Optional.ofNullable(FUNC_CACHE.get(name))
-                .map(WeakReference::get)
-                .orElseGet(() -> {
-                    SerializedLambda lambda = SerializedLambda.resolve(func, clazz.getClassLoader());
-                    FUNC_CACHE.put(name, new WeakReference<>(lambda));
-                    return lambda;
-                });
+            .map(WeakReference::get)
+            .orElseGet(() -> {
+                SerializedLambda lambda = SerializedLambda.resolve(func, clazz.getClassLoader());
+                FUNC_CACHE.put(name, new WeakReference<>(lambda));
+                return lambda;
+            });
     }
 
     /**
@@ -107,7 +107,7 @@ public final class LambdaUtils {
         }
 
         info.getFieldList().forEach(i ->
-                map.put(formatKey(i.getProperty()), new ColumnCache(i.getColumn(), i.getSqlSelect()))
+            map.put(formatKey(i.getProperty()), new ColumnCache(i.getColumn(), i.getSqlSelect(), i.getMapping()))
         );
         return map;
     }

+ 9 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/support/ColumnCache.java

@@ -38,4 +38,13 @@ public class ColumnCache implements Serializable {
      * 查询 column
      */
     private String columnSelect;
+    /**
+     * mapping
+     */
+    private String mapping;
+
+    public ColumnCache(String column, String columnSelect) {
+        this.column = column;
+        this.columnSelect = columnSelect;
+    }
 }