1. 程式人生 > 其它 >Oracle merge into插入和修改資料

Oracle merge into插入和修改資料

技術標籤:oracleoracle

Oracle merge into插入和修改資料

目錄

語法:

Mybatis:

單個插入:

多個插入:

總結:


在使用oracle資料庫,需要操作資料,增加和刪除。使用merge into 的語法可以一個語句搞定。

語法:

MERGE INTO [target-table] A USING [source-table sql] B ON([conditional expression] and [...]...)

WHEN MATCHED THEN

 [UPDATE sql]

WHEN NOT MATCHED THEN

 [INSERT sql]

虛擬例子

MERGE INTO tableX x

 USING (SELECT 1222 AS id, 'yan' AS name FROM dual) y

 ON ( x.id=y.id)

 WHEN MATCHED THEN

 UPDATE SET x.name = y.name

 WHEN NOT MATCHED THEN

 INSERT (id,name) VALUES(y.id,y.id);

實際例子

merge INTO report_show_cfg a

using (select 5 as cfgId,

 1 as classId,

 '0BF' as isPage,

 10 as pageSize

 from dual) b

on (a.CFG_ID = b.cfgId and a.CLASS_ID = b.classId)

when matched then

 update

 set a.IS_PAGE = b.isPage,

 a.PAGE_SIZE = b.pageSize

-- 這邊就不用寫判斷條件了,上面on 裡面有

-- where a.CFG_ID = b.cfgId and a.CLASS_ID = b.classId

when not matched then

 insert

 (a.CFG_ID,

 a.CLASS_ID,

 a.IS_PAGE,

 a.PAGE_SIZET)

 values

 (REPORT_SHOW_CFG_SEQ.nextval,

 b.classId,

 b.isPage,

 b.pageSize)

瞭解了語法,操作起來比較簡單,那對應mybatis裡面的xml怎麼寫呢?

Mybatis:

單個插入:

Mapper:

void mergeReportShowCfg(@Param("param") ReportShowCfg reportShowCfg);

顯象宣告引數,養成好習慣

Xml:

<update id="mergeReportShowCfg" parameterType=" report.entity.ReportShowCfg" databaseId="oracle">

 merge into sys_report_show_cfg a using (
 SELECT #{param.cfgId,jdbcType=BIGINT} as cfgId, #{param.classId,jdbcType=BIGINT} as classId,
 #{param.isPage,jdbcType=VARCHAR} as isPage, #{param.pageSize,jdbcType=INTEGER} as pageSize FROM dual
 ) b on (
 a.CFG_ID= b.cfgId
 AND a.CLASS_ID= b.classId
 )
 when matched then
 UPDATE SET
 a.IS_PAGE = b.isPage,
 a.PAGE_SIZE = b.pageSize
 # 這邊就不用寫判斷條件了,上面on 裡面有 
# where a.CFG_ID= b.cfgId AND a.CLASS_ID= b.classId
 when not matched then
 INSERT (a.CFG_ID, a.CLASS_ID, a.IS_PAGE, a.PAGE_SIZE)
 values (
SYS_REPORT_SHOW_CFG_SEQ.nextval,b.classId,b.isPage,b.pageSize )
</update>

#{param.cfgId,jdbcType=BIGINT} 宣告引數的資料型別

databaseId="oracle" 指定使用什麼資料庫。 在多資料庫中會用到,比如可能用mysql資料,pg資料庫等

多個插入:

Mapper:

void mergeReportParamBach(@Param("reportParamList") List<ReportParam> reportParamList);

Xml:

<update id="mergeSysReportParamBach" databaseId="oracle">

merge into report_param a using (
 <foreach collection="reportParamList" index="index" item="item" open=""
 close="" separator="union all">
 SELECT #{item.paramId,jdbcType=BIGINT} as paramId, #{item.paramName,jdbcType=VARCHAR} as paramName,
 #{item.sortId,jdbcType=INTEGER} as sortId, #{item.classId,jdbcType=BIGINT} as classId FROM dual
 </foreach>
 ) b on (
 a.PARAM_ID= b.paramId
 AND a.CLASS_ID= b.classId
 )
 when matched then
 UPDATE SET
 a.PARAM_NAME = b.paramName,
a.SORT_ID = b.sortId
# 這邊就不用寫判斷條件了,上面on 裡面有 
# where a.PARAM_ID= b.paramId AND .CLASS_ID= b.classId
 when not matched then
 INSERT (a.PARAM_ID, a.PARAM_NAME,a.SORT_ID, a.CLASS_ID)
 values (
 SYS_REPORT_PARAM_SEQ.nextval,b.paramName,b.sortId,b.classId
 )

</update>

總結:

使用oracle資料庫,merge into 可以一起處理增加修改的操作。

在mybatis注意引數的顯象宣告和宣告資料型別。

Xml裡面中指定databaseId 可以根據實際情況,動態讀取資料庫。 具體情況參考資料操作,使用不同資料庫處理方式