最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Mybatis-Plus自定义集合类型的类型处理器代码解析
时间:2022-06-29 01:57:53 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Mybatis-Plus自定义集合类型的类型处理器代码解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
两种方法,第一种很麻烦,对mp自带的插入操作有限制,后来改为更简洁的第二种方法
1.配合xml文件
TypeHandler
/**
* 描述:fastjson的集合对象类型处理器,将mysql表中的json字段映射到实体类中的{@code List>}属性
* 对照MP自带的FastjsonTypeHandler,自带的类型处理器会把所有的{@code List>}都会解析为{@code List},
* 这样在遍历其中对象时,调用对象属性的get、set方法就会发送类型转换JsonObject->?,这种情况转换错误。
* 该处理器必须配合xml文件使用,不然无法获取要解析的对象类型,同时不能配合MP自带的{@link com.baomidou.mybatisplus.annotation.TableField}
* 使用,默认情况下@TableField会将JavaType设置为字段的类型,如果是List>则type = List.class,无法明确其中的泛型,类型转换会变成JsonObject。
* 用法:
* {@code
* 1.实体类上使用注解@TableName(value = "表名", resultMap = "xml文件中的resultMap的id")
* 2.xml文件中自定义resultMap并设置需要JSON转换的字段
*
* 3.自定义方法上的用法
* @Mapper
* public interface DemoDao extends BaseMapper {
* @Select("select * from demo where demo_id = #{demoId}")
* @ResultMap(value = "xml文件中的resultMap的id")
* List selectListByDemoId(Long demoId);
* }
* }
*/
@Slf4j
@MappedJdbcTypes(value = JdbcType.VARCHAR)
public class FastJsonArrayTypeHandler extends AbstractJsonTypeHandler> {
private Class> type;
public FastJsonArrayTypeHandler(Class> type) {
if (log.isTraceEnabled()) {
log.trace("FastjsonTypeHandler(" + type + ")");
}
Assert.notNull(type, "Type argument cannot be null");
this.type = type;
}
@Override
protected List> parse(String json) {
return JSON.parseArray(json, type);// 注意不要使用parseObject方法
}
@Override
protected String toJson(List> obj) {
return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty);
}
}
xml文件
Dao
@Mapper public interface DemoDao extends BaseMapper{ @Select("select * from demo where demo_id = #{demoId}") @ResultMap(value = "demoMap") List selectListByPlanId(Long demoId); }
Entity
@Data
@TableName(value = "demo", resultMap = "demoMap")
public class DemoEntity implements Serializable {
private static final long serialVersionUID = -1L;
/**
* 主键
*/
@TableId
private Long id;
private Long demoId
/**
* json字段
*/
private List demoInfo;
}
2.手动注册
TypeHandler
@Slf4j @MappedJdbcTypes(value = JdbcType.VARCHAR) public class FastJsonArrayTypeHandlerextends AbstractJsonTypeHandler
初始化,剩下的bean和dao都不需要额外配置
/**
* 描述:初始化配置
* 手动注册类型处理器
*/
@Component
public class InitConfig implements CommandLineRunner {
public static final com.alibaba.fastjson.TypeReference> F_DEMO_INFO = new com.alibaba.fastjson.TypeReference>() {
};
public static final TypeReference> M_DEMO_INFO = new TypeReference>() {
};
// mp自动装配时注入的factory,可用于获取mybatis的配置属性,这里用来获取类型处理器的注册器
private final SqlSessionFactory factory;
public InitConfig(SqlSessionFactory factory) {
this.factory = factory;
}
/**
* 注册类型处理器
* {@code
* 1.List类型处理器
* ...
* }
*
* @param args incoming main method arguments
* @throws Exception on error
*/
@SuppressWarnings("all")
@Override
public void run(String... args) throws Exception {
TypeHandlerRegistry typeHandlerRegistry = factory.getConfiguration().getTypeHandlerRegistry();
typeHandlerRegistry.register(M_DEMO_INFO, new FastJsonArrayTypeHandler(F_DEMO_INFO));
}
}
目前方法二存在的缺陷:虽然新增、查询不存在问题,执行MP自带的更新操作时,parameterMap参数类型都是Object,不会经过自定义的TypeHandler处理,最后会把json对象直接set进去(update demo ..., demo_info = JSON对象 ...)导致报错
相关文章
- 战锤40K星际战士2全成就汇总及解锁攻略 11-07
- 黑神话悟空全部战车位置介绍说明 11-07
- 黑神话悟空仙品酒酿选择推荐分享 11-07
- 黑神话悟空铜云棒后期获得介绍说明 11-07
- 第五人格活动11日历完整版 11-07
- 蛋仔派对艾比保卫战八方来客的过关秘籍 11-07