mybatis Annotation How to Implement Object Batch Change

  • 2021-10-25 06:43:54
  • OfStack

mybatis Annotation Object Bulk Change

1. Introduction

When there are multiple objects that need to be changed, batch modify the object collection List

2. Code


@Update("<script>"
+ "<foreach collection='listUserAnswerRecord' item='item' open='' close=''  separator=';'> "
+ " update t_qb_record_201910"
+ " set answered = 0, progress = 1, answer_sheet = null, gmt_update = #{item.gmtUpdate}"
+ " <where>"
+ "<choose>"
+ "<when test='item.unionid !=null'> unionid=#{item.unionid}</when>"
+ "<otherwise> openid= #{item.openid} </otherwise>"
+ "</choose>"
+ " and goods_id = #{item.goodsId} and charpter_id = #{item.charpterId} and type = #{item.type}"
+ "</where>"
+ "</foreach>"
+ "</script>")
Integer deleteUserAnswerSheet(@Param("listUserAnswerRecord") List<UserAnswerRecordNew> listUserAnswerRecord);

mybatis Annotation Bulk Update, Insert


// Bulk insertion 
	@Insert({
        "<script>",
        "insert into table(column1, column2) values ",
        "<foreach collection='userLists' item='item' index='index' separator=','>",
        "(#{item.column1}, #{item.column2} )",
        "</foreach>",
        "</script>"
	})
	public int insertUsers(@Param(value="userLists") List<User> userLists);
// Batch update 
@Update({
		"<script>",
		"<foreach collection='userLists' item='item' index='index' separator=';'>",
		"update table b",
		"set b.column1= #{item.column1} , b.column2= #{item.column2}  where b.column3= #{item.column3}",
		"</foreach>",
		"</script>"
    })
    public int updateUser(@Param(value="userLists") List<User> userLists);

collection : The collection you sent

item : Classes inside

index Is the i of the for loop

separator : Spacer


Related articles: