MybatisPlus如何自定義TypeHandler映射JSON類型為List
自定義TypeHandler映射JSON類型為List
1. 實(shí)體類
這里只展示需要映射的字段,分別在所需映射的字段和實(shí)體類上添加注解。
@Data @TableName(value = "report", autoResultMap = true) public class Report { private static final long serialVersionUID = 1L; @ApiModelProperty("id") @TableId(value = "id", type = IdType.AUTO) private Integer id; @ApiModelProperty("報(bào)名信息") @TableField(typeHandler = ReportUserListTypeHandler.class) private List<ReportUser> reportInfo; }
2. ListTypeHandler
提供一個(gè) JSONArray 轉(zhuǎn)換為 Java List集合的處理器
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcTypes; import org.apache.ibatis.type.MappedTypes; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @MappedJdbcTypes(JdbcType.VARBINARY) @MappedTypes({List.class}) public abstract class ListTypeHandler<T> extends BaseTypeHandler<List<T>> { @Override public void setNonNullParameter(PreparedStatement ps, int i, List<T> parameter, JdbcType jdbcType) throws SQLException { String content = CollUtil.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 StrUtil.isBlank(content) ? new ArrayList<>() : JSON.parseObject(content, this.specificType()); } /** * 具體類型,由子類提供 * * @return 具體類型 */ protected abstract TypeReference<List<T>> specificType(); }
3. ReportUserListTypeHandler
由具體的子類提供List集合泛型類型
import com.alibaba.fastjson.TypeReference; import com.hanku.business.model.ReportUser; import java.util.List; public class ReportUserListTypeHandler extends ListTypeHandler<ReportUser> { @Override protected TypeReference<List<ReportUser>> specificType() { return new TypeReference<List<ReportUser>>() { }; } }
4. Java 泛型
如果在 ListTypeHandler 類中直接提供 TypeReference<List<T>> 這種類型,那就等效于TypeReference<List<Object>> 這種類型,后續(xù) fastjson 在轉(zhuǎn)換時(shí)無法確定具體的 Java 類型,轉(zhuǎn)換后的類型最終就會(huì)是 List<JSONObject> ;同理,如果使用 Jackson 作為 JSON 轉(zhuǎn)換工具,不確定具體類型時(shí),最總會(huì)被轉(zhuǎn)換為LinkedHashMap 類型,都需要再使用 TypeReference 來轉(zhuǎn)換一次。
自定義TypeHandler的使用筆記
可通過自定義的TypeHandler實(shí)現(xiàn)某個(gè)屬性在插入數(shù)據(jù)庫以及查詢時(shí)的自動(dòng)轉(zhuǎn)換,本例中是要將Map類型的屬性轉(zhuǎn)化成CLOB,然后存入數(shù)據(jù)庫。由于是復(fù)雜的Map,mp自帶的json轉(zhuǎn)換器會(huì)丟失部分信息。
類型轉(zhuǎn)換器還可以通過注解配置 java類型和jdbc類型
@MappedTypes
:注解配置 java 類型@MappedJdbcTypes
:注解配置 jdbc 類型
定義
@Slf4j @MappedTypes({Object.class}) @MappedJdbcTypes(JdbcType.VARCHAR) public class WeightListTypeHandler ?extends AbstractJsonTypeHandler<Object> { ? ? private static Gson gson = new Gson(); ? ? private final Class<?> type; ? ? public WeightListTypeHandler(Class<?> type) { ? ? ? ? if (log.isTraceEnabled()) { ? ? ? ? ? ? log.trace("WeightListTypeHandler(" + type + ")"); ? ? ? ? } ? ? ? ? Assert.notNull(type, "Type argument cannot be null"); ? ? ? ? this.type = type; ? ? } ? ? @Override ? ? protected Object parse(String json) { ? ? ? ? Type type1 = new TypeToken<Map<String, List<WeightItem>>>(){}.getType(); ? ? ? ? return gson.fromJson(json, type1); ? ? } ? ? @Override ? ? protected String toJson(Object obj) { ? ? ? ? return gson.toJson(obj); ? ? } ? ? public static void setGson(Gson gson) { ? ? ? ? Assert.notNull(gson, "Gson should not be null"); ? ? ? ? WeightListTypeHandler.gson = gson; ? ? } }
使用
注意@TableName 注解 autoResultMap 屬性
@Data @NoArgsConstructor @TableName(value = "mix_target",autoResultMap = true) public class MixTarget extends Model<MixTarget> { @TableId(value = "id", type = IdType.AUTO) private Long id; /** *指標(biāo)描述 */ @TableField("description") private String description; /** * 指標(biāo)名 */ @TableField("name") private String name; /** * 對(duì)應(yīng)屬性名 */ @TableField("property_name") private String propertyName; /** * 起始點(diǎn)類型 */ @TableField("source_type") private String sourceType; /** * 屬性對(duì)應(yīng)權(quán)值列表 * key 屬性名 value指定條件下的權(quán)值 */ @TableField(value = "weight_list",typeHandler = WeightListTypeHandler.class,jdbcType = JdbcType.CLOB) private Map<String, List<WeightItem>> weightList; /** * 運(yùn)行狀態(tài) * 0 新建未運(yùn)行 * 1 運(yùn)行中 * 2 已運(yùn)行 成功 * 3 已運(yùn)行 失敗 */ @TableField("status") private Integer status; /** * 是否可用 * 1 true * 0 false */ @TableField("enable") private Integer enable; @TableField("create_time") private LocalDateTime createTime; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot后端數(shù)據(jù)校驗(yàn)實(shí)戰(zhàn)操作指南
在項(xiàng)?開發(fā)中,對(duì)于前端提交的表單,后臺(tái)接?接收到表單數(shù)據(jù)后,為了保證程序的嚴(yán)謹(jǐn)性,通常后端會(huì)加?業(yè)務(wù)參數(shù)的合法校驗(yàn)操作來避免程序的?技術(shù)性?bug,這篇文章主要給大家介紹了關(guān)于SpringBoot后端數(shù)據(jù)校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2022-07-07AQS(AbstractQueuedSynchronizer)抽象隊(duì)列同步器及工作原理解析
AQS是用來構(gòu)建鎖或者其他同步器組件的重量級(jí)基礎(chǔ)框架及整個(gè)JUC體系的基石,通過內(nèi)置的FIFO對(duì)列來完成資源獲取線程的排隊(duì)工作,并通過一個(gè)int類型變量表示持有鎖的狀態(tài),本文給大家詳細(xì)介紹下AQS抽象隊(duì)列同步器的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-03-03解決IDEA2020.1.2IDEA打不開的問題(最新分享)
由于idea安裝多了某個(gè)jar,點(diǎn)擊出現(xiàn)讀條后閃退情況,接下來通過本文給大家分享解決IDEA2020.1.2IDEA打不開的問題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨小編一起看看吧2020-07-07Spring Boot整合QueryDSL的實(shí)現(xiàn)示例
這篇文章主要介紹了Spring Boot整合QueryDSL的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Spring?Boot統(tǒng)一處理全局異常的實(shí)戰(zhàn)教程
最近在做項(xiàng)目時(shí)需要對(duì)異常進(jìn)行全局統(tǒng)一處理,所以下面這篇文章主要給大家介紹了關(guān)于Spring?Boot統(tǒng)一處理全局異常的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12Java使用異或運(yùn)算實(shí)現(xiàn)簡單的加密解密算法實(shí)例代碼
這篇文章主要介紹了Java使用異或運(yùn)算實(shí)現(xiàn)簡單的加密解密算法實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12使用HttpClient調(diào)用接口的實(shí)例講解
下面小編就為大家?guī)硪黄褂肏ttpClient調(diào)用接口的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Java?ConcurrentHashMap實(shí)現(xiàn)線程安全的代碼示例
眾所周知ConcurrentHashMap是HashMap的多線程版本,HashMap?在并發(fā)操作時(shí)會(huì)有各種問題,而這些問題,只要使用ConcurrentHashMap就可以完美解決了,本文將給詳細(xì)介紹ConcurrentHashMap是如何保證線程安全的2023-05-05IDEA 如何導(dǎo)入別人的javaweb項(xiàng)目進(jìn)行部署
這篇文章主要介紹了IDEA 如何導(dǎo)入別人的javaweb項(xiàng)目進(jìn)行部署,本文給大家分享我的詳細(xì)部署過程及遇到問題解決方法,需要的朋友可以參考下2023-03-03