增删改能生效,但是保存数据的格式异于平常,且查询失效。
我的操作如下:
1.自定义类JacksonTypeHandler,由于想要一次性解决所有的映射,所以这里我写成了 @MappedTypes({Object.class})
/**
* 通用类型的TypeHandler
*/
@Slf4j
@MappedTypes({Object.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class JacksonTypeHandler extends BaseTypeHandler<Object> {
private static ObjectMapper objectMapper;
private Class<Object> type;
static {
objectMapper = new ObjectMapper();
}
public JacksonTypeHandler(Class<Object> type) {
if (log.isTraceEnabled()) {
log.trace("JacksonTypeHandler(" + type + ")");
}
if (null == type) {
throw new MybatisPlusException("Type argument cannot be null");
}
this.type = type;
}
private Object parse(String json) {
try {
if (json == null || json.length() == 0) {
return null;
}
return objectMapper.readValue(json, type);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private String toJsonString(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
return parse(rs.getString(columnName));
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return parse(rs.getString(columnIndex));
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return parse(cs.getString(columnIndex));
}
@Override
public void setNonNullParameter(PreparedStatement ps, int columnIndex, Object parameter, JdbcType jdbcType) throws SQLException {
ps.setString(columnIndex, toJsonString(parameter));
}
}
2786

被折叠的 条评论
为什么被折叠?



