mybatis通过<foreach>拼接sql批量更新数据(where条件和更新对应字段都是动态变化处理的)条件和更新字段都是通过java代码传过来的

 xml代码如下:

    <select id="updateQuestionHistoryAnswer">
        update  et_user_question_history
        set  error_book =
        <foreach collection="updateAll" item="item" index="index" separator=" " open="case question_id" close="end">
            when #{item.questionId} then #{item.errorBook}
        </foreach>
        , is_right =
        <foreach collection="updateAll" item="item" index="index" separator=" " open="case question_id" close="end">
            when #{item.questionId} then #{item.isRight}
        </foreach>
        where
        user_id =  #{userId } and  question_id in
        <foreach item="item" index="index" collection="updateAll" open="("  close=")" separator=",">
             #{item.questionId}
        </foreach>
    </select>

参数配置如下:

 mapper代码

void updateQuestionHistoryAnswer(@Param("updateAll")List<QuestionHistoryAnswer> updateAll, @Param("userId")Integer userId );

实体类代码

@Data
public class QuestionHistoryAnswer {
	private int questionId;
	private boolean isRight;
	private int userId;
	private String createTime;
	private String updateTime;
	private boolean errorBook;
}

猜你喜欢

转载自blog.csdn.net/qq_36961226/article/details/121249377