Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解
1.新增多行數(shù)據(jù)
@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.執(zhí)行多條SQL語句
特別注意:open屬性設(shè)置為begin,close設(shè)置為;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;來執(zhí)行多行sql語句,各條sql之間用;分割
補充知識:使用mybatis注解批量插入Oracle數(shù)據(jù)庫與批量插入MySQL數(shù)據(jù)庫區(qū)別
批量插入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數(shù)據(jù)庫詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java數(shù)學(xué)工具類Math詳解(round方法)
這篇文章主要為大家詳細介紹了java數(shù)學(xué)工具類Math,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08