Procházet zdrojové kódy

修复Lambda首位属性为基类属性时错误.

nieqiurong před 7 roky
rodič
revize
740173da4a

+ 3 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractLambdaWrapper.java

@@ -47,13 +47,14 @@ public abstract class AbstractLambdaWrapper<T, This extends AbstractLambdaWrappe
     }
 
     private String getColumn(SerializedLambda lambda) {
-        if (!initColumnMap) {
+        String fieldName = StringUtils.resolveFieldName(lambda.getImplMethodName());
+        if (!initColumnMap || columnMap.get(fieldName) == null) {
             String entityClassName = lambda.getImplClass().replace("/", ".");
             columnMap = LambdaUtils.getColumnMap(entityClassName);
             Assert.notEmpty(columnMap, "该模式不能应用于非 baseMapper 的泛型 entity 之外的 entity!");
             initColumnMap = true;
         }
-        return Optional.ofNullable(columnMap.get(StringUtils.resolveFieldName(lambda.getImplMethodName())))
+        return Optional.ofNullable(columnMap.get(fieldName))
             .orElseThrow(() -> ExceptionUtils.mpe("该模式不能应用于非数据库字段!"));
     }
 }

+ 11 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -17,7 +17,6 @@ package com.baomidou.mybatisplus.core.metadata;
 
 import java.lang.reflect.Field;
 
-import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.annotation.FieldFill;
 import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.annotation.SqlCondition;
@@ -25,6 +24,8 @@ import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableLogic;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import lombok.Getter;
+import lombok.Setter;
 
 /**
  * <p>
@@ -93,13 +94,18 @@ public class TableFieldInfo {
      */
     private FieldFill fieldFill = FieldFill.DEFAULT;
 
+
+    @Getter
+    @Setter
+    private Class<?> parentClass;
+
     /**
      * <p>
      * 存在 TableField 注解构造函数
      * </p>
      */
     public TableFieldInfo(boolean underCamel, GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
-                          String column, String el, Field field, TableField tableField) {
+                          String column, String el, Field field, TableField tableField,Class<?> parentClass) {
         this.property = field.getName();
         this.propertyType = field.getType();
         /*
@@ -142,9 +148,10 @@ public class TableFieldInfo {
          * 保存当前字段的填充策略
          */
         this.fieldFill = tableField.fill();
+        this.parentClass = parentClass;
     }
 
