Browse Source

增加Fastjson2支持.

nieqiurong 1 năm trước cách đây
mục cha
commit
e9ff15e599

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

@@ -21,6 +21,7 @@ dependencies {
     implementation "${lib.'mybatis-velocity'}"
     implementation "${lib.'mybatis-freemarker'}"
     implementation "de.ruedigermoeller:fst:3.0.4-jdk17"
+    implementation "com.alibaba.fastjson2:fastjson2:2.0.43"
     implementation "com.github.ben-manes.caffeine:caffeine:2.9.3"
     testImplementation "com.github.pagehelper:pagehelper:5.3.1"
     testImplementation "com.google.guava:guava:31.1-jre"

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

@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2011-2023, baomidou (jobob@qq.com).
+ *
+ * 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.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;
+
+/**
+ * Fastjson2 实现 JSON 字段类型处理器
+ *
+ * @author nieqiurong
+ * @since 3.5.5
+ */
+@Slf4j
+@MappedTypes({Object.class})
+@MappedJdbcTypes(JdbcType.VARCHAR)
+public class Fastjson2TypeHandler extends AbstractJsonTypeHandler<Object> {
+
+    private final Class<?> type;
+
+    public Fastjson2TypeHandler(Class<?> type) {
+        if (log.isTraceEnabled()) {
+            log.trace("Fastjson2TypeHandler(" + type + ")");
+        }
+        Assert.notNull(type, "Type argument cannot be null");
+        this.type = type;
+    }
+
+
+    @Override
+    protected Object parse(String json) {
+        return JSON.parseObject(json, this.type);
+    }
+
+    @Override
+    protected String toJson(Object obj) {
+        return JSON.toJSONString(obj, JSONWriter.Feature.WriteMapNullValue,
+            JSONWriter.Feature.WriteNullListAsEmpty, JSONWriter.Feature.WriteNullStringAsEmpty);
+    }
+
+}

+ 82 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/test/handlers/FastJson2TypeHandlerTest.java

@@ -0,0 +1,82 @@
+package com.baomidou.mybatisplus.test.handlers;
+
+import com.baomidou.mybatisplus.extension.handlers.Fastjson2TypeHandler;
+import com.baomidou.mybatisplus.test.model.UserBean;
+import org.apache.ibatis.type.JdbcType;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.lenient;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * @author nieqiurong 2020/2/28.
+ */
+@ExtendWith(MockitoExtension.class)
+public class FastJson2TypeHandlerTest extends BaseTypeHandlerTest {
+
+    private static final Fastjson2TypeHandler FASTJSON_TYPE_HANDLER = new Fastjson2TypeHandler(UserBean.class);
+
+    @Test
+    @Override
+    public void setParameter() throws Exception {
+        FASTJSON_TYPE_HANDLER.setParameter(preparedStatement, 1, null, JdbcType.VARCHAR);
+        verify(preparedStatement).setNull(1, JdbcType.VARCHAR.TYPE_CODE);
+        FASTJSON_TYPE_HANDLER.setParameter(preparedStatement, 2, "", JdbcType.VARCHAR);
+        verify(preparedStatement).setString(2, "\"\"");
+        FASTJSON_TYPE_HANDLER.setParameter(preparedStatement, 3, "{}", JdbcType.VARCHAR);
+        verify(preparedStatement).setString(3, "\"{}\"");
+    }
+
+    @Test
+    @Override
+    public void getResultFromResultSetByColumnName() throws Exception {
+        UserBean bean;
+        when(resultSet.getString("column")).thenReturn(null);
+        bean = (UserBean) FASTJSON_TYPE_HANDLER.getResult(resultSet, "column");
+        Assertions.assertNull(bean);
+        when(resultSet.getString("column")).thenReturn("");
+        bean = (UserBean) FASTJSON_TYPE_HANDLER.getResult(resultSet, "column");
+        Assertions.assertNull(bean);
+        when(resultSet.getString("column")).thenReturn("{\"id\":123,\"name\":\"测试\"}");
+        bean = (UserBean) FASTJSON_TYPE_HANDLER.getResult(resultSet, "column");
+        assertEquals(bean.getId(), 123L);
+        assertEquals(bean.getName(), "测试");
+    }
+
+    @Test
+    @Override
+    public void getResultFromResultSetByColumnIndex() throws Exception {
+        UserBean bean;
+        when(resultSet.getString(1)).thenReturn(null);
+        bean = (UserBean) FASTJSON_TYPE_HANDLER.getResult(resultSet, 1);
+        Assertions.assertNull(bean);
+        when(resultSet.getString(2)).thenReturn("");
+        bean = (UserBean) FASTJSON_TYPE_HANDLER.getResult(resultSet, 2);
+        Assertions.assertNull(bean);
+        when(resultSet.getString(3)).thenReturn("{\"id\":123,\"name\":\"测试\"}");
+        bean = (UserBean) FASTJSON_TYPE_HANDLER.getResult(resultSet, 3);
+        assertEquals(bean.getId(), 123L);
+        assertEquals(bean.getName(), "测试");
+    }
+
+    @Test
+    @Override
+    public void getResultFromCallableStatement() throws Exception {
+        UserBean bean;
+        lenient().when(callableStatement.getString(1)).thenReturn(null);
+        bean = (UserBean) FASTJSON_TYPE_HANDLER.getResult(resultSet, 1);
+        Assertions.assertNull(bean);
+        when(callableStatement.getString(2)).thenReturn("");
+        bean = (UserBean) FASTJSON_TYPE_HANDLER.getResult(callableStatement, 2);
+        Assertions.assertNull(bean);
+        when(callableStatement.getString(3)).thenReturn("{\"id\":123,\"name\":\"测试\"}");
+        bean = (UserBean) FASTJSON_TYPE_HANDLER.getResult(callableStatement, 3);
+        assertEquals(bean.getId(), 123L);
+        assertEquals(bean.getName(), "测试");
+    }
+}