1. 程式人生 > 其它 >mybatis批量更新,和修改出現的小問題

mybatis批量更新,和修改出現的小問題

在連線資料庫的配置url中要加入?allowMultiQueries=true這段

然後mapper層就如下寫

最後mapper.xml就是正常的寫法,解釋一下,我的collection="list",為什麼寫list,因為傳入的是一個list集合,這裡必須寫list,

如果傳入一個數組比如Integer[],那麼就要寫collection="array"

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <updateid="updateStudentAnswer" parameterType="java.util.List">
<iftest="list!=null"> <foreachcollection="list" item="studentAnswer" index= "index" open="" close="" separator =";"> update studentanswerinfo <set> SAnswer=#{studentAnswer.SAnswer}, Getpoint=#{studentAnswer.Getpoint}, other=#{studentAnswer.other} </set> <where> questionID=#{studentAnswer.questionID}
</where> </foreach> </if> </update>
    <!--查詢狀態後同步 -->
    <update id="updateTaskInfo" parameterType="java.util.List">

            <foreach collection="list" item="TmpTableTaskInfoDTO" index="index" open="" close="" separator=";">
                update mapping_integration.task_info
                
<set> project_name = #{TmpTableTaskInfoDTO.projectId}, task_status_desc = #{TmpTableTaskInfoDTO.status}, create_person = #{TmpTableTaskInfoDTO.submitUser}, flow = #{TmpTableTaskInfoDTO.flowId}, finished_time = #{TmpTableTaskInfoDTO.endTime} </set> <where> exec_id = #{TmpTableTaskInfoDTO.execId} </where> </foreach> </update>

連線資料庫的開頭加入?allowMultiQueries=true 時,還是一直報這個錯誤

Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';

今天在Stack Overflow上找到了另外一種解決辦法,也不需要在連線資料庫的url上加入?allowMultiQueries=true。原來出現這種原因,主要是批量插入list時,mysql是拼接sql語句,之前這樣寫,拼接會出現values後面會有空格等mysql不識別的sql語句。需要如下拼接

1 2 3 4 5 6 7 8 <insert id="insertRecords"parameterType="java.util.List"> replaceintobi_staff_skill_information (staff_id, skill_id, skill_level)values <foreachitem="staffSkillInfo"index="index"collection="list" open="("separator="),("close=")"> #{staffSkillInfo.staffId},#{staffSkillInfo.skillId},#{staffSkillInfo.skillLevel} </foreach> </insert>