详解mybatis插入数据后返回自增主键ID的问题
作者:charlyFeng 时间:2021-09-04 19:08:00
1.场景介绍:
开发过程中我们经常性的会用到许多的中间表,用于数据之间的对应和关联.这个时候我们关联最多的就是ID,我们在一张表中插入数据后级联增加到关联表中.我们熟知的mybatis在插入数据后返回的是插入成功的条数,那么这个时候我们想要得到相应的这条新增数据的ID,该怎么办呢?
2.插入数据返回自增主键ID方法(一)
在映射器中配置获取记录主键值xml映射:
在xml中定义useGeneratedKeys为true,返回主键id的值,keyProperty和keyColumn分别代表数据库记录主键字段和java对象成员属性名
<!-- 插入数据:返回记录主键id值 -->
<insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into stu (name,age) values (#{name},#{age})
</insert>
接口映射器
在接口映射器中通过注解@Options
分别设置参数useGeneratedKeys,keyProperty,keyColumn值
// 返回主键字段id值
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@Insert("insert into stu (name,age) values (#{name},#{age})")
void insert(Student stu);
获取新添加记录主键字段值
需要注意的是,在MyBatis中添加操作返回的是记录数并非记录主键id。因此,如果需要获取新添加记录的主键值,需要在执行添加操作之后,直接读取Java对象的主键属性。
Integer rows = sqlSession.getMapper(StuMapper.class).insertOneTest(student);
System.out.println("rows = " + rows); // 添加操作返回记录数
System.out.println("id = " + student.getId()); // 执行添加操作之后通过Java对象获取主键属性值
添加批量记录时返回主键ID
如果希望执行批量添加并返回各记录主键字段值,只能在xml映射器中实现,在接口映射器中无法做到。
<!-- 批量添加数据,并返回主键字段 -->
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into stu (name,age) values
<foreach collection="list" separator="," item="t">
(#{t.name},#{t.age})
</foreach>
</insert>
可以看到,执行批量添加并返回记录主键值的xml映射器配置,跟添加单条记录时是一致的。不同的地方仅仅是使用了foreach元素构建批量添加语句。
来源:https://www.cnblogs.com/charlypage/p/11253610.html
标签:mybatis,插入数据,主键,ID
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 静默安装实现方法
2021-08-04 05:11:25
Java PreparedStatement用法详解
2023-08-08 20:20:51
![](https://img.aspxhome.com/file/2023/1/58061_0s.png)
Android Bitmap的加载优化与Cache相关介绍
2022-02-04 17:20:51
Android内存优化杂谈
2023-06-18 15:46:41
老生常谈java中cookie的使用
2023-11-11 04:37:59
Java设计模式之Builder建造者模式
2021-12-16 07:21:53
![](https://img.aspxhome.com/file/2023/0/68180_0s.png)
spring boot如何使用POI读取Excel文件
2022-09-19 21:26:33
![](https://img.aspxhome.com/file/2023/1/68201_0s.png)
RocketMQ消息过滤与查询的实现
2023-06-26 10:04:25
![](https://img.aspxhome.com/file/2023/0/68700_0s.jpg)
C# 嵌入dll 的方法
2022-11-06 08:39:10
![](https://img.aspxhome.com/file/2023/1/116591_0s.png)
Springboot引用外部配置文件的方法步骤
2022-06-14 13:29:35
![](https://img.aspxhome.com/file/2023/0/92710_0s.jpg)
C# 4.0 大数的运算--BigInteger的应用详解
2022-02-02 06:40:05
![](https://img.aspxhome.com/file/2023/2/74832_0s.png)
C语言代码实现三子棋小游戏
2023-04-16 02:12:41
![](https://img.aspxhome.com/file/2023/6/112306_0s.jpg)
浅谈java多线程wait,notify
2023-11-29 16:29:03
使用controller传boolean形式值
2023-11-28 23:05:33
使用注解@Validated和BindingResult对入参进行非空校验方式
2022-09-16 11:30:44
![](https://img.aspxhome.com/file/2023/5/66215_0s.png)
JVM类加载,垃圾回收
2022-08-16 02:44:36
![](https://img.aspxhome.com/file/2023/1/61771_0s.png)
java:程序包org.springframework.boot不存在的完美解决方法
2023-09-03 20:37:32
![](https://img.aspxhome.com/file/2023/3/84163_0s.jpg)
java理论基础Stream管道流状态与并行操作
2021-08-31 14:24:07
![](https://img.aspxhome.com/file/2023/6/87206_0s.jpg)
Java面向对象编程(封装/继承/多态)实例解析
2023-11-11 11:33:09
Java Mybatis批量修改封装详解
2021-12-29 22:41:52