欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis-Plus自定義集合類型的類型處理器詳解

 更新時(shí)間:2022年01月13日 15:46:24   作者:nasoda_pro1993  
這篇文章主要介紹了Mybatis-Plus自定義集合類型的類型處理器詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

兩種方法,第一種很麻煩,對(duì)mp自帶的插入操作有限制,后來改為更簡潔的第二種方法

1.配合xml文件

TypeHandler

/**
?* 描述:fastjson的集合對(duì)象類型處理器,將mysql表中的json字段映射到實(shí)體類中的{@code List<?>}屬性
?* 對(duì)照MP自帶的FastjsonTypeHandler,自帶的類型處理器會(huì)把所有的{@code List<?>}都會(huì)解析為{@code List<JsonObject>},
?* 這樣在遍歷其中對(duì)象時(shí),調(diào)用對(duì)象屬性的get、set方法就會(huì)發(fā)送類型轉(zhuǎn)換JsonObject->?,這種情況轉(zhuǎn)換錯(cuò)誤。
?* 該處理器必須配合xml文件使用,不然無法獲取要解析的對(duì)象類型,同時(shí)不能配合MP自帶的{@link com.baomidou.mybatisplus.annotation.TableField}
?* 使用,默認(rèn)情況下@TableField會(huì)將JavaType設(shè)置為字段的類型,如果是List<?>則type = List.class,無法明確其中的泛型,類型轉(zhuǎn)換會(huì)變成JsonObject。
?* 用法:
?* <pre>{@code
?* 1.實(shí)體類上使用注解@TableName(value = "表名", resultMap = "xml文件中的resultMap的id")
?* 2.xml文件中自定義resultMap并設(shè)置需要JSON轉(zhuǎn)換的字段
?* <result property="實(shí)體類屬性名" column="表字段名" javaType="List中的對(duì)象全類名,例如List<Email>,則JavaType為Email的全類名" typeHandler="自定義處理器全類名"/>
?* 3.自定義方法上的用法
?* @Mapper
?* public interface DemoDao extends BaseMapper<DemoEntity> {
?* ? ?@Select("select * from demo where demo_id = #{demoId}")
?* ? ?@ResultMap(value = "xml文件中的resultMap的id")
?* ? ?List<DemoEntity> selectListByDemoId(Long demoId);
?* }
?* }</pre>
?*/
@Slf4j
@MappedJdbcTypes(value = JdbcType.VARCHAR)
public class FastJsonArrayTypeHandler extends AbstractJsonTypeHandler<List<?>> {?
? ? 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文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="a.b.dao.DemoDao">
? ? <resultMap type="a.b.entity.DemoEntity" id="demoMap">
? ? ? ? <result property="DemoInfo" column="demo_info" javaType="a.b.xx.DiseaseInfo" typeHandler="a.b.handler.FastJsonArrayTypeHandler"/>
? ? </resultMap>
</mapper>

Dao

@Mapper
public interface DemoDao extends BaseMapper<DemoEntity> {
? ? @Select("select * from demo where demo_id = #{demoId}")
? ? @ResultMap(value = "demoMap")
? ? List<DemoEntity> 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> demoInfo;
}

2.手動(dòng)注冊(cè)

TypeHandler

@Slf4j
@MappedJdbcTypes(value = JdbcType.VARCHAR)
public class FastJsonArrayTypeHandler<T> extends AbstractJsonTypeHandler<Object> {?
? ? private TypeReference<List<T>> type;?
? ? public FastJsonArrayTypeHandler(TypeReference<List<T>> type) {
? ? ? ? this.type = type;
? ? }
?
? ? @Override
? ? protected Object parse(String json) {
? ? ? ? return JSON.parseObject(json, type);
? ? }
?
? ? @Override
? ? protected String toJson(Object obj) {
? ? ? ? return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty);
? ? }?
}

初始化,剩下的bean和dao都不需要額外配置

