最近业务有需要一些页数字段进行传输,考虑到后续可能继续扩展,所以使用了一个实体类来存储,没有在数据库中创建字段;先看一下我的要存的特殊字符:
要使用resultMap,来制定typeHandler类型,但是存基本的字段的话只需要用最基本的JSONObkectHander即可(无map、list等前提下),不需要更换,但是读取的话就会自动走我制定了Handler:
insert into nlu_template_library
(template_id,param_id,begin,end,special_word)
values
(#{sp.templateId},#{sp.paramId},#{sp.begin},#{sp.end},#{sp.specialWord,typeHandler=com.liuxy.voiceplatform.typehandler.JsonObjectTypeHandler})
typeHandler的内容是指定你的实体类的类型,如果有Map等需要特殊处理重写方法,我在父类中定义了很多方法,正常只需要继承(正常为没有特殊的list和map)
@MappedJdbcTypes({JdbcType.VARCHAR})
@MappedTypes({TransferInfo.class})
public class NluTemplateTypeHandler extends AbstractObjectTypeHandler {
}
最核心的类只需要重写BaseTypeHandler的方法即可
public class AbstractObjectTypeHandler extends BaseTypeHandler {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, JsonUtil.getJsonFromObject(ps));
}
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
String data = rs.getString(columnName);
return StringUtils.isBlank(data) ? null : JsonUtil.getObjectFromString(data,(Class) getRawType());
}
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String data = rs.getString(columnIndex);
return StringUtils.isBlank(data) ? null : JsonUtil.getObjectFromString(data,(Class) getRawType());
}
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String data = cs.getString(columnIndex);
return StringUtils.isBlank(data) ? null : JsonUtil.getObjectFromString(data,(Class) getRawType());
}
public class JsonObjectTypeHandler extends BaseTypeHandler {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i, JSONObject.toJSONString(o));
}
@Override
public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
return JSONObject.parse(s);
}
@Override
public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
return JSONObject.parse(resultSet.getString(i));
}
@Override
public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
return JSONObject.parse(callableStatement.getString(i));
}
}
public class JsonArrayTypeHandler implements TypeHandler> {
private List getListByJsonArrayString(String content) {
if (StringUtils.isEmpty(content)) {
return new ArrayList<>();
}
return JSONObject.parseObject(content, new TypeReference>() {});
}
@Override
public void setParameter(PreparedStatement preparedStatement, int i, List ts, JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i, JSONObject.toJSONString(ts));
}
@Override
public List getResult(ResultSet resultSet, String s) throws SQLException {
return getListByJsonArrayString(resultSet.getString(s));
}
@Override
public List getResult(ResultSet resultSet, int i) throws SQLException {
return getListByJsonArrayString(resultSet.getString(i));
}
@Override
public List getResult(CallableStatement callableStatement, int i) throws SQLException {
return getListByJsonArrayString(callableStatement.getString(i));
}
}