mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
add fastjson gson jackson TypeHandler
This commit is contained in:
parent
5f551c4d1a
commit
b7e72a98ad
@ -29,6 +29,41 @@
|
||||
<artifactId>mybatis</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--optional start-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>2.0.25</version>
|
||||
<optional>true</optional>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>2.0.25</version>
|
||||
<optional>true</optional>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.10.1</version>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.14.2</version>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!--optional end-->
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
|
||||
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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>
|
||||
* http://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.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<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 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);
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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>
|
||||
* http://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.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<Object> {
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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>
|
||||
* http://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.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<Object> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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>
|
||||
* http://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.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<Object> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,66 @@
|
||||
/**
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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>
|
||||
* http://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.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<Object> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user