Java Mybatis针对于Oracle表的插入更新操作merge into tablename using

近期公司产品提出需求,表数据根据主键判断插入或更新,有则更新无则插入,类似于hibernate的saveorupdate方法。对于Mybatis来说Mapping.xml怎么写呢,针对于oracle数据库的merge into tablename using语法,代码如下:

"insertList" parameterType="java.util.List">
    MERGE INTO SE_BMS T1
    USING
    (
     "list" item="item" index="index" separator="union" > 
         SELECT 
             #{item.Student_id,jdbcType=VARCHAR} Student_id,
             #{item.Student_name,jdbcType=VARCHAR}Student_name,
             #{item.Student_age,jdbcType=VARCHAR} Student_age,
             #{item.Student_sex,jdbcType=INTEGER} Student_sex
             FROM DUAL
     
    ) T
    ON
    (
      T1.Student_id = T.Student_id 
    )
    WHEN MATCHED THEN

           UPDATE SET 
             Student_name= T.Student_name,
             Student_age= T.Student_age,
             Student_sex= T.Student_sex

    WHEN NOT MATCHED THEN
            INSERT(Student_id,Student_name,Student_age,Student_sex)
            VALUES(
             T.Student_id,
             T.Student_name,
             T.Student_age,
             T.Student_sex
            )

根据T1.Student_id = T.Student_id去判断唯一性。有则执行update,没有则执行WHEN NOT MATCHED THEN后面的插入语句。

你可能感兴趣的:(Java Mybatis针对于Oracle表的插入更新操作merge into tablename using)