Mybatis裏面實現多條語句
通常用在刪除主表信息同時刪除子表信息。如果利用多次Dao進行執行sql,程序就寫起來麻煩並且閱讀難度會提升。
(刪除income表中的信息,同時刪除子表income_detail表中的相關信息)
delete from income_detail where income_id=#{id};
delete from income where id=#{id};
或者是批量更新,比如利用foreach批量update多條數據。
<update id="update">
<foreach collection="xxList" item="item" index="index" open="" close="" separator=";">
update t_xxx
<set>
xxx = #{item.xxx}
</set>
where id = #{item.id}
</foreach>
</update>
這些語句的類似點在於都是在mybatis中帶有分號的多條sql。而直接執行又會報錯,所以我們需要在jdbc連接中加上allowMultiQueries參數,設置爲true。
<property name="url" value="jdbc:mysql://localhost:3306/amoeba?characterEncoding=UTF-8&allowMultiQueries=true"/>
jdbc.url=jdbc:mysql://localhost:3306/amoeba?characterEncoding=UTF-8&allowMultiQueries=true