根据json串直接入库
生活随笔
收集整理的這篇文章主要介紹了
根据json串直接入库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?得到一串json串之后,直接把json解析成map,然后調動公共入庫方法進行存庫操作
service層:保存單個json對象:{}
Map resultData = (Map)JSON.parse(result);//對json內層的{}或者[]進行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"); //需要保存的數據 saveMap.put("data", resultData); //入庫邏輯 flowMapper.saveData(saveMap);?service層:保存數組json對象:[{}]
List<Map> valueList = (List<Map>)JSON.parse(result);if (CollectionUtils.isNotEmpty(valueList) && valueList.size() > 0){for (Map map : valueList) {//對json內層的{}或者[]進行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"); //取一個對象的key,作為insert SQL 的數據庫字段名 saveMap.put("key", valueList.get(0).keySet().toArray()); //數據集合 saveMap.put("data", valueList);//入庫邏輯 flowMapper.saveDataList(saveMap);Dao層
//json為單個對象:{} int saveData(@Param("map") Map<String, Object> map);//json為集合:[{}] int saveDataList(@Param("map") Map<String, Object> map);Mybatis
<!-- 使用#{}點位符時, 不要使用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>總結
以上是生活随笔為你收集整理的根据json串直接入库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2.3 深度学习开发任务实例
- 下一篇: 微服务[面临的挑战]