Mybatis注解方式操作Oracle数据库详解

时间:2021-05-20

1.新增多行数据

@Insert({"<script>insert all " + "<foreach collection=\"list\" index=\"index\" item=\"item\" open=\"\" separator=\"\" close=\"\">" + " into s_user (user,username) values " + "(#{item.user},#{item.username}) " + "</foreach>" + " select 1 from dual " + "</script>"})

2.执行多条SQL语句

特别注意:open属性设置为begin,close设置为;end;

@Update({"<script>" + "<foreach collection=\"list\" separator=\";\" item=\"item\" open=\"begin\" close=\";end;\">" + "update s_user set user=#{item.user},username=#{item.username} where id=#{item.id}" + "</foreach>" + "</script>"})

ORACLE中通过begin...end;来执行多行sql语句,各条sql之间用;分割

补充知识:使用mybatis注解批量插入Oracle数据库与批量插入MySQL数据库区别

批量插入MySQL语法

insert into table_name values (column01, column02, ...), (value01, value02, ...),

(value01, value02, ...);

例子:

@Select({"<script>" , "insert into t_sales_target (area_id, area_name, shop_id, shop_name, year) values " , " <foreach collection='saleTargetList' item='item' index='index' separator=','>", " (", " #{item.areaId},", " #{item.areaName},", " #{item.shopId},", " #{item.shopName},", " #{item.year}," " )", " </foreach>", "</script>" } )

批量插入Oracle语法

insert into table_name (column01, column02, ...) select * from ( select value01, value02, ... from dual union select value01, value02, ... from dual union select value01, value02, ... from dual )

例子:

@Insert({"<script>", "insert into t_sales_date_amount (amount, sale_date, shop_id, area_id) select A.* from (", " <foreach collection='salesDateAmountList' item='item' index='index' separator='UNION ALL'>", " SELECT", " #{item.salesAmount},", " #{item.salesDateTime},", " #{item.shopId},", " #{item.areaId}", " from dual", " </foreach>", " )A", "</script>" })

以上这篇Mybatis注解方式操作Oracle数据库详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章