原 mybatis 批量新增和批量修改
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1335
方法一:适用于list中每个字段都不为空的情况
<insert id="batchInsertUser" parameterType="java.util.List">
insert into m_user(name, nick_name, phone) values
<foreach collection="userList" item="item" index="index" separator=",">
(
#{item.name,jdbcType=VARCHAR},
#{item.nickName,jdbcType=VARCHAR},
#{item.phone,jdbcType=VARCHAR}
)
</foreach>
</insert>
方法二:适用于list中存在字段为空,如果为空则不进行新增的情况
<insert id="batchInsert" parameterType="java.util.List">
<foreach collection="list" item="item" separator=";">
insert into public.pmf_brand_achievement(id, system_resource, data_year,
<if test='null!=item.allCurrentCount and item.allCurrentCount!=""'>
all_current_count,
</if>
<if test='null!=item.allTotalCount and item.allTotalCount!=""'>
all_total_count,
</if>
create_time)
values
(
#{item.id,jdbcType=BIGINT},
#{item.systemResource,jdbcType=VARCHAR},
#{item.dataYear,jdbcType=VARCHAR},
<if test='null!=item.allCurrentCount and item.allCurrentCount!=""'>
#{item.allCurrentCount,jdbcType=VARCHAR},
</if>
<if test='null!=item.allTotalCount and item.allTotalCount!=""'>
#{item.allTotalCount,jdbcType=VARCHAR},
</if>
#{item.createTime,jdbcType=TIMESTAMP}
)
</foreach>
</insert>
<update id="updateBatchProduct" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";">
update d_product set
name=#{item.numStandard,jdbcType=VARCHAR},
product_sys=#{item.productSys,jdbcType=VARCHAR}
where product_no=#{item.productNo,jdbcType=VARCHAR}
</foreach>
</update>
备注:批量修改的时候需要在jdbc连接后面加上:allowMultiQueries=true,因为默认是不支持批处理的
jdbc:mysql://ip:3306/ms?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=true&requireSSL=false&serverTimezone=GMT%2B8
2021-08-23 14:39:17 阅读(922)
名师出品,必属精品 https://www.91mszl.com
博主信息