龙空技术网

mybatis单个方法执行多条sql语句

it村男主任 277

前言:

现在小伙伴们对“mysql逗号隔开拆成多条数据”可能比较重视,看官们都想要剖析一些“mysql逗号隔开拆成多条数据”的相关资讯。那么小编也在网络上网罗了一些关于“mysql逗号隔开拆成多条数据””的相关文章,希望同学们能喜欢,小伙伴们快快来学习一下吧!

前言

方法中过多的连接数据库会导致请求响应慢。我们可以连接一次数据库,执行多条sql语句。

mybatis默认支持mapper中单个方法执行多条sql语句,针对不同数据库,写法不同。

Mysql写法

数据库连接中添加 allowMultiQueries=true

spring.datasource.url=jdbc:mysql://localhost:3306/data_test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true

Mapper中多个sql用逗号隔开

<insert id="saveAllUserJurisdict" useGeneratedKeys="false"><foreach item="menuid" index="index" collection="menuidList" >select * from ts_online_user where id = #{menuid};</foreach></insert><delete id="deleteUserById" parameterType="String">delete from sys_user_role where userId=#{id};delete from sys_user where id=#{id};delete from sys_user_role_relation where id=#{id};</delete>
Oracle写法

数据库连接无额外配置,mapper中的多个sql语句使用事务形式编写,注意end后面分号不能省略。

<foreach item="menuid" index="index" collection="menuidList" separator=";" close=";end;" open="begin">MERGE INTO T_ONLE_JURISDICTION A USING (select b.user_id,#{menuid,jdbcType=VARCHAR} as quanxianid from T_ONLE_USER B<where><if test="vipTag!=null and vipTag!=''"><choose><when test="vipTag=='Y'.toString()">VIP_TAG = 'Y'</when><when test="vipTag=='N'.toString()">VIP_TAG = 'N'</when><otherwise></otherwise></choose></if></where>) CON (A.user_id=c.user_id and c.quanxianid = a.menu_id)WHEN NOT MATCHED THENINSERT(a.jurisdiction_id , a.user_id , a.menu_id , a.operator_id , a.operator_time)VALUES(f_get_unique(), C.user_id,C.quanxianid,#{jurisdict.operatorId,jdbcType=VARCHAR},(select sysdate from dual))</foreach>

你的赞和关注是对我最大的肯定,希望大家多多支持,谢谢大家。

标签: #mysql逗号隔开拆成多条数据 #c执行sql语句 #oraclesql取最新10条 #oracle中拼接出分号 #mysql多条sql