Mybatis遍历修改和添加

本文详细解析了MyBatis框架下如何进行批量插入和更新操作,特别关注使用list作为参数的数据处理方式,通过XML配置文件实现高效的数据批量操作。

https://note.youdao.com/share/?id=056b64b482370c18ccfb5f7f8dda3687&type=note#/

mybaits传参问题(传入list),批量插入

在这个批量插入的语句中,collection="list" 这是死的,不能更改的,不管在DAO传什么参数,只要是list类型的都可以用,item="item"是遍历这个list的一个引用对像。

<!-- 批量 ,传入一个list list为传进来的对像集合, item为对像的引用-->

<insert id="insertBatch" >

insert into student ( <include refid="Base_Column_List" /> )

values

<foreach collection="list" item="item" index="index" separator=",">

(null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId})

</foreach>

</insert>

<include refid="Base_Column_List" />

这句话的意思是在那个xml文件中会有一个以Base_Column_List为ID的SQL语句

<select id="Base_Column_List">name,type,,,字段N</select>

 

<!-- 批量更新 ,传入一个list -->

<update id="listuptAbnormity">

update jc_abnormal_detail

<trim prefix="set" suffixOverrides=",">

<trim prefix="channel_id =case" suffix="end,">

<foreach collection="list" item="i" index="index">

<if test="i.channelId!=null">

when id = #{i.abnormityId} then #{i.channelId}

</if>

</foreach>

</trim>

<trim prefix="end_time =case" suffix="end,">

<foreach collection="list" item="i" index="index">

<if test="i.endTime!=null">

when id = #{i.abnormityId} then DATE_FORMAT(#{i.endTime},'%Y-%m-%d %H:%i:%s')

</if>

</foreach>

</trim>

</trim>

</update>

在以上的批量更新时,需要注意的是:1.when是作为更新条件的;2.then是传进来对像中的某一个值 把对像的值赋给 等于case的值 。<trim prefix="channel_id =case">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值