根据json串直接入库
生活随笔
收集整理的這篇文章主要介紹了
根据json串直接入库
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?得到一串json串之后,直接把json解析成map,然后調(diào)動(dòng)公共入庫(kù)方法進(jìn)行存庫(kù)操作
service層:保存單個(gè)json對(duì)象:{}
Map resultData = (Map)JSON.parse(result);//對(duì)json內(nèi)層的{}或者[]進(jìn)行toString() resultData.put("allLevelCount",resultData.get("allLevelCount")==null?null:resultData.get("allLevelCount").toString());Map<String, Object> saveMap = new HashMap<>(); //表名 saveMap.put("tableName", "rec_bd_flow_nodeChildCount_data"); //需要保存的數(shù)據(jù) saveMap.put("data", resultData); //入庫(kù)邏輯 flowMapper.saveData(saveMap);?service層:保存數(shù)組json對(duì)象:[{}]
List<Map> valueList = (List<Map>)JSON.parse(result);if (CollectionUtils.isNotEmpty(valueList) && valueList.size() > 0){for (Map map : valueList) {//對(duì)json內(nèi)層的{}或者[]進(jìn)行toString()map.put("linkStates",map.get("linkStates")==null? null:map.get("linkStates").toString());map.put("baiduEvents",map.get("baiduEvents")==null?null:map.get("baiduEvents").toString()); }Map<String, Object> saveMap = new HashMap<>(); //表名 saveMap.put("tableName", "rec_bd_flow_alarmList_data"); //取一個(gè)對(duì)象的key,作為insert SQL 的數(shù)據(jù)庫(kù)字段名 saveMap.put("key", valueList.get(0).keySet().toArray()); //數(shù)據(jù)集合 saveMap.put("data", valueList);//入庫(kù)邏輯 flowMapper.saveDataList(saveMap);Dao層
//json為單個(gè)對(duì)象:{} int saveData(@Param("map") Map<String, Object> map);//json為集合:[{}] int saveDataList(@Param("map") Map<String, Object> map);Mybatis
<!-- 使用#{}點(diǎn)位符時(shí), 不要使用statementType="STATEMENT"聲明 --><insert id="saveData" parameterType="java.util.HashMap">insert into ${map.tableName} (<foreach collection="map.data" item="value" index="key" separator=",">`${key}`</foreach>, `paramId`)values (<foreach collection="map.data" item="value" index="key" separator=",">#{value}</foreach>)</insert><insert id="saveDataList" parameterType="java.util.HashMap">insert into ${map.tableName} (<foreach collection="map.key" item="value" separator=",">`${value}`</foreach>, `paramId`)values<foreach collection="map.data" item="line" separator=",">(<foreach collection="line" index="key" item="value" separator=",">#{value}</foreach>)</foreach></insert>總結(jié)
以上是生活随笔為你收集整理的根据json串直接入库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2.3 深度学习开发任务实例
- 下一篇: 微服务[面临的挑战]