Mybatis批量添加JSONArray(JSONArray里是JSONObject)

这篇博客主要展示了如何使用Java进行数据库操作,包括批量保存数据内容到`dynamic_excel_content`表以及更新已有数据。通过遍历JSON数组,将内容转化为Map并插入到List中,然后利用MyBatis的动态SQL实现批量插入和更新操作。更新时,首先检查是否存在数据,如果存在则根据ID更新对应内容。
摘要由CSDN通过智能技术生成

1.void saveContentData(List<Map<String, String>> list) throws Exception;

 //保存数据内容
        List<Map<String, String>> contentList = new ArrayList<>();
        Map<String, String> content = null;
        //获取数据(JSONArray里是JSONObject)
        JSONArray contentArray = (JSONArray) pd.get("content");
        //把Json转为String
        for (Object data : contentArray) {
        	content =new HashMap<>();
            content.put("bid", pd.get("bid") + "");
            content.put("content", JSON.toJSONString(data, SerializerFeature.QuoteFieldNames));
            contentList.add(content);
        }

 <insert id="saveContentData" parameterType="List">
        INSERT INTO `dynamic_excel_content`(bid, content) VALUES
         <foreach collection="list" index="index" item="item" separator=",">
             <foreach collection="item" index="key" item="value" open="(" close=")" separator=",">
                 #{value}
             </foreach>
         </foreach>
     </insert>

2.void updateBatch(List list) throws Exception;
批量更新数据

//保存数据内容
        List<Map<String, String>> contentList = new ArrayList<>();
        List<PageData> list = new ArrayList<>();
        Map<String, String> content = null;
        PageData p = null;

        //获取数据(JSONArray里是JSONObject)
        JSONArray contentArray = (JSONArray) pd.get("content");
        
		//判断之前是否有数据
        Integer[] ids = dynamicExcelContentMapper.getDynamicExcelContentIds(pd);
        if (ids.length > 0) {
            for (int i = 0, len = contentArray.size(); i < len; i++) {
                p = new PageData();
                p.put("dynamic_contentid", ids[i]);
                p.put("content", JSON.toJSONString(contentArray.getJSONObject(i), SerializerFeature.QuoteFieldNames));
                list.add(p);
            }
            dynamicExcelContentMapper.updateBatch(list);
        }
       
	<update id="updateBatch" parameterType="list">
        update dynamic_excel_content
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="content =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.content !=null and item.content != ''">
                        when dynamic_contentid = #{item.dynamic_contentid} then #{item.content}
                    </if>
                </foreach>
            </trim>
        </trim>
        where dynamic_contentid in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.dynamic_contentid}
        </foreach>
    </update>

update dynamic_excel_content set content =case 
when dynamic_contentid = 'ff' then '22'
when dynamic_contentid = 'ff' then '33'
end
where dynamic_contentid in (143, 146)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值