简体   繁体   English

在MyBatis(Oracle)中插入后如何返回多个生成的列

[英]How to return multiple generated columns after insert in MyBatis(Oracle)

I have table with primary key, which consist of several columns. 我有带有主键的表,该表由几列组成。 There is batch insert with special Oracle hook - ignore_row_on_dupkey_index . 有带有特殊Oracle钩子的批处理插入-ignore_row_on_dupkey_index That hook allow to ignore Unique Constraint Exception, duplicate records just get ignored, whereas non duplicate get successfully inserted. 该挂钩允许忽略唯一约束异常,重复记录只是被忽略,而非重复记录则可以成功插入。 Using simple jdbc I could easily return primary key(consist of several columns) by code: 使用简单的jdbc,我可以通过代码轻松返回主键(由几列组成):

try(PreparedStatement st = connectio.preparedStatement("insert /* ignore_row_on_dupkey(Table_name, Constraint) */ into TABLE(c1, c2, c3) values(?,?,?)", new String [] {"c1", "c2"})) {
//Batch insert then get generated keys
}

Then I could analyze duplicates by iterating over returned keys. 然后,我可以通过遍历返回的键来分析重复项。

I want to achieve this the same by MyBatis. 我想通过MyBatis实现相同的目标。 I found Options annotation, which allows to do it by setting property useGeneratedKeys and keyColumn . 我发现了Options批注,该批注允许通过设置属性useGeneratedKeyskeyColumn来实现 The problem is I have complex primary key, whereas keyColumn has type String. 问题是我有复杂的主键,而keyColumn的类型为String。 Also I dont want to use SelectKey annotation. 我也不想使用SelectKey注释。

So my question is can I return several columns value and how by MyBatis or not? 所以我的问题是我可以返回几个列的值吗?

Thank you. 谢谢。

keyColumn allows to specify multiple columns. keyColumn允许指定多个列。 Here's relevant piece of the documentation (note the last sentence): 这是文档的相关部分(请注意最后一句话):

keyColumn | keyColumn | (insert and update only) Sets the name of the column in the table with a generated key. (仅插入和更新)使用生成的键设置表中的列名称。 This is only required in certain databases (like PostgreSQL) when the key column is not the first column in the table. 仅当键列不是表中的第一列时,才在某些数据库(如PostgreSQL)中需要这样做。 Can be a comma separated list of columns names if multiple generated columns are expected. 如果需要多个生成的列,则可以是用逗号分隔的列名称列表。

And an example from mybatis tests : 还有来自mybatis 测试的示例:

<insert id="insertTable2WithGeneratedKeyXml" useGeneratedKeys="true"
    keyProperty="nameId,generatedName" keyColumn="ID,NAME_FRED">
  insert into table2 (name) values(#{name})
</insert>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM