diff --git a/mybatis-flex-core/pom.xml b/mybatis-flex-core/pom.xml index 088ec47b..2eda5a9b 100644 --- a/mybatis-flex-core/pom.xml +++ b/mybatis-flex-core/pom.xml @@ -29,6 +29,41 @@ mybatis + + + + com.alibaba + fastjson + 2.0.25 + true + compile + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.25 + true + compile + + + + com.google.code.gson + gson + 2.10.1 + compile + true + + + + com.fasterxml.jackson.core + jackson-databind + 2.14.2 + compile + true + + + mysql mysql-connector-java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/BaseJsonTypeHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/BaseJsonTypeHandler.java new file mode 100644 index 00000000..094ca938 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/BaseJsonTypeHandler.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.core.handler; + +import com.mybatisflex.core.util.StringUtil; +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; + +public abstract class BaseJsonTypeHandler extends BaseTypeHandler { + + @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 StringUtil.isBlank(json) ? null : parseJson(json); + } + + @Override + public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + final String json = rs.getString(columnIndex); + return StringUtil.isBlank(json) ? null : parseJson(json); + } + + @Override + public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + final String json = cs.getString(columnIndex); + return StringUtil.isBlank(json) ? null : parseJson(json); + } + + protected abstract T parseJson(String json); + + protected abstract String toJson(T object); +} \ No newline at end of file diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/Fastjson2TypeHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/Fastjson2TypeHandler.java new file mode 100644 index 00000000..7800899a --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/Fastjson2TypeHandler.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.core.handler; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONWriter; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +@MappedTypes({Object.class}) +@MappedJdbcTypes(JdbcType.VARCHAR) +public class Fastjson2TypeHandler extends BaseJsonTypeHandler { + + private final Class type; + + public Fastjson2TypeHandler(Class type) { + this.type = type; + } + + @Override + protected Object parseJson(String json) { + return JSON.parseObject(json, type); + } + + @Override + protected String toJson(Object object) { + return JSON.toJSONString(object + , JSONWriter.Feature.WriteMapNullValue + , JSONWriter.Feature.WriteNullListAsEmpty + , JSONWriter.Feature.WriteNullStringAsEmpty + ); + } +} \ No newline at end of file diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/FastjsonTypeHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/FastjsonTypeHandler.java new file mode 100644 index 00000000..e0c2fe1d --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/FastjsonTypeHandler.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.core.handler; + +import com.alibaba.fastjson.serializer.SerializerFeature; +import com.alibaba.fastjson.JSON; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +@MappedTypes({Object.class}) +@MappedJdbcTypes(JdbcType.VARCHAR) +public class FastjsonTypeHandler extends BaseJsonTypeHandler { + + private final Class type; + + public FastjsonTypeHandler(Class type) { + this.type = type; + } + + @Override + protected Object parseJson(String json) { + return JSON.parseObject(json, type); + } + + @Override + protected String toJson(Object object) { + return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue, + SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty); + } +} \ No newline at end of file diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/GsonTypeHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/GsonTypeHandler.java new file mode 100644 index 00000000..61fe9fab --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/GsonTypeHandler.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.core.handler; + +import com.google.gson.Gson; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +@MappedTypes({Object.class}) +@MappedJdbcTypes(JdbcType.VARCHAR) +public class GsonTypeHandler extends BaseJsonTypeHandler { + + private static Gson gson; + private final Class type; + + public GsonTypeHandler(Class type) { + this.type = type; + } + + @Override + protected Object parseJson(String json) { + return getGson().fromJson(json, type); + } + + @Override + protected String toJson(Object object) { + return getGson().toJson(object); + } + + + public static Gson getGson() { + if (null == gson) { + gson = new Gson(); + } + return gson; + } + + public static void setGson(Gson gson) { + GsonTypeHandler.gson = gson; + } +} \ No newline at end of file diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/JacksonTypeHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/JacksonTypeHandler.java new file mode 100644 index 00000000..bf49926d --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/JacksonTypeHandler.java @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.core.handler; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.mybatisflex.core.exception.FlexExceptions; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +import java.io.IOException; + +@MappedTypes({Object.class}) +@MappedJdbcTypes(JdbcType.VARCHAR) +public class JacksonTypeHandler extends BaseJsonTypeHandler { + + private static ObjectMapper objectMapper; + private final Class type; + + public JacksonTypeHandler(Class type) { + this.type = type; + } + + @Override + protected Object parseJson(String json) { + try { + return getObjectMapper().readValue(json, type); + } catch (IOException e) { + throw FlexExceptions.wrap(e, "Can not parseJson by JacksonTypeHandler: " + json); + } + } + + @Override + protected String toJson(Object object) { + try { + return getObjectMapper().writeValueAsString(object); + } catch (JsonProcessingException e) { + throw FlexExceptions.wrap(e, "Can not convert object to Json by JacksonTypeHandler: " + object); + } + } + + public static ObjectMapper getObjectMapper() { + if (null == objectMapper) { + objectMapper = new ObjectMapper(); + } + return objectMapper; + } + + public static void setObjectMapper(ObjectMapper objectMapper) { + JacksonTypeHandler.objectMapper = objectMapper; + } +} \ No newline at end of file