We can use the useGeneratedKeys and keyProperty attributes to let the database generate the auto_increment column value and set that generated value into one of the input object properties as follows:

<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="studId">
INSERT INTO STUDENTS(NAME, EMAIL, PHONE) VALUES(#{name},#{email},#{phone})
</insert>

Here the STUD_ID column value will be autogenerated by MySQL database, and the generated value will be set to the studId property of the student object.

StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
mapper.insertStudent(student);

Now you can obtain the STUD_ID value of the inserted STUDENT record as follows:

int studentId = student.getStudId();

Some databases such as Oracle don't support AUTO_INCREMENT columns and use SEQUENCE to generate the primary key values.

Assume we have a SEQUENCE called STUD_ID_SEQ to generate the STUD_ID primary key values. Use the following code to generate the primary key:

<insert id="insertStudent" parameterType="Student">
<selectKey keyProperty="studId" resultType="int" order="BEFORE">
SELECT ELEARNING.STUD_ID_SEQ.NEXTVAL FROM DUAL
</selectKey>
INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL, PHONE) VALUES(#{studId},#{name},#{email},#{phone})
</insert>

Here we used the <selectKey> subelement to generate the primary key value and stored it in the studId property of the Student object. The attribute order="BEFORE" indicates that MyBatis will get the primary key value, that is, the next value from the sequence and store it in the studId property before executing the INSERT query.

We can also set the primary key value using a trigger where we will obtain the next value from the sequence and set it as the primary key column value before executing the INSERT query.

If you are using this approach, the INSERT mapped statement will be as follows:

<insert id="insertStudent" parameterType="Student">
INSERT INTO STUDENTS(NAME,EMAIL, PHONE) VALUES(#{name},#{email},#{phone})
<selectKey keyProperty="studId" resultType="int" order="AFTER">
  SELECT ELEARNING.STUD_ID_SEQ.CURRVAL FROM DUAL
</selectKey>
</insert>

MyBatis(3.2.3) - Mapped statements: The INSERT statement, Autogenerated keys的更多相关文章

  1. Spring+Mybatis整合报错Mapped Statements collection does not contain value原因之一

    报错如下: ### Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements coll ...

  2. Springboot+Mybatisplus替换mybatis整合报错Mapped Statements collection does not contain value

    问题一: mybatisPlus完全兼容mybatis,一般来说直接替换掉就可以了,如果mybatis的数据源不能取消创建的话,就注掉mybatisplus的数据源 //@Configurationp ...

  3. mybatis启动报错Mapped Statements collection already contains value for com.autoyol.mapper.trans.TransDispatchingMapper解决

    1.检查sqlsession配置,在applicationContext文件中.检查mybatis配置文件. 2.检查TransDispatchingMapper.java 是接口类,无注解. 3.T ...

  4. Mybatis 保错:Mapped Statements collection already contains value for jaxrs.dch.projects.y

    原因是mapper.xml中定义了相同的两个方法

  5. mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map

    用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...

  6. 错误: Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for studentDao.insert

    详细错误信息: org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: java. ...

  7. mybatis报错Mapped Statements collection does not contain value for com.inter.IOper

    首页 > 精品文库 > mybatis报错Mapped Statements collection does not contain value for com.inter.IOper m ...

  8. 搭建Mybatis 出现 Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mapper.BatchCustomer.findBatchCustomerOneToOne

    Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection doe ...

  9. 报错Mapped Statements collection does not contain value for com.atguigu.mybatis.dao.EmployeeMapperPlus

    报错Mapped Statements collection does not contain value for com.atguigu.mybatis.dao.EmployeeMapperPlus ...

随机推荐

  1. CodeForces 706B Interesting drink (二分查找)

    题意:给定 n 个数,然后有 m 个询问,每个询问一个数,问你小于等于这个数的数有多少个. 析:其实很简单么,先排序,然后十分查找,so easy. 代码如下: #pragma comment(lin ...

  2. http协议详细介绍

    HTTP协议/IIS 原理及ASP.NET运行机制浅析[图解] 转自:http://www.cnblogs.com/wenthink/archive/2013/05/06/HTTP_IIS_ASPNE ...

  3. 红帽 Enterprise Linux OpenStack Platform 4.0全面上市

    十一月,红帽公司推出Red Hat Enterprise Linux OpenStack Platform 4.0测试版,这款企业级解决方案集Red Hat Enterprise Linux的稳定性与 ...

  4. Windows下sqlmap的使用_01

    环境:win8.1 64位    一.下载 首先,需下载SqlMap以及适用于Windows系统的Python.下载地址如下:   1.1.SqlMap下载地址:https://github.com/ ...

  5. window.open()打开窗口的几种方式

    1. window.open("http://www.baidu.com/", "_search");//在一个新的窗口打开百度,并且使URL地址出现在搜索栏中 ...

  6. Meta键盘

    由于著名的编辑器Emacs中用到Meta键,但如今大多国人所用键盘上实际并无此键,想必多有不明之处,故多方收集资料撰写此文,简要描述了Meta键及相关键盘的发展始末,至于在Emacs上如何使用国人键盘 ...

  7. hadoop namenode又一次格式化以后hbase的hmaster进程启动后立即消失

    hadoop的 namenode又一次格式化以后.重新启动hbase.发现它的hmaster进程启动后立即消失,查看一大堆日志,最后在zookeeper的日志里发现例如以下问题 Unable to r ...

  8. Chrome Apps将可以打包成iOS或Android应用

    Chrome Apps 将可以在 iOS 和 Android 设备上独立运行了.开发者只要使用 Google今天 提供的工具集(toolchain)将自己的 Web App 打包,并将生成的应用上传到 ...

  9. BW知识点总结及面试要点

    1.       如何理解数据仓库? 数据仓库 是 一个面向主题的,集成的,相对稳定的,反应历史变化的数据集合,用于支持管理决策. 2.       OLAP 和 OLTP的基本概念 和 区别? Ol ...

  10. SAP BW 例程(Routine)【开始例程、关键值或特性的例程、结束例程】

    定义 可以使用例程定义关键值或特性的复杂的转换规则. 例程是本地 ABAP 类,它们包括预定义的定义和实施范围.进站和出站参数的 TYPES及方法签名都存储在定义范围中.实际例程创建于实施范围中.使用 ...