/**
?* 描述:初始化配置
?* 手動(dòng)注冊(cè)類型處理器
?*/
@Component
public class InitConfig implements CommandLineRunner {
? ? public static final com.alibaba.fastjson.TypeReference<List<DemoInfo>> F_DEMO_INFO = new com.alibaba.fastjson.TypeReference<List<DemoInfo>>() {
? ? };
? ? public static final TypeReference<List<DemoInfo>> M_DEMO_INFO = new TypeReference<List<DemoInfo>>() {
? ? };
? ? // mp自動(dòng)裝配時(shí)注入的factory,可用于獲取mybatis的配置屬性,這里用來獲取類型處理器的注冊(cè)器
? ? private final SqlSessionFactory factory;
?
? ? public InitConfig(SqlSessionFactory factory) {
? ? ? ? this.factory = factory;
? ? }
?
? ? /**
? ? ?* 注冊(cè)類型處理器
? ? ?* <pre>{@code
? ? ?* 1.List<DemoInfo>類型處理器
? ? ?* ...
? ? ?* }</pre>
? ? ?*
? ? ?* @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));
? ? }
}

目前方法二存在的缺陷:雖然新增、查詢不存在問題,執(zhí)行MP自帶的更新操作時(shí),parameterMap參數(shù)類型都是Object,不會(huì)經(jīng)過自定義的TypeHandler處理,最后會(huì)把json對(duì)象直接set進(jìn)去(update demo ..., demo_info = JSON對(duì)象 ...)導(dǎo)致報(bào)錯(cuò)

暫無優(yōu)雅的解決方案,先做個(gè)記錄 。以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在lambda中使用外部變量的一些心得分享

    在lambda中使用外部變量的一些心得分享

    這篇文章主要介紹了在lambda中使用外部變量的一些心得,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • SpringBoot上傳圖片的示例

    SpringBoot上傳圖片的示例

    這篇文章主要介紹了SpringBoot上傳圖片的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫方式

    MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫方式

    這篇文章主要介紹了MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 詳解Spring Cloud Consul 實(shí)現(xiàn)服務(wù)注冊(cè)和發(fā)現(xiàn)

    詳解Spring Cloud Consul 實(shí)現(xiàn)服務(wù)注冊(cè)和發(fā)現(xiàn)

    這篇文章主要介紹了Spring Cloud Consul 實(shí)現(xiàn)服務(wù)注冊(cè)和發(fā)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • MyBatis一對(duì)一映射初識(shí)教程

    MyBatis一對(duì)一映射初識(shí)教程

    MyBatis是一個(gè)支持普通SQL查詢,存儲(chǔ)過程和高級(jí)映射的優(yōu)秀持久層框架。在我們生活中一對(duì)一的例子很多見,下面通過本文給大家?guī)砹薽ybatis一對(duì)一映射初識(shí)教程,感興趣的朋友一起看下吧
    2016-08-08
  • 完美解決PermGen space異常的問題

    完美解決PermGen space異常的問題

    這篇文章主要介紹了完美解決PermGen space異常的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 淺談什么是SpringBoot異常處理自動(dòng)配置的原理

    淺談什么是SpringBoot異常處理自動(dòng)配置的原理

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著SpringBoot異常處理自動(dòng)配置展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Java分布式session存儲(chǔ)解決方案圖解

    Java分布式session存儲(chǔ)解決方案圖解

    這篇文章主要介紹了Java分布式session存儲(chǔ)解決方案圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java中程序的運(yùn)行全過程

    Java中程序的運(yùn)行全過程

    這篇文章主要介紹了Java中程序的運(yùn)行全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 淺談Timer和TimerTask與線程的關(guān)系

    淺談Timer和TimerTask與線程的關(guān)系

    下面小編就為大家?guī)硪黄獪\談Timer和TimerTask與線程的關(guān)系。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03

最新評(píng)論