<updateid="deleteList"> update article_info set articleStatus =3 where id in <foreachcollection="list"index="index"item="item"open="("separator=","close=")"> #{item} </foreach> </update>
<updateid="batchUpdate"> update article_info <trimprefix="set"> <trimprefix=" articleKind = case "suffix=" end, "> <foreachcollection="list"item="item"> when id = #{item.id} then #{item.articleKind} </foreach> </trim> 更多字段可以直接添加,end后面的逗号需要保留 <trimprefix=" checkUser = case "suffix=" end "> <foreachcollection="list"item="item"> when id = #{item.id} then #{item.checkUser} </foreach> </trim> </trim> <where> id in <foreachcollection="list"item="item"open="("separator=","close=")"index="index"> #{item.id} </foreach> </where> </update>
上述代码转换成 sql 如下:
1 2 3 4 5 6 7 8 9 10 11
update article_info set articleKind = case when id = #{item.id} then #{item.articleKind} //此处应该是<foreach>展开值 ... end, checkUser = case when id = #{item.id} then #{item.checkUser} //此处应该是<foreach>展开值 ... end where id in (...);
<selectid="getArticleByIdList"resultType="com.xxx.ArticleInfo"> select * from article_info where isValid=1 <iftest="list != null and list.size() != 0"> and grade in <foreachcollection="list"index="index"open="("close=")"item="id"separator=","> #{id} </foreach> </if> </select>