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

mybatis 实现 insert 语句返回 主键

时间:2017-06-29 10:01:28      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:type   bat   ast   select   适用于   主键   记录   返回值   mybatis   

mybatis 中 insertSelective(example) 语句默认返回并不是 插入记录的主键。

我们可以考虑插入记录主键不直接作为 函数的返回值, 而是体现在改变函数参数 "example", 使其主键等于插入记录的主键。

在insertSelective xml定义中,加入以下代码:

<selectKey resultType="Integer" keyProperty="id" >
  SELECT last_insert_id() as id;
</selectKey>

这样就完成了对与参数改变。

 

注: 该方法只适用于 mysql 数据库!

 

mybatis 实现 insert 语句返回 主键

标签:type   bat   ast   select   适用于   主键   记录   返回值   mybatis   

原文地址:http://www.cnblogs.com/yxmfighting/p/7092614.html

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