我有一个带有某些类型字段的 pojo Set<String>
。我想将它们作为 json 保存在数据库中,所以我创建了一个自定义 typeHandler,但是当我尝试保存时,我得到了错误:没有找到 TypeHandler ...。
testSave(it.infora.suap.service.MailMessageServiceTest): nested exception is org.apache.ibatis.executor.ExecutorException: There was no TypeHandler found for parameter recipients_to of statement it.infora.suap.persistence.MailMessageMapper.insertEmailMessage
a中的参数recipients_toSet<String>
这是我的自定义 typeHandler 类:
public class SetTypeHandler implements TypeHandler<Set<String>>{
@Override
public void setParameter(PreparedStatement ps, int columnIndex, Set<String> parameter, JdbcType jt) throws SQLException {
ps.setString(columnIndex, serializeToJson(parameter));
}
@Override
public Set<String> getResult(ResultSet rs, String columnName) throws SQLException {
return deserializeFromJson(rs.getString(columnName));
}
@Override
public Set<String> getResult(ResultSet rs, int columnIndex) throws SQLException {
return deserializeFromJson(rs.getString(columnIndex));
}
@Override
public Set<String> getResult(CallableStatement cs, int columnIndex) throws SQLException {
return deserializeFromJson(cs.getString(columnIndex));
}
private String serializeToJson(Set<String> parameter){
Gson gson = new Gson();
return gson.toJson(parameter);
}
private Set<String> deserializeFromJson(String value){
Gson gson = new Gson();
Type collectionType = new TypeToken<Set<String>>(){}.getType();
Set<String> result = gson.fromJson(value, collectionType);
return result;
}
}
怎么了?我正在使用带注释的映射器接口而不是 mapper.xml
谢谢
安德烈亚