MyBatis Implementation Saveorupdate

Source: Internet
Author: User

MyBatis Implementation Saveorupdate

This article is mainly about how to implement Saveorupdate in XML, but it is still recommended to implement it in the service.

Example
<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>
Condition limits

Depending on the logic of the judgment, it will be different, as the above example requires that the entity class contain count attributes (which can be other names). Otherwise selectKey The result cannot be saved, if the entry is a Map type, there is no such limit.

Description

From the example, there is no other problem but a limitation.

The first query is made by Selectkey and then judged according to the result, so here order="BEFORE" is the necessary.

Because of this, it is not possible to BEFORE <bind> temporarily store the middle value through the tag, only add the attribute in the entry parameter to store.

Test code
the ID already exists in the database, but Countryname=chinaCountry Country =NewCountry (); Country.setid ( *); Country.setcountryname ("China"); Country.setcountrycode ("CN");//Due to presence, this will updateintresult = Countrymapper.saveorupdate (country);//Query results to determine if changes have been madeCountry C2 = Countrymapper.selectbyid ( *); Assertequals ("China", C2.getcountryname ());//id=300 doesn't exist.C2 = Countrymapper.selectbyid ( -); Assertnull (C2);//Will id=300Country.setid ( -);//Because id=300 does not exist, this will insertresult = Countrymapper.saveorupdate (country);//Query ResultsC2 = Countrymapper.selectbyid ( -); Assertnotnull (C2);
Output log
DEBUG ==> Preparing:SelectCOUNT (*) fromCountrywhereid =? DEBUG ==> Parameters: *(Integer) TRACE <== columns:c1trace <== Row:1DEBUG <== Total:1DEBUG ==> Preparing:update CountrySetCountryName =?, CountryCode =?whereid =? DEBUG ==> Parameters: China (String), CN (String), *(Integer) DEBUG <== Updates:1DEBUG ==> Preparing:Select* fromCountrywhereid =? DEBUG ==> Parameters: *(Integer) TRACE <== Columns:id, CountryName, Countrycodetrace <== Row: *, China, Cndebug <== total:1DEBUG ==> Preparing:Select* fromCountrywhereid =? DEBUG ==> Parameters: -(Integer) DEBUG <== Total:0DEBUG ==> Preparing:SelectCOUNT (*) fromCountrywhereid =? DEBUG ==> Parameters: -(Integer) TRACE <== columns:c1trace <== Row:0DEBUG <== Total:1DEBUG ==> Preparing:insert intoCountry VALUES (?,?,?) DEBUG ==> Parameters: -(Integer), China (String), CN (String) DEBUG <== Updates:1DEBUG ==> Preparing:Select* fromCountrywhereid =? DEBUG ==> Parameters: -(Integer) TRACE <== Columns:id, CountryName, Countrycodetrace <== Row: -, China, Cndebug <== total:1
At last

This approach only takes advantage of Selectkey to execute a query more than once, but if you also need to get sequences or self-increasing IDs through Selectkey, it can be a lot of trouble (Oracle trouble, other support self-increasing or very easy).

It is recommended that you do better in complex situations or in service implementations.

MyBatis Tools: www.mybatis.tk

MyBatis Implementation Saveorupdate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.