如何在带有注释的mysql中使用mybatis返回插入内容上的id [英] How to return ids on Inserts with mybatis in mysql with annotations

查看:661
本文介绍了如何在带有注释的mysql中使用mybatis返回插入内容上的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 查看Postgres的相关问题。出于某种原因,该解决方案对我不起作用 - insert语句的返回值始终为1。

  • 请参阅基于XML的解决方案。我想在没有XML的情况下做同样的事情 - 插入记录并找到我刚刚检查的记录的新自动生成的id。

  • See this related question for Postgres. For some reason, the solution doesn't work for me - the return value of the insert statement is always "1".
  • See this other question for an XML based solution. I would like to do the same without XML - insert a record and find the new auto-generated id of the record I just insreted.

我没有找到与< selectkey> 匹配的注释(请参阅未解决的问题
如何继续?

I didn't find a matching annotation to <selectkey> (see this open issue) How do I proceed?

检查mybatis代码显示 INSERT 是通过 UPDATE 实现的,并且始终返回插入的行数!所以...除非我在这里完全遗漏了一些东西,否则使用当前的(3.0.3)实现无法做到这一点。

Examining mybatis code reveals that INSERT is implemented via UPDATE, and always returns the number of inserted rows! So ... unless I'm completely missing something here, there's no way to do this using the current (3.0.3) implementation.

推荐答案

实际上,可以使用 @Options 注释(假设您在数据库中使用auto_increment或类似内容)来执行此操作:

Actually, it's possible to do it, with the @Options annotation (provided you're using auto_increment or something similar in your database) :

@Insert("insert into table3 (id, name) values(null, #{name})") 
@Options(useGeneratedKeys=true, keyProperty="idName")
int insertTable3(SomeBean myBean); 

请注意 keyProperty =idName如果SomeBean中的key属性被命名为id,则不需要part。还有一个 keyColumn 属性可用,因为MyBatis无法自己找到主键列的极少数情况。另请注意,通过使用 @Options ,您将方法提交到某些默认参数;查阅文档非常重要(链接如下 - 当前版本的第60页)!

Note that the keyProperty="idName" part is not necessary if the key property in SomeBean is named "id". There's also a keyColumn attribute available, for the rare cases when MyBatis can't find the primary key column by himself. Please also note that by using @Options, you're submitting your method to some default parameters ; it's important to consult the doc (linked below -- page 60 in the current version) !

(旧答案)(最近) @SelectKey 注释可用于更复杂的密钥检索(序列,identity()函数...)。这是 MyBatis 3用户指南(pdf)提供了示例:

(Old answer) The (quite recent) @SelectKey annotation can be used for more complex key retrieval (sequences, identity() function...). Here's what the MyBatis 3 User Guide (pdf) offers as examples :


此示例显示使用@SelectKey批注检索
插入前的序列中的值:

This example shows using the @SelectKey annotation to retrieve a value from a sequence before an insert:



@Insert("insert into table3 (id, name) values(#{nameId}, #{name})") 
@SelectKey(statement="call next value for TestSequence", keyProperty="nameId", before=true, resultType=int.class) 
int insertTable3(Name name); 




此示例显示使用@SelectKey批注后检索标识值插入:

This example shows using the @SelectKey annotation to retrieve an identity value after an insert:



@Insert("insert into table2 (name) values(#{name})")
@SelectKey(statement="call identity()", keyProperty="nameId", before=false, resultType=int.class)
int insertTable2(Name name);

这篇关于如何在带有注释的mysql中使用mybatis返回插入内容上的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