我需要使用mybatis序列化和反序列化Set<MyEnum>
。序列化时效果很好,反序列化时得到Set<String>
。这是我的代码:
mybatis-config.xml:
<typeHandlers>
<typeHandler handler="com.my.JsonTypeHandler" javaType="com.my.MyEnum"/>
<typeHandler handler="com.my.JsonTypeHandler" javaType="java.util.Set"/>
</typeHandlers>
MyEnum:
@Getter
@AllArgsConstructor
public enum MyEnum {
MILEAGE(1),
DRIVE_DURATION(2)
;
private int value;
@JsonCreator
public static MyEnum fromName(String name) {
return MyEnum.valueOf(name);
}
}
mapper.xml:
<select id="queryByParam" parameterType="com.my.DaoQueryParam" resultType="com.my.model.Product">
select * from product
<where>
product_id = #{assetProductId}
</where>
</select>
<insert id="insert" parameterType="com.my.model.Product">
insert into product
(some code here)
</insert>
产品:
@Data // lombok
public class Product {
private Set<MyEnum> myEnums;
}
JsonTypeHandler:
public class JsonTypeHandler<T extends Object> extends BaseTypeHandler<T> {
private static final ObjectMapper mapper = new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
private Class<T> clazz;
public JsonTypeHandler(Class<T> clazz) {
if (clazz == null) throw new IllegalArgumentException("Type argument cannot be null");
this.clazz = clazz;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, this.toJson(parameter));
}
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
return this.toObject(rs.getString(columnName), clazz);
}
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return this.toObject(rs.getString(columnIndex), clazz);
}
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return this.toObject(cs.getString(columnIndex), clazz);
}
private String toJson(T object) {
try {
return mapper.writeValueAsString(object);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private T toObject(String content, Class<?> clazz) {
if (content != null && !content.isEmpty()) {
try {
return (T) mapper.readValue(content, clazz);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
}
mysql中myEnums列的对应类型是VARCHAR。
序列化时,我得到令人满意的结果,例如。 ["MILEAGE"]
。
反序列化时,它不会引发任何错误,但是事实证明myEnums
中的Product
字段实际上是Set<String>
。
我试图修改mybatis-config.xml:
<typeHandlers>
<typeHandler handler="com.my.JsonTypeHandler" javaType="java.util.Set<MyEnum>"/>
</typeHandlers>
但发生错误,并且在javaType中不允许使用'<'字符。 我是mybatis的新手,这个问题困扰了我几个小时。有人可以帮忙吗?
答案 0 :(得分:0)
我认为,您能做的最好的就是写一个JsonSetTypeHandler
。
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Set;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonSetTypeHandler extends BaseTypeHandler<Set<?>> {
private static final ObjectMapper mapper = new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
private final JavaType javaType;
public JsonSetTypeHandler(Class<?> clazz) {
if (clazz == null)
throw new IllegalArgumentException("Type argument cannot be null");
this.javaType = mapper.getTypeFactory()
.constructCollectionType(Set.class, clazz);
}
@Override
public void setNonNullParameter(PreparedStatement ps,
int i, Set<?> parameter, JdbcType jdbcType)
throws SQLException {
ps.setString(i, this.toJson(parameter));
}
@Override
public Set<?> getNullableResult(ResultSet rs,
String columnName) throws SQLException {
return this.toSet(rs.getString(columnName));
}
@Override
public Set<?> getNullableResult(ResultSet rs,
int columnIndex) throws SQLException {
return this.toSet(rs.getString(columnIndex));
}
@Override
public Set<?> getNullableResult(CallableStatement cs,
int columnIndex) throws SQLException {
return this.toSet(cs.getString(columnIndex));
}
private String toJson(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private Set<?> toSet(String content) {
if (content != null && !content.isEmpty()) {
try {
return mapper.readValue(content, javaType);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
}
您可能需要为每个参数/结果映射指定typeHandler
,而不是全局注册类型处理程序[1]。
<resultMap type="com.my.model.Product" id="productRM">
<id property="id" column="id" />
<result property="myEnums" column="my_enums"
typeHandler="com.my.JsonSetTypeHandler" javaType="com.my.MyEnum" />
</resultMap>
<select id="queryByParam" resultMap="productRM">
select * from product where ...
</select>
<insert id="insert">
insert into product (id, my_enums) values (#{id},
#{myEnums,typeHandler=com.my.JsonSetTypeHandler,javaType=com.my.MyEnum})
</insert>