MyBatis(3.2.3) - Mapped statements: The INSERT statement, Autogenerated keys
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的更多相关文章
- Spring+Mybatis整合报错Mapped Statements collection does not contain value原因之一
报错如下: ### Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements coll ...
- Springboot+Mybatisplus替换mybatis整合报错Mapped Statements collection does not contain value
问题一: mybatisPlus完全兼容mybatis,一般来说直接替换掉就可以了,如果mybatis的数据源不能取消创建的话,就注掉mybatisplus的数据源 //@Configurationp ...
- mybatis启动报错Mapped Statements collection already contains value for com.autoyol.mapper.trans.TransDispatchingMapper解决
1.检查sqlsession配置,在applicationContext文件中.检查mybatis配置文件. 2.检查TransDispatchingMapper.java 是接口类,无注解. 3.T ...
- Mybatis 保错:Mapped Statements collection already contains value for jaxrs.dch.projects.y
原因是mapper.xml中定义了相同的两个方法
- 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 ...
- 错误: Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for studentDao.insert
详细错误信息: org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: java. ...
- 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 ...
- 搭建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 ...
- 报错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 ...
随机推荐
- upload.php --->文件上传
<?php header("Content-type:text/html;charset=utf-8"); print_r($_FILES['file']); $filena ...
- UVaLive 6859 Points (几何,凸包)
题意:给定 n 个点,让你用最长的周长把它们严格包围起来,边长只能用小格子边长或者是小格子对角线. 析:先把每个点的上下左右都放到一个集合中,然后求出一个凸包,然后先边长转成题目的方式,也好转两个点的 ...
- HDU 2050 折线分割平面 (递推)
题意:略. 析:多写几个就找到规律了,第1条是2,2条时是7个,3条时是16,4条时是29,.... 那么规律就出来了2 * n * n + 1 - n; 也可以递推,第n条折线的两条边都与前n-1条 ...
- freemaker自定义分页控件实现
<link href="${res}/css/pages-jhdb.css" rel="stylesheet" type="text/css&q ...
- freemaker遍历嵌套list的map
<#if disMap?exists> <#list disMap?keys as key> <#if ((disMap[key]))??&&((disM ...
- 关于在Eclipse中使用正则表达式替换的一点记录(使用正则表达式的分组)
今天在工作中遇到了点替换的麻烦事,由于数据类进行了变动,具体情况是这样的,需要将下面的代码: player.skillData[i].name 替换为: player.skillData.getSki ...
- MemCacheManager
#region Fields private AreaRepository _areaRepository = new AreaRepository(); private ICacheManager ...
- AngularJS~集成的ajax和服务的注入
AngularJS很美,以至于迷倒了不少年青人和我这位大叔,它的美不仅仅是在写法上,而且在设计方法上都进乎于完美,用什么服务就注入什么服务,这样方法本来就很直观,程序员感觉直观了,程序在运行起来也按需 ...
- Uva 3767 Dynamic len(set(a[L:R])) 树套树
Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...
- c# socket通信较完善方案
c#的socket通信应用.文件较多.附件为工程. core AbstractBytesWorker.cs 字节工作器(基类),用于用于同一不同功能的字节工作器 BinaryHand.c ...