原生jdbc方式:
示例:
Statement stmt = = <span style="color: #0000ff;">try<span style="color: #000000;"> {
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Create a Statement instance that we can use for
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 'normal' result sets assuming you have a
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Connection 'conn' to a MySQL database already
</span><span style="color: #008000;">//</span><span style="color: #008000;"> available</span>
<span style="color: #000000;">
stmt =<span style="color: #000000;"> conn.createStatement();
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Issue the DDL queries for the table for this example
</span><span style="color: #008000;">//
<span style="color: #000000;">
stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial"<span style="color: #000000;">);
stmt.executeUpdate(
"CREATE TABLE autoIncTutorial ("
- "priKey INT NOT NULL AUTO_INCREMENT,"
- "dataField VARCHAR(64),PRIMARY KEY (priKey))"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Insert one row that will generate an AUTO INCREMENT
</span><span style="color: #008000;">//</span><span style="color: #008000;"> key in the 'priKey' field
</span><span style="color: #008000;">//
<span style="color: #000000;">
stmt.executeUpdate(
"INSERT INTO autoIncTutorial (dataField) "
- "values ('Can I Get the Auto Increment Field?')"<span style="color: #000000;">,Statement.RETURN_GENERATED_KEYS);
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Example of using Statement.getGeneratedKeys()
</span><span style="color: #008000;">//</span><span style="color: #008000;"> to retrieve the value of an auto-increment
</span><span style="color: #008000;">//</span><span style="color: #008000;"> value
</span><span style="color: #008000;">//
<span style="color: #0000ff;">int autoIncKeyFromApi = -1<span style="color: #000000;">;
rs </span>=<span style="color: #000000;"> stmt.getGeneratedKeys();
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (rs.next()) {
autoIncKeyFromApi </span>= rs.getInt(1<span style="color: #000000;">);
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> throw an exception from here</span>
<span style="color: #000000;"> }
System.out.println(</span>"Key returned from getGeneratedKeys():"
+<span style="color: #000000;"> autoIncKeyFromApi);
} <span style="color: #0000ff;">finally<span style="color: #000000;"> {
</span><span style="color: #0000ff;">if</span> (rs != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
rs.close();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SQLException ex) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> ignore</span>
<span style="color: #000000;"> }
}
</span><span style="color: #0000ff;">if</span> (stmt != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
stmt.close();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SQLException ex) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> ignore</span>
<span style="color: #000000;"> }
}
}
也有使用
Statement stmt = = <span style="color: #0000ff;">try<span style="color: #000000;"> {
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Create a Statement instance that we can use for
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 'normal' result sets.</span>
<span style="color: #000000;">
stmt =<span style="color: #000000;"> conn.createStatement();
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Issue the DDL queries for the table for this example
</span><span style="color: #008000;">//
<span style="color: #000000;">
stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial"<span style="color: #000000;">);
stmt.executeUpdate(
"CREATE TABLE autoIncTutorial ("
- "priKey INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (priKey))"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Insert one row that will generate an AUTO INCREMENT
</span><span style="color: #008000;">//</span><span style="color: #008000;"> key in the 'priKey' field
</span><span style="color: #008000;">//
<span style="color: #000000;">
stmt.executeUpdate(
"INSERT INTO autoIncTutorial (dataField) "
- "values ('Can I Get the Auto Increment Field?')"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Use the MySQL LAST_INSERT_ID()
</span><span style="color: #008000;">//</span><span style="color: #008000;"> function to do the same thing as getGeneratedKeys()
</span><span style="color: #008000;">//
<span style="color: #0000ff;">int autoIncKeyFromFunc = -1<span style="color: #000000;">;
rs = stmt.executeQuery("SELECT LAST_INSERT_ID()"<span style="color: #000000;">);
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (rs.next()) {
autoIncKeyFromFunc </span>= rs.getInt(1<span style="color: #000000;">);
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> throw an exception from here</span>
<span style="color: #000000;"> }
System.out.println(</span>"Key returned from " +
"'SELECT LAST_INSERT_ID()': " +<span style="color: #000000;">
autoIncKeyFromFunc);
} <span style="color: #0000ff;">finally<span style="color: #000000;"> {
</span><span style="color: #0000ff;">if</span> (rs != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
rs.close();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SQLException ex) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> ignore</span>
<span style="color: #000000;"> }
}
</span><span style="color: #0000ff;">if</span> (stmt != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
stmt.close();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SQLException ex) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> ignore</span>
<span style="color: #000000;"> }
}
}
mybatis封装后的配置如下:
调用
和以前一样结果后返回1,使用post.getId()可以获取到自增的id。
参考文献:
【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 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|