原生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() 注意:并发可能会出现问题。示例:

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">

调用

postDao.add(post);

和以前一样结果后返回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

最新文章

  1. 提交数据url太长导致提交失败
  2. Windows Azure Backup Agent安装注意事项
  3. dos 下 查看和设置classpath的命令
  4. TI BLE CC2541的SPI主模式
  5. HDU 4539 郑厂长系列故事――排兵布阵(曼哈顿距离)
  6. EasyUI学习笔记
  7. POJ -- 3842
  8. History Grading
  9. 固定textview大小,根据文字多少调整字体自适应textview大小
  10. GUI(主)线程与子线程之间的通信(用信号槽通讯)
  11. 关于css3的calc()
  12. CentOS 6.X启动流程
  13. 【interview】Microsoft面经
  14. UNIX网络编程(卷1)——学习过程中遇到的新词语
  15. Android Studio 加载网络图片
  16. Eureka微服务ID
  17. CentOS上部署JAVA服务【转】
  18. from表单文件上传后页面跳转解决办法
  19. HTML页面中显示HTML标签&lt;xmp&gt;
  20. 【Python】Pycharm2018激活方式【亲测好用】

热门文章

  1. Domino服务器SSL的配置录像
  2. PHP的数组分为两种类型,一种是索引数组,一种是关联数组。有如下关联数组,我们如何获取它的第一个key和value呢?
  3. 【CS Round #37 (Div. 2 only) A】Boring Number
  4. HttpComponents入门解析
  5. java设计模式--事件监听器模式(观察者模式)
  6. KendoUi中KendoDropDownList控件的使用——三级级联模块的实现
  7. Unity3D:粒子系统Particle System
  8. Vue绑定事件
  9. Linux下MySQL导入导出数据库
  10. 67.nodejs取参四种方法req.body,req.params,req.param,req.body