miemie 5 years ago
parent
commit
5d3aef0263

+ 1 - 0
build.gradle

@@ -60,6 +60,7 @@ ext {
         "junit-jupiter-engine"       : "org.junit.jupiter:junit-jupiter-engine:${junitVersion}",
         "fastjson"                   : "com.alibaba:fastjson:1.2.62",
         "jackson"                    : "com.fasterxml.jackson.core:jackson-databind:2.10.1",
+        "gson"                       : "com.google.code.gson:gson:2.8.6",
         "lagarto"                    : "org.jodd:jodd-lagarto:5.1.0-20190624",
         //datasource
         "p6spy"                      : "p6spy:p6spy:3.8.6",

+ 1 - 1
mybatis-plus-extension/build.gradle

@@ -12,5 +12,5 @@ dependencies {
     implementation("${lib["p6spy"]}")
     implementation("${lib["jackson"]}")
     implementation("${lib["fastjson"]}")
-
+    implementation("${lib["gson"]}")
 }

+ 59 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/AbstractJsonTypeHandler.java

@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * https://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.extension.handlers;
+
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * @author miemie
+ * @since 2019-11-28
+ */
+public abstract class AbstractJsonTypeHandler<T> extends BaseTypeHandler<T> {
+
+    @Override
+    public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
+        ps.setString(i, toJson(parameter));
+    }
+
+    @Override
+    public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
+        final String json = rs.getString(columnName);
+        return StringUtils.isBlank(json) && rs.wasNull() ? null : parse(json);
+    }
+
+    @Override
+    public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
+        final String json = rs.getString(columnIndex);
+        return StringUtils.isBlank(json) && rs.wasNull() ? null : parse(json);
+    }
+
+    @Override
+    public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
+        final String json = cs.getString(columnIndex);
+        return StringUtils.isBlank(json) && cs.wasNull() ? null : parse(json);
+    }
+
+    protected abstract T parse(String json);
+
+    protected abstract String toJson(T obj);
+}

+ 6 - 22
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/FastjsonTypeHandler.java

@@ -19,16 +19,10 @@ 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.BaseTypeHandler;
 import org.apache.ibatis.type.JdbcType;
 import org.apache.ibatis.type.MappedJdbcTypes;
 import org.apache.ibatis.type.MappedTypes;
 
-import java.sql.CallableStatement;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
 /**
  * Fastjson 实现 JSON 字段类型处理器
  *
@@ -38,7 +32,7 @@ import java.sql.SQLException;
 @Slf4j
 @MappedTypes({Object.class})
 @MappedJdbcTypes(JdbcType.VARCHAR)
-public class FastjsonTypeHandler extends BaseTypeHandler<Object> {
+public class FastjsonTypeHandler extends AbstractJsonTypeHandler<Object> {
     private Class<Object> type;
 
     public FastjsonTypeHandler(Class<Object> type) {
@@ -50,23 +44,13 @@ public class FastjsonTypeHandler extends BaseTypeHandler<Object> {
     }
 
     @Override
-    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
-        preparedStatement.setString(i, JSON.toJSONString(o, SerializerFeature.WriteMapNullValue,
-            SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty));
-    }
-
-    @Override
-    public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
-        return JSON.parseObject(resultSet.getString(s), type);
-    }
-
-    @Override
-    public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
-        return JSON.parseObject(resultSet.getString(i), type);
+    protected Object parse(String json) {
+        return JSON.parseObject(json, type);
     }
 
     @Override
-    public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
-        return JSON.parseObject(callableStatement.getString(i), type);
+    protected String toJson(Object obj) {
+        return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue,
+            SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty);
     }
 }

+ 60 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/GsonTypeHandler.java

@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * https://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+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;
+
+/**
+ * Gson 实现 JSON 字段类型处理器
+ *
+ * @author hubin
+ * @since 2019-08-25
+ */
+@Slf4j
+@MappedTypes({Object.class})
+@MappedJdbcTypes(JdbcType.VARCHAR)
+public class GsonTypeHandler extends AbstractJsonTypeHandler<Object> {
+    private static Gson gson = new Gson();
+    private Class<Object> type;
+
+    public GsonTypeHandler(Class<Object> type) {
+        if (log.isTraceEnabled()) {
+            log.trace("GsonTypeHandler(" + type + ")");
+        }
+        Assert.notNull(type, "Type argument cannot be null");
+        this.type = type;
+    }
+
+    public static void setGson(Gson gson) {
+        Assert.notNull(gson, "Gson should not be null");
+        GsonTypeHandler.gson = gson;
+    }
+
+    @Override
+    protected Object parse(String json) {
+        return gson.fromJson(json, type);
+    }
+
+    @Override
+    protected String toJson(Object obj) {
+        return gson.toJson(obj);
+    }
+}

+ 11 - 32
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/JacksonTypeHandler.java

@@ -19,16 +19,11 @@ import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.ibatis.type.BaseTypeHandler;
 import org.apache.ibatis.type.JdbcType;
 import org.apache.ibatis.type.MappedJdbcTypes;
 import org.apache.ibatis.type.MappedTypes;
 
 import java.io.IOException;
-import java.sql.CallableStatement;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
 
 /**
  * Jackson 实现 JSON 字段类型处理器
@@ -39,8 +34,8 @@ import java.sql.SQLException;
 @Slf4j
 @MappedTypes({Object.class})
 @MappedJdbcTypes(JdbcType.VARCHAR)
-public class JacksonTypeHandler extends BaseTypeHandler<Object> {
-    private ObjectMapper objectMapper = new ObjectMapper();
+public class JacksonTypeHandler extends AbstractJsonTypeHandler<Object> {
+    private static ObjectMapper objectMapper = new ObjectMapper();
     private Class<Object> type;
 
     public JacksonTypeHandler(Class<Object> type) {
@@ -51,42 +46,26 @@ public class JacksonTypeHandler extends BaseTypeHandler<Object> {
         this.type = type;
     }
 
-    private Object parse(String json) {
+    public static void setObjectMapper(ObjectMapper objectMapper) {
+        Assert.notNull(objectMapper, "ObjectMapper should not be null");
+        JacksonTypeHandler.objectMapper = objectMapper;
+    }
+
+    @Override
+    protected Object parse(String json) {
         try {
-            if (json == null || json.length() == 0) {
-                return null;
-            }
             return objectMapper.readValue(json, type);
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }
 
-    private String toJsonString(Object obj) {
+    @Override
+    protected String toJson(Object obj) {
         try {
             return objectMapper.writeValueAsString(obj);
         } catch (JsonProcessingException e) {
             throw new RuntimeException(e);
         }
     }
-
-    @Override
-    public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
-        return parse(rs.getString(columnName));
-    }
-
-    @Override
-    public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
-        return parse(rs.getString(columnIndex));
-    }
-
-    @Override
-    public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
-        return parse(cs.getString(columnIndex));
-    }
-
-    @Override
-    public void setNonNullParameter(PreparedStatement ps, int columnIndex, Object parameter, JdbcType jdbcType) throws SQLException {
-        ps.setString(columnIndex, toJsonString(parameter));
-    }
 }