MyBatis實現SaveOrUpdate,saveorupdate

來源:互聯網
上載者:User

MyBatis實現SaveOrUpdate,saveorupdate
MyBatis實現SaveOrUpdate

這篇文章主要講如何通過xml方式實現SaveOrUpdate,但是仍然建議在Service中實現。

例子
<insert id="saveOrUpdate" >  <selectKey keyProperty="count" resultType="int" order="BEFORE">    select count(*) from country where id = #{id}  </selectKey>  <if test="count > 0">    update country     set countryname = #{countryname},countrycode = #{countrycode}     where id = #{id}  </if>  <if test="count==0">    insert into country values(#{id},#{countryname},#{countrycode})  </if></insert>
條件限制

根據不同的判斷邏輯,會有所不同,就上面這個例子而言,就要求實體類中包含count屬性(可以是別的名字)。否則selectKey的結果沒法儲存,如果入參是個Map類型,就沒有這個限制。

說明

從例子來看除了有個限制外,也沒別的麻煩。

通過selectKey做第一次查詢,然後根據結果進行判斷,所以這裡的order="BEFORE"是必須的。

也是因為BEFORE,所以沒法通過<bind>標籤來臨時儲存中間的值,只能在入參中增加屬性來存放。

測試代碼
//資料庫中已經存在該ID,但是countryname=ChinaCountry country = new Country();country.setId(35);country.setCountryname("中國");country.setCountrycode("CN");//由於存在,這裡會updateint result = countryMapper.saveOrUpdate(country);//查詢結果,判斷是否已經改變Country c2 = countryMapper.selectById(35);assertEquals("中國",c2.getCountryname());//id=300的不存在c2 = countryMapper.selectById(300);assertNull(c2);//將id=300country.setId(300);//由於id=300不存在,這裡會Insertresult = countryMapper.saveOrUpdate(country);//查詢結果c2 = countryMapper.selectById(300);assertNotNull(c2);
輸出日誌
DEBUG ==>  Preparing: select count(*) from country where id = ? DEBUG ==> Parameters: 35(Integer)TRACE <==    Columns: C1TRACE <==        Row: 1DEBUG <==      Total: 1DEBUG ==>  Preparing: update country set countryname = ?,countrycode = ? where id = ? DEBUG ==> Parameters: 中國(String), CN(String), 35(Integer)DEBUG <==    Updates: 1DEBUG ==>  Preparing: select * from country where id = ? DEBUG ==> Parameters: 35(Integer)TRACE <==    Columns: ID, COUNTRYNAME, COUNTRYCODETRACE <==        Row: 35, 中國, CNDEBUG <==      Total: 1DEBUG ==>  Preparing: select * from country where id = ? DEBUG ==> Parameters: 300(Integer)DEBUG <==      Total: 0DEBUG ==>  Preparing: select count(*) from country where id = ? DEBUG ==> Parameters: 300(Integer)TRACE <==    Columns: C1TRACE <==        Row: 0DEBUG <==      Total: 1DEBUG ==>  Preparing: insert into country values(?,?,?) DEBUG ==> Parameters: 300(Integer), 中國(String), CN(String)DEBUG <==    Updates: 1DEBUG ==>  Preparing: select * from country where id = ? DEBUG ==> Parameters: 300(Integer)TRACE <==    Columns: ID, COUNTRYNAME, COUNTRYCODETRACE <==        Row: 300, 中國, CNDEBUG <==      Total: 1
最後

這種方式只是利用了selectKey會多執行一次查詢來實現的,但是如果你同時還需要通過selectKey擷取序列或者自增的id,就會麻煩很多(oracle麻煩,其他支援自增的還是很容易)。

建議在複雜情況下,還是選擇在Service中實現更好。

MyBatis工具:www.mybatis.tk

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.