-    public TableFieldInfo(boolean underCamel, GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field) {
+    public TableFieldInfo(boolean underCamel, GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field,Class<?> parentClass) {
         if (dbConfig.isColumnUnderline()) {
             /* 开启字段下划线申明 */
             this.setColumn(dbConfig, StringUtils.camelToUnderline(field.getName()));
@@ -160,6 +167,7 @@ public class TableFieldInfo {
         this.fieldStrategy = dbConfig.getFieldStrategy();
         this.propertyType = field.getType();
         this.setCondition(dbConfig);
+        this.parentClass = parentClass;
         tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
     }
 

+ 6 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -17,6 +17,8 @@ package com.baomidou.mybatisplus.core.metadata;
 
 import java.util.List;
 
+import lombok.Getter;
+import lombok.Setter;
 import org.apache.ibatis.session.Configuration;
 
 import com.baomidou.mybatisplus.annotation.IdType;
@@ -90,6 +92,10 @@ public class TableInfo {
      */
     private boolean logicDelete = false;
 
+    @Getter
+    @Setter
+    private Class<?> parentClass;
+
     /**
      * <p>
      * 获得注入的 SQL Statement

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

@@ -65,11 +65,27 @@ public final class LambdaUtils {
      * 缓存实体类名与表字段映射关系
      * </p>
      *
-     * @param className 实体类名
+     * @param clazz 实体
      * @param tableInfo 表信息
      */
-    public static void createCache(String className, TableInfo tableInfo) {
-        LAMBDA_CACHE.put(className, createLambdaMap(tableInfo));
+    public static void createCache(Class clazz, TableInfo tableInfo) {
+        LAMBDA_CACHE.put(clazz.getName(), createLambdaMap(tableInfo,clazz));
+
+    }
+
+    /**
+     * 保存缓存信息
+     * @param className 类名
+     * @param property 属性
+     * @param column 字段
+     */
+    private static void saveCache(String className,String property,String column){
+        Map<String, String> cacheMap = LAMBDA_CACHE.get(className);
+        if(cacheMap==null){
+            cacheMap = new HashMap<>();
+        }
+        cacheMap.put(property,column);
+        LAMBDA_CACHE.put(className,cacheMap);
     }
 
     /**
@@ -80,12 +96,20 @@ public final class LambdaUtils {
      * @param tableInfo 表信息
      * @return
      */
-    private static Map<String, String> createLambdaMap(TableInfo tableInfo) {
+    private static Map<String, String> createLambdaMap(TableInfo tableInfo,Class clazz) {
         Map<String, String> map = new HashMap<>(16);
         if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
+            if(tableInfo.getParentClass()!=clazz){
+                saveCache(tableInfo.getParentClass().getName(),tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
+            }
             map.put(tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
         }
-        tableInfo.getFieldList().forEach(i -> map.put(i.getProperty(), i.getColumn()));
+        tableInfo.getFieldList().forEach(i -> {
+            if(i.getParentClass()!=clazz){
+                saveCache(i.getParentClass().getName(),i.getProperty(), i.getColumn());
+            }
+            map.put(i.getProperty(), i.getColumn());
+        });
         return map;
     }
 

+ 4 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/TableInfoHelper.java

@@ -15,7 +15,6 @@
  */
 package com.baomidou.mybatisplus.core.toolkit;
 
-import static java.util.stream.Collectors.joining;
 import static java.util.stream.Collectors.toList;
 
 import java.lang.reflect.Field;
@@ -236,7 +235,7 @@ public class TableInfoHelper {
             /*
              * 字段, 使用 camelToUnderline 转换驼峰写法为下划线分割法, 如果已指定 TableField , 便不会执行这里
              */
-            fieldList.add(new TableFieldInfo(underCamel, dbConfig, tableInfo, field));
+            fieldList.add(new TableFieldInfo(underCamel, dbConfig, tableInfo, field, field.getDeclaringClass()));
         }
 
         /* 字段列表 */
@@ -256,7 +255,7 @@ public class TableInfoHelper {
         /*
          * 缓存 Lambda 映射关系
          */
-        LambdaUtils.createCache(clazz.getName(), tableInfo);
+        LambdaUtils.createCache(clazz, tableInfo);
         return tableInfo;
     }
 
@@ -325,6 +324,7 @@ public class TableInfoHelper {
                         column = column.toUpperCase();
                     }
                 }
+                tableInfo.setParentClass(field.getDeclaringClass());
                 tableInfo.setKeyColumn(column);
                 tableInfo.setKeyProperty(field.getName());
                 return true;
@@ -409,7 +409,7 @@ public class TableInfoHelper {
         if (columns.length == els.length) {
             for (int i = 0; i < columns.length; i++) {
                 fieldList.add(new TableFieldInfo(underCamel, dbConfig, tableInfo,
-                    columns[i], els[i], field, tableField));
+                    columns[i], els[i], field, tableField,field.getDeclaringClass()));
             }
             return true;
         }

+ 14 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserTest.java

@@ -168,6 +168,14 @@ public class H2UserTest extends BaseTest {
             Assert.assertNotNull(u.getName());
             Assert.assertNull(u.getPrice());
         }
+        ew = new QueryWrapper<>(null,"test_id","name","age","price");
+        ew.excludeColumns(H2User.class, "age", "price", null);
+        list = userService.list(ew);
+        for (H2User u : list) {
+            Assert.assertNotNull(u.getTestId());
+            Assert.assertNotNull(u.getName());
+            Assert.assertNull(u.getPrice());
+        }
         Wrapper<H2User> wrapper = new QueryWrapper<H2User>().lambda().select().excludeColumns(H2User.class,H2User::getAge,H2User::getPrice);
         List<H2User> list2  = userService.list(wrapper);
         for (H2User u : list2) {
@@ -175,5 +183,11 @@ public class H2UserTest extends BaseTest {
             Assert.assertNotNull(u.getName());
             Assert.assertNull(u.getPrice());
         }
+        wrapper = new QueryWrapper<H2User>().lambda().select(H2User::getTestId,H2User::getName,H2User::getTestDate,H2User::getPrice,H2User::getAge).excludeColumns(H2User.class,H2User::getAge,H2User::getTestDate,H2User::getVersion,H2User::getPrice);
+        list2 = userService.list(wrapper);
+        for (H2User u : list2) {
+            Assert.assertNotNull(u.getName());
+            Assert.assertNull(u.getPrice());
+        }
     }
 }