码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis执行insert返回主键

时间:2020-01-21 15:53:28      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:sel   http   tty   selectkey   记录   java   val   identity   art   

第一种方式(推介):

<!-- 
所有数据库通用,插入成功返回最近一次插入的id
它会将id直接赋值到对应的实体当中
TStudent stu = new TStudent(); studentMapper.add(TStudent ); int pk = stu.getId(); // 这就是我们的主键id --> <insert id="add" parameterType="TStudent" useGeneratedKeys="true" keyProperty="id"> insert into TStudent(name, age) values(#{name}, #{age}) </insert>

  

第二种方式:

<!-- 注意 keyProperty 属性,selectKey 标签,主键是id -->	
<insert id="insertEstimate" parameterType="java.util.Map" useGeneratedKeys="true" keyProperty="id">
<!-- 获取最近一次插入记录的主键值的方式 -->
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
SELECT @@IDENTITY
</selectKey>
insert into test_table(estimate_no) values(#{budgetNo})
</insert>	

  

常用第一种方式进行


原文链接:https://blog.csdn.net/u012489091/article/details/89339437

mybatis执行insert返回主键

标签:sel   http   tty   selectkey   记录   java   val   identity   art   

原文地址:https://www.cnblogs.com/wangsong412/p/12221870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!