Mybatis中如何映射mysql中的JSON字段
數(shù)據(jù)庫mysql中的的某一個字段,存放的是一個List <String>的集合,需要將字段對應(yīng)到entity的某一個參數(shù)上,mapper.xml中使用<id property="abnormalEigenList" column="AbnormalEigen">的方式直接進(jìn)行字段映射時(shí),會出現(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)
這時(shí),我們需要定義一個類,對該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中,需要在字段映射時(shí)加入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)文章
在eclipse中使用SVN的實(shí)現(xiàn)方法(圖文教程)
這篇文章主要介紹了在eclipse中使用SVN的實(shí)現(xiàn)方法(圖文教程),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Java8中stream流的collectingAndThen方法應(yīng)用實(shí)例詳解
Java8中的Stream流提供了collectingAndThen方法,用于對歸納結(jié)果進(jìn)行二次處理,文章通過User類的數(shù)據(jù)填充,演示了如何使用該方法進(jìn)行集合去重、查找最高工資員工、計(jì)算平均工資等操作,感興趣的朋友跟隨小編一起看看吧2025-03-03
java BASE64Encoder詳細(xì)介紹及簡單實(shí)例
這篇文章主要介紹了java BASE64Encoder詳細(xì)介紹及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01
redisson.tryLock()參數(shù)的使用及理解
這篇文章主要介紹了redisson.tryLock()參數(shù)的使用,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
idea自帶Jacoco/idea自動測試語句覆蓋率方法(使用詳解)
這篇文章主要介紹了idea自帶Jacoco/idea自動測試語句覆蓋率方法,本文給大家分享使用方法,通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04

