MyBatis, batch add, modify, delete

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/sanyuesan0000/article/details/19998727

Recently need to use the MyBatis batch new Oracle database, just started on the Internet to find the method is to update MySQL, try to find not suitable for Oracle, and later found that the correct Oracle batch new SQL is:

<InsertID= "Insertattractionsbatch"ParameterType= "Java.util.List">INSERT INTO Attractions (ID, NAME, longitude, LATITUDE, Update_time)<foreachCollection= "List"Item= "Item"Index= "Index"Separator= "UNION All" >(select #{item.id,jdbctype=varchar}, #{item.name,jdbctype=varchar}, #{item.longitude,jdbctype=decimal}, #{item . Updatetime,jdbctype=timestamp} from Dual)</foreach></Insert>

It is important to note that there is no values in SQL, and in the <foreach> tab (Selece ... from dual), the SQL in MySQL is this:

New:

<InsertID= "Insertattractionsbatch"ParameterType= "Java.util.List">INSERT INTO Attractions (ID, NAME, longitude, LATITUDE, Update_time)<foreachCollection= "List"Item= "Item"Index= "Index"Separator= "UNION All" >#{item.id,jdbctype=varchar}, #{item.name,jdbctype=varchar}, #{item.longitude,jdbctype=decimal}, #{ Item.updatetime,jdbctype=timestamp}</foreach></Insert>

Oracle updates cannot be done in a normal way, as needed:

<UpdateID= "Updateattractionsbatch"ParameterType= "Java.util.List">begin<foreachCollection= "List"Item= "Item"Index= "Index"Separator=";" >Update Attractions<Set>            <ifTest= "Item.id!=null and item.id!=">id = #{item.id},</if>            <ifTest= "Item.head!=null and item.head!=">HEAD = #{item.head},</if>            </Set>WHERE id = #{item.id}</foreach>; end; </Update>

The deletion is the same as MySQL:

<DeleteID= "Deleteattractions"ParameterType= "Java.util.List">Delete from Attractions<where>  <foreachCollection= "List"Index= "Index"Item= "Item"Open="("Separator= "or"Close=")">Id=#{item.id}</foreach>  </where>  </Delete>


MyBatis, batch add, modify, delete

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.