Mybatis中如何映射mysql中的JSON字段
數(shù)據(jù)庫mysql中的的某一個字段,存放的是一個List <String>的集合,需要將字段對應(yīng)到entity的某一個參數(shù)上,mapper.xml中使用<id property="abnormalEigenList" column="AbnormalEigen">的方式直接進(jìn)行字段映射時,會出現(xiàn)java.lang.IllegalStateException: No typehandler found for property abnormalEigenList,具體的錯誤:
java.lang.IllegalStateException: No typehandler found for property abnormalEigenList at org.apache.ibatis.mapping.ResultMapping$Builder.validate(ResultMapping.java:151) at org.apache.ibatis.mapping.ResultMapping$Builder.build(ResultMapping.java:140) at org.apache.ibatis.builder.MapperBuilderAssistant.buildResultMapping(MapperBuilderAssistant.java:446) at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:393) at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:280) at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:254) at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:246) at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:119)
這時,我們需要定義一個類,對該json字符串進(jìn)行轉(zhuǎn)義:
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import java.util.ArrayList;
import java.util.List;
/**
* @author:
* @date: 2023-07-02 15:08
* @description: 進(jìn)行json字段的轉(zhuǎn)換
*/
public class JsonHandler extends JacksonTypeHandler {
public JsonHandler (Class<?> type) {
super(type);
}
@Override
protected List<String> parse(String json) {
List<String> jsons = new ArrayList<>();
try {
jsons = JSONObject.parseArray(json, String.class);
} catch (JSONException e) {
jsons.add(JSONObject.parseObject(json, String.class));
}
return jsons;
}
}在mapper.xml中,需要在字段映射時加入typeHandler,具體:<id property="abnormalEigenList" column="AbnormalEigen" typeHandler="com.xxx.config.JsonHandler">
到此這篇關(guān)于Mybatis中映射mysql中的JSON字段的文章就介紹到這了,更多相關(guān)mysql JSON字段內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
集群環(huán)境中使用ehcache_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了集群環(huán)境中使用ehcache的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
SpringBoot+Kotlin中使用GRPC實現(xiàn)服務(wù)通信的示例代碼
本文主要介紹了SpringBoot+Kotlin中使用GRPC實現(xiàn)服務(wù)通信的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
SpringBoot+URule實現(xiàn)可視化規(guī)則引擎的方法示例
規(guī)則引擎其實是一種組件,它可以嵌入到程序當(dāng)中,將程序復(fù)雜的判斷規(guī)則從業(yè)務(wù)代碼中剝離出來,使得程序只需要關(guān)心自己的業(yè)務(wù),而不需要去進(jìn)行復(fù)雜的邏輯判斷,本文給大家介紹了SpringBoot+URule實現(xiàn)可視化規(guī)則引擎的方法示例,需要的朋友可以參考下2024-12-12
Springboot啟動同時創(chuàng)建數(shù)據(jù)庫和表實現(xiàn)方法
這篇文章主要介紹了Springboot啟動同時創(chuàng)建數(shù)據(jù)庫和表,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
SpringBoot+Echarts實現(xiàn)請求后臺數(shù)據(jù)顯示餅狀圖
這篇文章主要介紹了SpringBoot+Echarts實現(xiàn)請求后臺數(shù)據(jù)顯示餅狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12

