如何使用 MyBatis 在 Oracle 中获取最后一个插入 id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15234718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:29:17  来源:igfitidea点击:

How to obtain last insert id in Oracle using MyBatis?

javaoraclemybatis

提问by mduck

I'm inserting some data into an Oracletable and need to retrieve the idof the inserted row. Said idis being generated by a sequence and then inserted to the table by a trigger.

我正在向Oracle表中插入一些数据,需要检索id插入行的 。所述id是由序列生成,然后由触发器插入到表中。

Now, I know there are several ways to get the id of the inserted row when using JDBC, but since I'm using MyBatisto execute the INSERTcommand, I can't seem to figure out how to obtain the id after inserting my data. Any advice would be greatly appreciated.

现在,我知道有几种方法可以在使用时获取插入行的 id JDBC,但是由于我使用的MyBatis是执行INSERT命令,我似乎无法弄清楚如何在插入数据后获取 id。任何建议将不胜感激。

采纳答案by natros

Something like this should work

这样的事情应该工作

class User {
  int userId
  ...
}

<insert id="addUser" useGeneratedKeys="true" keyColumn="user_id" keyProperty="userId">
  INSERT INTO user(login, name,...) VALUES(#{login}, #{name},...
</insert>

回答by T M

For me it works like this (mybatis 3)

对我来说它是这样工作的(mybatis 3)

<insert id="create" parameterType="Project" useGeneratedKeys="true" keyProperty="project.projectId" keyColumn="PROJECT_ID">
    INSERT INTO PROJECT (TITLE,DESCRIPTION)
    VALUES
    (#{title},#{description})
</insert>

No need for selectKey. Just sure to put the correct value in keyProperty.. I have a trigger before insert in oracle to get next id from sequence.

不需要选择键。只要确保在 keyProperty 中输入正确的值。我在插入 oracle 之前有一个触发器,可以从序列中获取下一个 id。

Alternatively this works also:

或者,这也适用:

<insert id="createEmpty" statementType="CALLABLE" parameterType="Panelist">
    BEGIN INSERT INTO PANELIST(PANEL_ID) VALUES (#{panelId})
    RETURNING PANELIST_ID INTO
    #{panelist.panelistId,mode=OUT,jdbcType=INTEGER}; END;
</insert>

回答by Enrique Jiménez Flores

With oracle, better is doing it in two phases. Works well and the price is only one more mapper:

使用 oracle,最好分两个阶段进行。效果很好,价格只是多一个映射器:

First phase:

第一阶段:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"     
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sample.work.dao.SequencerMapper" >
<select id="selectNextId" resultType="long" >
 select seq_sample.nextval from dual
</select>
</mapper>

You obtain the seq, put into your object place holder and

您获得 seq,放入您的对象占位符并

Second Phase:

第二阶段:

insert your object

插入你的对象

回答by Jirawat Uttayaya

Let's say the trigger uses id_seq Oracle sequence to get the id. If you execute from MyBatis using the same database session, the SQL

假设触发器使用 id_seq Oracle 序列来获取 id。如果使用相同的数据库会话从 MyBatis 执行,SQL

select id_seq.currval from dual;

You will get the ID used.

您将获得使用的 ID。