mybatis plus json查询

文章介绍了如何在MyBatis中自定义类型处理器`FileVoListTypeHandler`,用于处理`List<FileVo>`类型的字段,通过`@TableField`和`@MappedJdbcTypes`等注解配置,并利用JSON进行序列化和反序列化。当`@TableName`的`autoResultMap`未设置时,查询结果可能无法自动映射。
摘要由CSDN通过智能技术生成
@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没有设置,查询时不会映射到字段上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值