MyBatis insert insert back from growth ID

Source: Internet
Author: User
How MySQL returns the ID after inserting data
The first method is to use the last_insert_id in the Mybtias framework to use selectkey this node, and set the Insert return value of the type of integer, you can return this ID value. For example: In fact, in the Mybtias framework using the Selectkey node, and set the Insert return value of the type of integer, you can return this ID value.
<insert id= "Add" parametertype= "Com.aspire.aplus.entity.model.AddPO" >
<selectkey resulttype= "Java.lang.Integer" keyproperty= "id" >
SELECT last_insert_id ()
</selectKey>
Insert into T_test (' name ') VALUES (' Lu Xing ');
</insert>
Note: Be sure to perform the insert operation before select LAST_INSERT_ID (), or you will not get the last ID you inserted.
Mysql> SELECT last_insert_id ();     The resulting IDs are saved on the server after each connection. This means that the value returned by the function to a given client is the first Auto_increment value that the client produces for the most recent statement that affects the Auto_increment column. This value cannot be affected by other clients, even if they produce their own auto_increment values. This behavior guarantees that you will be able to retrieve your ID without worrying about the activities of other clients and that you do not need to lock or handle it.         Each mysql_query operation on the MySQL server can be understood as an "atomic" operation, write operations often need to lock the table, is the MySQL Application Server lock table is not our application lock table.       It is noteworthy that if you insert more than one record at a time, this function returns the ID value of the first record.       Because LAST_INSERT_ID is based on connection, as long as each thread uses a separate connection object, the LAST_INSERT_ID function returns that connection to Auto_ Increment the latest insert or update* as the ID of the first record that was generated. This value cannot be affected by other clients (Connection), which guarantees that you will be able to retrieve your ID without worrying about the activities of other clients and that you do not need to lock it. Inserts multiple records using a single INSERT statement, &NBSP;LAST_INSERT_ID returns a list.       LAST_INSERT_ID is table-Independent, last_insert_id will change if you insert data into table A and then insert the data into form B.   Method The second is to use Max (ID)   Use last_insert_id is the basis of the connection, if you change a window when the call will always return ten   if not frequent insertion we can also use this method to get the return ID value   1 Select Max (ID) from user; The disadvantage of this method is that it is not suitable for high concurrency. The value returned when inserting at the same time may not be accurate.   Method Three is to create a stored procedure, in the stored procedure to invoke the first insert to get the maximum value of the Operation   1 DELIMITER $$ 2 DROP proceDure IF EXISTS ' test ' $$ 3 CREATE definer= ' root ' @ ' localhost ' PROCEDURE ' test ' (in name varchar (MB), out OID int) 4 BEGIN 5   INSERT INTO User (LoginName) values (name); 6   SELECT MAX (ID) from user into OID; 7   Select OID; 8 End $$ 9 DELIMITER; 1 Call Test (' GG ', @id);
Method four uses @ @identity 1 SELECT @ @IDENTITY @ @identity is the most recent time to insert data into a table with the identity attribute (that is, the self-added column), which is a system-defined global variable. General system-defined global variables begin with @@ 开头 and user-defined variables begin with @. For example, there is a table A, its own column is ID, when inserting a row of data into table A, if the value of the self added column automatically increases to 101 after inserting the data, the value obtained by the SELECT @ @identity is 101. The use of the @ @identity is provided that after the insert operation, the connection is not closed when the SELECT @ @identity is performed, otherwise the null value is obtained. Method Five is to use Getgeneratedkeys () Connection conn =; Serializable ret = null; PreparedStatement state =.; ResultSet Rs=null; A try {state.executeupdate () () (), State.getgeneratedkeys (), or if (Rs.next ()) {ret = (Ser ializable) Rs.getobject (1); One} catch (SQLException e) {} return ret; To sum up, getting IDs after inserting in MySQL can be very error-prone in high concurrency. In addition LAST_INSERT_ID is based on session but does not know why the test is not successful. If you are Hunan's welcome to join Hunan People in Shenzhen-java Group: 557651502

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.