MySQL database new insert data, need to get the latest inserted ID immediately

Source: Internet
Author: User

In MySQL, use the ID field of the Auto_increment type as the primary key for the table.   The usual practice is through "SELECT Max (ID) from tablename" approach, but obviously this practice needs to consider the concurrency situation, need to in the transaction to the main table with "x lock", to get the value of Max (ID), and then unlock. This approach requires a lot of steps, some trouble, and a bad concurrency. Is there a simpler way to do it? One answer is through the Select last_insert_id () operation. At first glance, it looks like select Max (ID), but it's actually thread-safe. In other words, it is based on database connection, what is the meaning of database connection? Example: (1), in connection 1 to a table insert a record, a table contains a auto_increment type ID. (2), in connection 2 to a table and then insert a record. (3), result: The result of executing select last_insert_id () in connection 1 is different from the result of executing select last_insert_id () in connection 2, and the result of executing select MAX (ID) in two connections is the same.

For the primary key setting in MySQL table I automatically grow, when we insert a record, the ID will automatically grow, and we want this ID, for another table insert, how do we get this ID? Some people get it through Max (ID), which is unreasonable, and if another person happens to insert a record before you perform a select MAX (ID) fetch, its ID is changed. And MySQL does not have the function of lock table, so we can get through last_insert_id.

For MyBatis

1<!--new Apps--2<insert id= "addapplication" parametertype= "Com.bxy.entity.application.Application"3Usegeneratedkeys= "true" keyproperty= "id" >4 INSERT INTO Vrv_application_list5         (6 AppName,7 Apptype,8 IconPath,9 filepath,Ten FileSize, One Pic1 A         ) - Values -         ( the #{appname}, - #{apptype}, - #{iconpath}, - #{filepath}, + #{filesize}, - #{pic1} +         ) A<selectkey resulttype= "Java.lang.Integer" order= "after" atkeyproperty= "id" > - SELECT last_insert_id () -</selectKey>

By the way above, I will return the newly inserted ID, binding back to my entity application, provided that your application entity must have the id attribute. Otherwise it will be an error.
Of course if your parametertype= "Java.util.Map", it is also possible, you do not need to do anything superfluous, will automatically bind to the MAP, for example:

1<!--adding messages--2<insert id= "AddMessage" parametertype= "Java.util.Map"3Usegeneratedkeys= "true" keyproperty= "id" >4 INSERT INTO Vrv_push_message_tab5         (6 type,7 title,8 message,9 create_id,Ten Create_time, One Force_open A         ) - Values -         ( the #{type}, - #{title}, - #{message}, - #{create_id}, + Now (), - #{force_open} +         ) A<selectkey resulttype= "Java.lang.Integer" order= "after" atkeyproperty= "id" > - SELECT last_insert_id () -</selectKey> -</insert>

In Java code, you just need to get it in the following way.

1 int id = addmessage (map);

MySQL database new insert data, need to get the latest inserted ID immediately

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.