Mybatis遍历修改和添加

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">

猜你喜欢

转载自blog.csdn.net/luo_Json/article/details/84632143