Skip to content

Commit

Permalink
代码调整.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Apr 7, 2024
1 parent 8918fea commit e0bb755
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
* Json类型处理器接口(实现类确保为多例状态).
* <p>
* 注意:查询返回时需要使用resultMap
*
* <pre>
* Example:
* &lt;result property="xx" column="xx" javaType="list" typeHandler="com.baomidou.mybatisplus.extension.handlers.GsonTypeHandler"/&gt;
* &lt;result property="xx" column="xx" typeHandler="com.baomidou.mybatisplus.extension.handlers.GsonTypeHandler"/&gt;
* </pre>
* </p>
*
* @author nieqiurong 2024年3月4日
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import com.baomidou.mybatisplus.core.handlers.IJsonTypeHandler;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
Expand All @@ -36,7 +36,7 @@
*/
public abstract class AbstractJsonTypeHandler<T> extends BaseTypeHandler<T> implements IJsonTypeHandler<T> {

protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected final Log log = LogFactory.getLog(this.getClass());

protected final Class<?> type;

Expand All @@ -63,6 +63,7 @@ public AbstractJsonTypeHandler(Class<?> type) {
*
* @param type 类型
* @param field 字段
* @since 3.5.6
*/
public AbstractJsonTypeHandler(Class<?> type, Field field) {
this(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public class FastjsonTypeHandler extends AbstractJsonTypeHandler<Object> {

public FastjsonTypeHandler(Class<?> type) {
super(type);
if (log.isTraceEnabled()) {
log.trace("FastjsonTypeHandler(" + type + ")");
}
}

public FastjsonTypeHandler(Class<?> type, Field field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public GsonTypeHandler(Class<?> type, Field field) {
super(type, field);
}


@Override
public Object parse(String json) {
return getGson().fromJson(json, this.getFieldType());
Expand All @@ -65,4 +64,5 @@ public static void setGson(Gson gson) {
Assert.notNull(gson, "Gson should not be null");
GsonTypeHandler.GSON = gson;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package com.baomidou.mybatisplus.extension.handlers;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.lang.reflect.Field;

/**
Expand All @@ -45,12 +47,15 @@ public JacksonTypeHandler(Class<?> type, Field field) {
super(type, field);
}


@Override
public Object parse(String json) {
ObjectMapper objectMapper = getObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
JavaType javaType = typeFactory.constructType(getFieldType());
try {
return getObjectMapper().readValue(json, getObjectMapper().getTypeFactory().constructType(getFieldType()));
} catch (IOException e) {
return objectMapper.readValue(json, javaType);
} catch (JacksonException e) {
log.error("deserialize json: " + json + " to " + javaType + " error ", e);
throw new RuntimeException(e);
}
}
Expand All @@ -60,6 +65,7 @@ public String toJson(Object obj) {
try {
return getObjectMapper().writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.error("serialize " + obj + " to json error ", e);
throw new RuntimeException(e);
}
}
Expand All @@ -75,4 +81,5 @@ public static void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "ObjectMapper should not be null");
JacksonTypeHandler.OBJECT_MAPPER = objectMapper;
}

}

0 comments on commit e0bb755

Please sign in to comment.