springboot+Mybatis+MySql 一個update標簽中執行多條update sql語句


MySql默認是不支持這種騷操作的,但是並不代表不能實現,只需要在jdbc的配置文件中稍做配置:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/airipo?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
username=imp
password=123

其中,allowMultiQueries=true這個配置是關鍵,必須寫

然后在映射文件中的標簽下將多條sql用;隔開即可,示例代碼:

<insert id="addUser" parameterType="User" >
      insert into t_users (name,password,phone) values (#{name}, #{password},#{phone});
      insert into t_dep (depname) values (#{depname})
 </insert>

<update id="updateBatchSingle" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close=";" separator=";">
update user
<set>
status=#{item.status}
</set>
where id = #{item.id}
</foreach>
</update>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM