日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

mysql 获取自增id的值的方法

發(fā)布時間:2025/7/14 68 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 获取自增id的值的方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原生jdbc方式:

Statement.getGeneratedKeys()

示例:

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// available 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 will 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// int autoIncKeyFromApi = -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 }} }

也有使用SELECT LAST_INSERT_ID() 注意:并發(fā)可能會出現(xiàn)問題。示例:

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 will 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()// int autoIncKeyFromFunc = -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 }} }

?

mybatis封裝后的配置如下:

<insert id="insert" parameterType="Post" useGeneratedKeys="true" keyProperty="id">

調(diào)用

postDao.add(post);

和以前一樣結(jié)果后返回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

《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的mysql 获取自增id的值的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。