Mybatis中利用foreach批量更新数据


1.xml中书写的方式

  

<update id="batchUpdate" parameterType="list">
         update songs
            <trim prefix="set" suffixOverrides=",">
                 <trim prefix="path =case" suffix="end,">
                     <foreach collection="list" item="i" index="index">
                             <if test="i.songid!=null">
                                  when songid=#{i.songid} then #{i.path}
                             </if>
                     </foreach>
                  </trim>
             </trim>
            where songid in 
            <foreach collection="list"  item="i" index="index" open="(" separator="," close=")">
              #{i.songid}
          </foreach>
    </update>

2.Mapper中方法

void batchUpdate(List<Songs> recordList);

3.打印出来的sql格式

UPDATE songs 
SET path =
CASE  SONGID 
      WHEN '10800537' THEN    'C:/songs/10800537.mpg'
      WHEN '10800118' THEN    'C:/songs/10800118.mpg'
END    
WHERE   SONGID IN ('10800537','10800118')

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM