@TableName(value = "",autoResultMap = true)
@TableField(typeHandler = FileVoListTypeHandler.class)
public class FileVoListTypeHandler extends ListTypeHandler<FileVo> {
@Override
protected TypeReference<List<FileVo>> specificType() {
return new TypeReference<List<FileVo>>() {
};
}
}
@MappedJdbcTypes({JdbcType.VARBINARY})
@MappedTypes({List.class})
public abstract class ListTypeHandler<T> extends BaseTypeHandler<List<T>> {
public ListTypeHandler() {
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, List<T> parameter, JdbcType jdbcType) throws SQLException {
String content = CollectionUtils.isEmpty(parameter) ? null : JSON.toJSONString(parameter);
ps.setString(i, content);
}
@Override
public List<T> getNullableResult(ResultSet rs, String columnName) throws SQLException {
return this.getListByJsonArrayString(rs.getString(columnName));
}
@Override
public List<T> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return this.getListByJsonArrayString(rs.getString(columnIndex));
}
@Override
public List<T> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return this.getListByJsonArrayString(cs.getString(columnIndex));
}
private List<T> getListByJsonArrayString(String content) {
return (List)(StringUtils.isEmpty(content) ? new ArrayList() : (List)JSON.parseObject(content, this.specificType(), new Feature[0]));
}
protected abstract TypeReference<List<T>> specificType();
}
如果注解TableName的autoResultMap没有设置,查询时不会映射到字段上
文章介绍了如何在MyBatis中自定义类型处理器`FileVoListTypeHandler`,用于处理`List<FileVo>`类型的字段,通过`@TableField`和`@MappedJdbcTypes`等注解配置,并利用JSON进行序列化和反序列化。当`@TableName`的`autoResultMap`未设置时,查询结果可能无法自动映射。

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



