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. BZOJ 2243: [SDOI2011]染色 (树链剖分+线段树合并)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2243 树链剖分的点剖分+线段树.漏了一个小地方,调了一下午...... 还是要细心啊! 结 ...

  2. Java系的大网站架构-LinkedIn和淘宝

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  3. PostgreSQL的 initdb 源代码分析之十四

    继续分析: /* * Make the per-database PG_VERSION for template1 only after init'ing it */ write_version_fi ...

  4. php7.0 redis扩展下载地址

    http://windows.php.net/downloads/pecl/snaps/redis/20160319/

  5. 使用maven命令建立java项目

    在terminal中输入: mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -Darc ...

  6. Metadata Lock原理3

      http://blog.itpub.net/26515977/viewspace-1208250/   腾讯工程师 随着5.5.3引入MDL,更多的Query被“Waiting for table ...

  7. SSO-Javascript模拟IE登录,不让IIS弹出登录窗口

    解决方案:         用JS模拟IE用户登录,再跳转到对应的系统.   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...

  8. textarea 中的换行符问题

    下面是我对这个问题的解决过程,最后算是完全搞懂了,真是阴沟里险些翻船 1.必须知道textarea中的换行符是 \n  (个人检测发现按回车键是\n,好像在linux下是\r\n) 2.用nl2br之 ...

  9. 一般php创建的文件默认不是utf-8格式的,在网上搜罗的解决办法如下:

    1.PHP本身是无编码的,所有的字符串通常都视为二进制流.因此只需要输入的字符串为Utf-8即可.若字符串采用其他编码,可以使用iconv系列函数转换编码. 2.注$content = iconv(& ...

  10. c语言趣味

    改编java趣味100题,c语言版 1.C语言版金字塔 #include <stdio.h> int main() { int n,i,j,k; scanf("%d", ...