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>

猜你喜欢

转载自www.cnblogs.com/qingmuchuanqi48/p/10934965.html