Browse Source

调整代码(目前只支持自动构建情况)

nieqiurong 1 year ago
parent
commit
aa664fd2e2

+ 0 - 9
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/IJsonTypeHandler.java

@@ -17,8 +17,6 @@ package com.baomidou.mybatisplus.core.handlers;
 
 import com.baomidou.mybatisplus.annotation.TableName;
 
-import java.lang.reflect.Field;
-
 /**
  * Json类型处理器接口(实现类确保为多例状态).
  *
@@ -28,13 +26,6 @@ import java.lang.reflect.Field;
  */
 public interface IJsonTypeHandler<T> {
 
-    /**
-     * 初始化处理器
-     *
-     * @param field 字段信息
-     */
-    void init(Field field);
-
     /**
      * 反序列化json
      *

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

@@ -563,9 +563,12 @@ public class TableFieldInfo implements Constants {
             TypeHandler<?> typeHandler = registry.getMappingTypeHandler(this.typeHandler);
             if (IJsonTypeHandler.class.isAssignableFrom(this.typeHandler)) {
                 // 保证每次实例化
-                typeHandler = registry.getInstance(this.propertyType, this.typeHandler);
-                IJsonTypeHandler<?> jsonTypeHandler = (IJsonTypeHandler<?>) typeHandler;
-                jsonTypeHandler.init(this.field);
+                try {
+                    //TODO 后面想一下怎么支持原生的方式,目前只支持自动生成的情况.
+                    typeHandler = this.typeHandler.getDeclaredConstructor(Class.class, Field.class).newInstance(this.propertyType, this.field);
+                } catch (ReflectiveOperationException e) {
+                    throw new RuntimeException(e);
+                }
             } else {
                 if (typeHandler == null) {
                     typeHandler = registry.getInstance(this.propertyType, this.typeHandler);

+ 32 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/AbstractJsonTypeHandler.java

@@ -16,9 +16,12 @@
 package com.baomidou.mybatisplus.extension.handlers;
 
 import com.baomidou.mybatisplus.core.handlers.IJsonTypeHandler;
+import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import org.apache.ibatis.type.BaseTypeHandler;
 import org.apache.ibatis.type.JdbcType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Type;
@@ -33,13 +36,36 @@ import java.sql.SQLException;
  */
 public abstract class AbstractJsonTypeHandler<T> extends BaseTypeHandler<T> implements IJsonTypeHandler<T> {
 
+    protected final Logger log = LoggerFactory.getLogger(this.getClass());
+
+    protected final Class<?> type;
+
     /**
      * @since 3.5.6
      */
     protected Type genericType;
 
-    @Override
-    public void init(Field field) {
+    /**
+     * 默认初始化
+     *
+     * @param type 类型
+     */
+    public AbstractJsonTypeHandler(Class<?> type) {
+        this.type = type;
+        if (log.isTraceEnabled()) {
+            log.trace(this.getClass().getSimpleName() + "(" + type + ")");
+        }
+        Assert.notNull(type, "Type argument cannot be null");
+    }
+
+    /**
+     * 通过字段初始化
+     *
+     * @param type  类型
+     * @param field 字段
+     */
+    public AbstractJsonTypeHandler(Class<?> type, Field field) {
+        this(type);
         this.genericType = field.getGenericType();
     }
 
@@ -66,4 +92,8 @@ public abstract class AbstractJsonTypeHandler<T> extends BaseTypeHandler<T> impl
         return StringUtils.isBlank(json) ? null : parse(json);
     }
 
+    public Type getFieldType() {
+        return this.genericType != null ? this.genericType : this.type;
+    }
+
 }

+ 7 - 8
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/Fastjson2TypeHandler.java

@@ -17,34 +17,33 @@ package com.baomidou.mybatisplus.extension.handlers;
 
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONWriter;
-import com.baomidou.mybatisplus.core.toolkit.Assert;
-import lombok.extern.slf4j.Slf4j;
 import org.apache.ibatis.type.JdbcType;
 import org.apache.ibatis.type.MappedJdbcTypes;
 import org.apache.ibatis.type.MappedTypes;
 
+import java.lang.reflect.Field;
+
 /**
  * Fastjson2 实现 JSON 字段类型处理器
  *
  * @author nieqiurong
  * @since 3.5.5
  */
-@Slf4j
 @MappedTypes({Object.class})
 @MappedJdbcTypes(JdbcType.VARCHAR)
 public class Fastjson2TypeHandler extends AbstractJsonTypeHandler<Object> {
 
     public Fastjson2TypeHandler(Class<?> type) {
-        if (log.isTraceEnabled()) {
-            log.trace("Fastjson2TypeHandler(" + type + ")");
-        }
-        Assert.notNull(type, "Type argument cannot be null");
+        super(type);
     }
 
+    public Fastjson2TypeHandler(Class<?> type, Field field) {
+        super(type, field);
+    }
 
     @Override
     public Object parse(String json) {
-        return JSON.parseObject(json, this.genericType);
+        return JSON.parseObject(json, this.getFieldType());
     }
 
     @Override

+ 8 - 5
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/FastjsonTypeHandler.java

@@ -17,33 +17,36 @@ package com.baomidou.mybatisplus.extension.handlers;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.serializer.SerializerFeature;
-import com.baomidou.mybatisplus.core.toolkit.Assert;
-import lombok.extern.slf4j.Slf4j;
 import org.apache.ibatis.type.JdbcType;
 import org.apache.ibatis.type.MappedJdbcTypes;
 import org.apache.ibatis.type.MappedTypes;
 
+import java.lang.reflect.Field;
+
 /**
  * Fastjson 实现 JSON 字段类型处理器
  *
  * @author hubin
  * @since 2019-08-25
  */
-@Slf4j
 @MappedTypes({Object.class})
 @MappedJdbcTypes(JdbcType.VARCHAR)
 public class FastjsonTypeHandler extends AbstractJsonTypeHandler<Object> {
 
     public FastjsonTypeHandler(Class<?> type) {
+        super(type);
         if (log.isTraceEnabled()) {
             log.trace("FastjsonTypeHandler(" + type + ")");
         }
-        Assert.notNull(type, "Type argument cannot be null");
+    }
+
+    public FastjsonTypeHandler(Class<?> type, Field field) {
+        super(type, field);
     }
 
     @Override
     public Object parse(String json) {
-        return JSON.parseObject(json, genericType);
+        return JSON.parseObject(json, this.getFieldType());
     }
 
     @Override

+ 9 - 7
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/GsonTypeHandler.java

@@ -17,18 +17,18 @@ package com.baomidou.mybatisplus.extension.handlers;
 
 import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.google.gson.Gson;
-import lombok.extern.slf4j.Slf4j;
 import org.apache.ibatis.type.JdbcType;
 import org.apache.ibatis.type.MappedJdbcTypes;
 import org.apache.ibatis.type.MappedTypes;
 
+import java.lang.reflect.Field;
+
 /**
  * Gson 实现 JSON 字段类型处理器
  *
  * @author hubin
  * @since 2019-08-25
  */
-@Slf4j
 @MappedTypes({Object.class})
 @MappedJdbcTypes(JdbcType.VARCHAR)
 public class GsonTypeHandler extends AbstractJsonTypeHandler<Object> {
@@ -36,15 +36,17 @@ public class GsonTypeHandler extends AbstractJsonTypeHandler<Object> {
     private static Gson GSON;
 
     public GsonTypeHandler(Class<?> type) {
-        if (log.isTraceEnabled()) {
-            log.trace("GsonTypeHandler(" + type + ")");
-        }
-        Assert.notNull(type, "Type argument cannot be null");
+        super(type);
     }
 
+    public GsonTypeHandler(Class<?> type, Field field) {
+        super(type, field);
+    }
+
+
     @Override
     public Object parse(String json) {
-        return getGson().fromJson(json, this.genericType);
+        return getGson().fromJson(json, this.getFieldType());
     }
 
     @Override

+ 8 - 7
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/JacksonTypeHandler.java

@@ -19,12 +19,12 @@ import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import lombok.extern.slf4j.Slf4j;
 import org.apache.ibatis.type.JdbcType;
 import org.apache.ibatis.type.MappedJdbcTypes;
 import org.apache.ibatis.type.MappedTypes;
 
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.lang.reflect.Type;
 
 /**
@@ -33,7 +33,6 @@ import java.lang.reflect.Type;
  * @author hubin
  * @since 2019-08-25
  */
-@Slf4j
 @MappedTypes({Object.class})
 @MappedJdbcTypes(JdbcType.VARCHAR)
 public class JacksonTypeHandler extends AbstractJsonTypeHandler<Object> {
@@ -41,19 +40,21 @@ public class JacksonTypeHandler extends AbstractJsonTypeHandler<Object> {
     private static ObjectMapper OBJECT_MAPPER;
 
     public JacksonTypeHandler(Class<?> type) {
-        if (log.isTraceEnabled()) {
-            log.trace("JacksonTypeHandler(" + type + ")");
-        }
-        Assert.notNull(type, "Type argument cannot be null");
+        super(type);
+    }
+
+    public JacksonTypeHandler(Class<?> type, Field field) {
+        super(type, field);
     }
 
+
     @Override
     public Object parse(String json) {
         try {
             return getObjectMapper().readValue(json, new TypeReference<Object>() {
                 @Override
                 public Type getType() {
-                    return genericType;
+                    return getFieldType();
                 }
             });
         } catch (IOException e) {