方法一: mapper.java Integer insertDep(Department department); xml select LAST_INSERT_ID()
mapper.java
Integer insertDep(Department department);
xml
select LAST_INSERT_ID() insert into sys_department(name,parentId,enabled,isParent) VALUES(#{name},#{parentId},1,0)
其中:
selecTKEy标签:将插入到数据库的某条记录的主键,返回到指定对象(user)对应属性中。
keyProperty: 指定返回的主键,存储在对象中(user)的哪个属性
order:相对于insert语句,selectKey标签中的sql的执行顺序。由于Mysql的自增原理,执行完insert语句之后才将主键生成,所以这里selectKey的执行顺序为after。
resultType: 返回的主键对应的JAVA类型
LAST_INSERT_ID(): 是mysql的函数,返回auto_increment自增列新记录id值。
使用:
useGeneratedKeys="true" keyProperty="departmentId"
insert into sys_department(departmentId,name,parentId,enabled,isParent) VALUES(uuid(),#{name},#{parentId},1,0)
来源地址:https://blog.csdn.net/w13966597931/article/details/128323675
--结束END--
本文标题: mybatis中Insert语句如何返回插入的主键
本文链接: https://www.lsjlt.com/news/423826.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0