Browse Source

扩展新增 json 类型处理器 jackson fastjson 两种实现

hubin 5 years ago
parent
commit
c763b24d8d

+ 3 - 3
build.gradle

@@ -57,8 +57,8 @@ ext {
         "junit-jupiter-api"          : "org.junit.jupiter:junit-jupiter-api:${junitVersion}",
         "junit-jupiter-engine"       : "org.junit.jupiter:junit-jupiter-engine:${junitVersion}",
         "mockito-all"                : "org.mockito:mockito-all:1.10.19",
-        "fastjson"                   : "com.alibaba:fastjson:1.2.56",
-        "jackson"                    : "com.fasterxml.jackson.core:jackson-databind:2.9.6",
+        "fastjson"                   : "com.alibaba:fastjson:1.2.59",
+        "jackson"                    : "com.fasterxml.jackson.core:jackson-databind:2.9.9",
         "tomcatjdbc"                 : "org.apache.tomcat:tomcat-jdbc:9.0.16",
         "lagarto"                    : "org.jodd:jodd-lagarto:5.0.7",
         //datasource
@@ -83,7 +83,7 @@ ext {
 
 allprojects {
     group = 'com.baomidou'
-    version = "3.2.0.2-SNAPSHOT"
+    version = "3.2.0"
 }
 
 description = "Mybatis 增强工具包 - 只做增强不做改变,简化CRUD操作"

+ 3 - 0
mybatis-plus-extension/build.gradle

@@ -10,9 +10,12 @@ dependencies {
     implementation("${lib["spring-jdbc"]}")
     implementation("${lib["slf4j-api"]}")
     implementation("${lib["p6spy"]}")
+    implementation("${lib["jackson"]}")
+    implementation("${lib["fastjson"]}")
 
     testImplementation("${lib["spring-web"]}")
     testImplementation("${lib["javax.servlet-api"]}")
+    testImplementation("${lib["jackson"]}")
     testImplementation("${lib["fastjson"]}")
 
     testImplementation("${lib["hikaricp"]}")

+ 72 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/FastjsonTypeHandler.java

@@ -0,0 +1,72 @@
+/*
+ * 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.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+import org.apache.ibatis.type.MappedJdbcTypes;
+
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Fastjson 实现 JSON 字段类型处理器
+ *
+ * @author hubin
+ * @since 2019-08-25
+ */
+@Slf4j
+@MappedJdbcTypes(JdbcType.VARCHAR)
+public class FastjsonTypeHandler extends BaseTypeHandler<Object> {
+    private Class<Object> type;
+
+    public FastjsonTypeHandler(Class<Object> type) {
+        if (log.isTraceEnabled()) {
+            log.trace("FastjsonTypeHandler(" + type + ")");
+        }
+        if (null == type) {
+            throw new MybatisPlusException("Type argument cannot be null");
+        }
+        this.type = type;
+    }
+
+    @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);
+    }
+
+    @Override
+    public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
+        return JSON.parseObject(callableStatement.getString(i), type);
+    }
+}

+ 96 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/handlers/JacksonTypeHandler.java

@@ -0,0 +1,96 @@
+/*
+ * 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.exceptions.MybatisPlusException;
+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 java.io.IOException;
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Jackson 实现 JSON 字段类型处理器
+ *
+ * @author hubin
+ * @since 2019-08-25
+ */
+@Slf4j
+@MappedJdbcTypes(JdbcType.VARCHAR)
+public class JacksonTypeHandler extends BaseTypeHandler<Object> {
+    private static ObjectMapper objectMapper;
+    private Class<Object> type;
+
+    static {
+        objectMapper = new ObjectMapper();
+    }
+
+    public JacksonTypeHandler(Class<Object> type) {
+        if (log.isTraceEnabled()) {
+            log.trace("JacksonTypeHandler(" + type + ")");
+        }
+        if (null == type) {
+            throw new MybatisPlusException("Type argument cannot be null");
+        }
+        this.type = type;
+    }
+
+    private 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) {
+        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));
+    }
+}