Native JDBC Mode:
Statement.getGeneratedKeys()
Example:
Statement stmt =NULL; ResultSet RS=NULL;Try { // //Create A Statement instance that we can use for//' normal ' result sets assuming you have a//Connection ' conn ' to a MySQL database already//availablestmt=conn.createstatement (); // //Issue the DDL queries for the table for this example//Stmt.executeupdate ("DROP TABLE IF EXISTS autoinctutorial"); Stmt.executeupdate ("CREATE TABLE autoinctutorial (" + "Prikey INT not NULL auto_increment," + "DataField VARCHAR (64), PRIMARY KEY (Prikey)) "); // //Insert one row that'll generate an AUTO INCREMENT//key in the ' Prikey ' field//Stmt.executeupdate ("INSERT into Autoinctutorial (DataField)" + "values (' Can I Get the Auto Increment Field? ')", Statement.return_generated_keys); // //Example of using Statement.getgeneratedkeys ()//To retrieve the value of an auto-increment//value// intAutoinckeyfromapi =-1; RS=Stmt.getgeneratedkeys (); if(Rs.next ()) {Autoinckeyfromapi= Rs.getint (1); } Else { //throw an exception from here} System.out.println ("Key returned from Getgeneratedkeys ():" +autoinckeyfromapi);} finally { if(rs! =NULL) { Try{rs.close (); } Catch(SQLException ex) {//Ignore } } if(stmt! =NULL) { Try{stmt.close (); } Catch(SQLException ex) {//Ignore } }}
There are also useSELECT LAST_INSERT_ID() 注意:并发可能会出现问题。示例:
Statement stmt =NULL; ResultSet RS=NULL;Try { // //Create A Statement instance that we can use for//' normal ' result sets.stmt=conn.createstatement (); // //Issue the DDL queries for the table for this example//Stmt.executeupdate ("DROP TABLE IF EXISTS autoinctutorial"); Stmt.executeupdate ("CREATE TABLE autoinctutorial (" + "Prikey INT not NULL auto_increment," + "DataField VARCHAR (64), PRIMARY KEY (Prikey)) "); // //Insert one row that'll generate an AUTO INCREMENT//key in the ' Prikey ' field//Stmt.executeupdate ("INSERT into Autoinctutorial (DataField)" + "values (' Can I Get the Auto Increment Field? ')"); // //Use the MySQL last_insert_id ()//function to do the same thing as Getgeneratedkeys ()// intAutoinckeyfromfunc =-1; RS= Stmt.executequery ("Select last_insert_id ()"); if(Rs.next ()) {Autoinckeyfromfunc= Rs.getint (1); } Else { //throw an exception from here} System.out.println ("Key returned from" + "' Select last_insert_id () ':" +autoinckeyfromfunc);} finally { if(rs! =NULL) { Try{rs.close (); } Catch(SQLException ex) {//Ignore } } if(stmt! =NULL) { Try{stmt.close (); } Catch(SQLException ex) {//Ignore } }}
The MyBatis package is configured as follows:
<id= "Insert" parametertype= "Post" usegeneratedkeys = "true" Keyproperty = "id" >
Call
postDao.add(post);
Returns 1 after the same result as before, using Post.getid () to obtain the self-increment ID.
Reference documents:
"1" http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-last-insert-id.html
"2" Http://stackoverflow.com/questions/12241260/get-auto-genearated-key-for-the-inserted-record-in-mybatis
MySQL method to get the value of the self-increment ID