Procházet zdrojové kódy

DbConfig 加入新属性 propertyFormat,TableFieldInfo 移除属性 related

miemie před 5 roky
rodič
revize
39219c9a3b

+ 9 - 3
mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/TableField.java

@@ -114,7 +114,9 @@ public @interface TableField {
     boolean keepGlobalFormat() default false;
 
     /**
-     * JDBC类型 (该默认值不代表会按照该值生效)
+     * JDBC类型 (该默认值不代表会按照该值生效),
+     * 只生效与 mp 自动注入的 method,
+     * 建议配合 {@link TableName#autoResultMap()} 一起使用
      * <p>
      * {@link ResultMapping#jdbcType} and {@link ParameterMapping#jdbcType}
      *
@@ -123,7 +125,9 @@ public @interface TableField {
     JdbcType jdbcType() default JdbcType.UNDEFINED;
 
     /**
-     * 类型处理器 (该默认值不代表会按照该值生效)
+     * 类型处理器 (该默认值不代表会按照该值生效),
+     * 只生效与 mp 自动注入的 method,
+     * 建议配合 {@link TableName#autoResultMap()} 一起使用
      * <p>
      * {@link ResultMapping#typeHandler} and {@link ParameterMapping#typeHandler}
      *
@@ -132,7 +136,9 @@ public @interface TableField {
     Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
 
     /**
-     * 指定小数点后保留的位数
+     * 指定小数点后保留的位数,
+     * 只生效与 mp 自动注入的 method,
+     * 建议配合 {@link TableName#autoResultMap()} 一起使用
      * <p>
      * {@link ParameterMapping#numericScale}
      *

+ 5 - 3
mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/TableName.java

@@ -50,13 +50,15 @@ public @interface TableName {
     boolean keepGlobalPrefix() default false;
 
     /**
-     * 实体映射结果集
+     * 实体映射结果集,
+     * 只生效与 mp 自动注入的 method
      */
     String resultMap() default "";
 
     /**
-     * 是否自动构建 resultMap 并使用
-     * 如果设置 resultMap 则不会进行 resultMap 的自动构建并注入
+     * 是否自动构建 resultMap 并使用,
+     * 只生效与 mp 自动注入的 method,
+     * 如果设置 resultMap 则不会进行 resultMap 的自动构建并注入,
      * 只适合个别字段 设置了 typeHandler 或 jdbcType 的情况
      *
      * @since 3.1.2

+ 10 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/config/GlobalConfig.java

@@ -113,13 +113,22 @@ public class GlobalConfig implements Serializable {
          */
         private String schema;
         /**
-         * 字段 format
+         * db字段 format
          * <li> 例: `%s` </li>
          * <p> 对主键无效 </p>
          *
          * @since 3.1.1
          */
         private String columnFormat;
+        /**
+         * entity字段 format,
+         * 只有在 column as property 这种情况下生效
+         * <li> 例: `%s` </li>
+         * <p> 对主键无效 </p>
+         *
+         * @since 3.2.1
+         */
+        private String propertyFormat;
         /**
          * 表名、是否使用下划线命名(默认 true:默认数据库表下划线命名)
          */

+ 31 - 45
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -15,30 +15,20 @@
  */
 package com.baomidou.mybatisplus.core.metadata;
 
-import java.lang.reflect.Field;
-
-import org.apache.ibatis.mapping.ResultMapping;
-import org.apache.ibatis.type.JdbcType;
-import org.apache.ibatis.type.TypeHandler;
-import org.apache.ibatis.type.TypeHandlerRegistry;
-import org.apache.ibatis.type.UnknownTypeHandler;
-
-import com.baomidou.mybatisplus.annotation.FieldFill;
-import com.baomidou.mybatisplus.annotation.FieldStrategy;
-import com.baomidou.mybatisplus.annotation.SqlCondition;
-import com.baomidou.mybatisplus.annotation.TableField;
-import com.baomidou.mybatisplus.annotation.TableLogic;
-import com.baomidou.mybatisplus.annotation.Version;
+import com.baomidou.mybatisplus.annotation.*;
 import com.baomidou.mybatisplus.core.MybatisConfiguration;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.toolkit.Constants;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
+import lombok.*;
+import org.apache.ibatis.mapping.ResultMapping;
+import org.apache.ibatis.type.JdbcType;
+import org.apache.ibatis.type.TypeHandler;
+import org.apache.ibatis.type.TypeHandlerRegistry;
+import org.apache.ibatis.type.UnknownTypeHandler;
 
-import lombok.AccessLevel;
-import lombok.EqualsAndHashCode;
-import lombok.Getter;
-import lombok.ToString;
+import java.lang.reflect.Field;
 
 /**
  * 数据库表字段反射信息
@@ -51,11 +41,6 @@ import lombok.ToString;
 @EqualsAndHashCode
 public class TableFieldInfo implements Constants {
 
-    /**
-     * 是否有存在字段名与属性名关联
-     * <p>true: 表示要进行 as</p>
-     */
-    private final boolean related;
     /**
      * 字段名
      */
@@ -129,7 +114,7 @@ public class TableFieldInfo implements Constants {
     /**
      * 缓存 sql select
      */
-    @Getter(AccessLevel.NONE)
+    @Setter(AccessLevel.NONE)
     private String sqlSelect;
     /**
      * JDBC类型
@@ -189,9 +174,17 @@ public class TableFieldInfo implements Constants {
         if (StringUtils.isNotBlank(columnFormat) && tableField.keepGlobalFormat()) {
             column = String.format(columnFormat, column);
         }
-
         this.column = column;
-        this.related = TableInfoHelper.checkRelated(tableInfo.isUnderCamel(), this.property, this.column);
+        if (TableInfoHelper.checkRelated(tableInfo.isUnderCamel(), this.property, this.column)) {
+            this.sqlSelect = column;
+        } else {
+            String propertyFormat = dbConfig.getPropertyFormat();
+            String asProperty = this.property;
+            if (StringUtils.isNotBlank(propertyFormat)) {
+                asProperty = String.format(propertyFormat, this.property);
+            }
+            this.sqlSelect += (" AS " + asProperty);
+        }
 
         /*
          * 优先使用单个字段注解,否则使用全局配置
@@ -257,7 +250,16 @@ public class TableFieldInfo implements Constants {
         }
 
         this.column = column;
-        this.related = TableInfoHelper.checkRelated(tableInfo.isUnderCamel(), this.property, this.column);
+        if (TableInfoHelper.checkRelated(tableInfo.isUnderCamel(), this.property, this.column)) {
+            this.sqlSelect = column;
+        } else {
+            String propertyFormat = dbConfig.getPropertyFormat();
+            String asProperty = this.property;
+            if (StringUtils.isNotBlank(propertyFormat)) {
+                asProperty = String.format(propertyFormat, this.property);
+            }
+            this.sqlSelect += (" AS " + asProperty);
+        }
     }
 
     /**
@@ -299,22 +301,6 @@ public class TableFieldInfo implements Constants {
         return StringUtils.isNotBlank(logicDeleteValue);
     }
 
-    /**
-     * 获取 select sql 片段
-     *
-     * @return sql 片段
-     */
-    public String getSqlSelect() {
-        if (sqlSelect != null) {
-            return sqlSelect;
-        }
-        sqlSelect = column;
-        if (related) {
-            sqlSelect += (" AS " + property);
-        }
-        return sqlSelect;
-    }
-
     /**
      * 获取 insert 时候插入值 sql 脚本片段
      * <p>insert into table (字段) values (值)</p>
@@ -439,7 +425,7 @@ public class TableFieldInfo implements Constants {
      */
     ResultMapping getResultMapping(final MybatisConfiguration configuration) {
         ResultMapping.Builder builder = new ResultMapping.Builder(configuration, property,
-                StringUtils.getTargetColumn(column), propertyType);
+            StringUtils.getTargetColumn(column), propertyType);
         TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry();
         if (jdbcType != null && jdbcType != JdbcType.UNDEFINED) {
             builder.jdbcType(jdbcType);
@@ -472,7 +458,7 @@ public class TableFieldInfo implements Constants {
         }
         if (fieldStrategy == FieldStrategy.NOT_EMPTY && isCharSequence) {
             return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and %s != ''", property, property),
-                    false);
+                false);
         }
         return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", property), false);
     }