MyBatis inserts a piece of data to return the corresponding data ID

Source: Internet
Author: User
Tags unique id

MyBatis used to insert a piece of data.

<insert id= "Insertlatlong" parametertype= "map" >
 insert INTO  gylatlong (UserID, longitude, latitude) 
 VALUES (#{userid}, #{longitude}, #{latitude})
 </insert>

Of course, in fact, he would return 1 int values.
But this int will always be 1. As he returned to the fact that he had affected the number of rows,
When you insert a piece of data, he doesn't return the unique ID that you inserted into his data.

After checking the information on the Internet, we finally know

<insert id= "Inserttaxiorder"   parametertype= "map" usegeneratedkeys= "true" keyproperty= "id" > 

     Insert Into  tb_taxiorderinfo (UserID, DriverID, startaddress, Endaddress,publictime,startlongitude,startlatitude 
      VALUES  (#{userid}, 0, #{startadd}, #{endadd}, GETDATE (), #{longitude}, #{latitude}
      )

Plus usegeneratedkeys= "true" keyproperty= "ID"

That means. After he inserts, the ID of the inserted data will be automatically generated

Into the ID that you set behind this keyproperty.

You can just take the value of the ID after execution.

The ParameterType I received here is map.
So before the map has to have this key

If you receive an entity class, then of course the entity class will define the id attribute.

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.