最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis中如何映射mysql中的JSON字段

 更新時(shí)間:2023年10月12日 14:35:17   作者:藍(lán)色格子  
在mapper.xml中,需要在字段映射時(shí)加入typeHandler,具體:<id?property="abnormalEigenList"?column="AbnormalEigen"?typeHandler="com.xxx.config.JsonHandler">,下面通過(guò)本文給大家介紹Mybatis中,映射mysql中的JSON字段,需要的朋友可以參考下

數(shù)據(jù)庫(kù)mysql中的的某一個(gè)字段,存放的是一個(gè)List <String>的集合,需要將字段對(duì)應(yīng)到entity的某一個(gè)參數(shù)上,mapper.xml中使用
<id property="abnormalEigenList" column="AbnormalEigen">的方式直接進(jìn)行字段映射時(shí),會(huì)出現(xiàn)java.lang.IllegalStateException: No typehandler found for property abnormalEigenList,具體的錯(cuò)誤:

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í),我們需要定義一個(gè)類,對(duì)該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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

炉霍县| 合肥市| 博罗县| 义乌市| 紫云| 永登县| 兴文县| 郯城县| 福鼎市| 密云县| 景德镇市| 遵化市| 商河县| 紫金县| 城固县| 吉林省| 来凤县| 大邑县| 安远县| 彰化县| 武冈市| 白水县| 温泉县| 上蔡县| 玉林市| 崇信县| 达尔| 兴仁县| 独山县| 桑植县| 蒙城县| 深州市| 米泉市| 余江县| 潼南县| 北宁市| 合山市| 西畴县| 淮北市| 威信县| 平泉县|