mybatis通過關鍵條件查詢后有就修改沒有新增


轉自:https://www.cnblogs.com/Springmoon-venn/p/8519592.html

mybatis 使用merge into,跟一般的update寫法相同:

<update id="mergeinfo">
        merge into user_type a
        using ( select #{name} as name, #{type} as type from dual ) b
        on (a.type = b.type)
        when not matched then
        insert  (type,name) values(#{type},#{name})
        when matched then
        update set name = #{name} where type = #{type}
</update>

傳入字段 name、type,使用 “select #{name} as name, #{type} as type from dual”,構建一個子查詢建別名 “b”,

目標表“a” 的type字段,與子查詢表“b”的type字段比較:

  如果不匹配:

    執行 :“insert (type,name) values(#{type},#{name})”

  如果匹配:

    執行:“update set name = #{name} where type = #{type}”

主要on 后面的括號不能省


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM