欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis中执行多条sql语句的方式

程序员文章站 2022-04-12 23:34:25
一下以mysql 和Oracle为例:mysql 中修改数据库连接 添加&allowMultiQueries=true 多条sql 语句用; 分割即可url: jdbc:mysql://localhost:3306/webdb?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowMultiQu....

一下以mysql 和Oracle为例:

mysql 中修改数据库连接 添加&allowMultiQueries=true  多条sql 语句用; 分割即可

url: jdbc:mysql://localhost:3306/webdb?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true

例如:

 <delete id="clearAlarmInfo" parameterType="java.util.HashMap">
      delete from t_b_alarm_info where id =1;
      delete from t_alarm_speed where id=2;
</delete> 

 

oracle中用 begin   end 实现多条语句的执行 例如:

        <foreach collection="list" open="begin" close=";end;" separator=";" item="item">
            MERGE INTO T_REL_ALARM_USER T1
            using (select #{userId} AS user_id,#{item} AS ALARM_TYPE FROM dual ) T2
            on ( T1.user_id=T2.user_id and T1.alarm_type= T2.alarm_type)
            WHEN MATCHED THEN
            UPDATE SET insert_time=SYSDATE
            WHEN NOT MATCHED THEN
            INSERT (USER_ID,ALARM_TYPE) VALUES (#{userId},#{item})
        </foreach>

注意 foreach参数: open="begin" close=";end;" separator=";" 是关键

本文地址:https://blog.csdn.net/u014282804/article/details/108974782