简体   繁体   English

MyBatis 3 + PostgreSQL-插入时获取主键值

[英]MyBatis 3 + PostgreSQL - get primary key value on insertion

I have a Maven project that uses Java Spring, MyBatis, and MyBatis-Spring to map objects to a PostgreSQL database. 我有一个Maven项目,该项目使用Java Spring,MyBatis和MyBatis-Spring将对象映射到PostgreSQL数据库。 I want to be able to query the value of the primary key at the same time that I insert a new record, and have yet to find a method that works. 我希望能够在插入新记录的同时查询主键的值,但尚未找到一种有效的方法。 My current implementation does not return the correct value; 我当前的实现未返回正确的值; it appears to always be returning 1. 它似乎总是返回1。

This is the mapper's XML configuration for the query: 这是查询的映射器的XML配置:

<insert id="registerNewUser" parameterType="com.hunter.databasejar.User">
    <selectKey keyProperty="ID" resultType="int">
      SELECT currval('"Users_ID_seq"')
    </selectKey>    
    insert into "Users" ("Username", "FirstName", "LastName") values (#{username}, #{firstName}, #{lastName})
</insert>

In Java, I write the following, and the value of i is always 1. 在Java中,我编写以下代码,并且i的值始终为1。

int i = sqlSession.insert("UserMapper.registerNewUser", user);

I have also tried altering the XML config to try the "returning" syntax from SQL, but always got an i value of -1. 我还尝试过更改XML配置,以尝试从SQL“返回”语法,但始终将i值设为-1。

<insert id="registerNewUser" parameterType="com.hunter.databasejar.User">
    insert into "Users" ("Username", "FirstName", "LastName") values (#{username}, #{firstName}, #{lastName}) returning "ID"
</insert>

My project is using MyBatis 3.2.4. 我的项目正在使用MyBatis 3.2.4。

I think you need to change the case of the keyProperty to be id with a suitable setter 我认为您需要将keyProperty的大小写更改为具有合适设置程序的id

setId(int value);

AFAIK, there is no way to create a Javabeans-compatible setter for an upper-case field. AFAIK,无法为大写字段创建与Javabeans兼容的setter。

But the return value you are seeing is the number of rows inserted, not the allocated primary key value. 但是,您看到的返回值是插入的行数,而不是分配的主键值。 There does not appear to be a way for MyBatis to harness the useful (but non-standard) returning clause. MyBatis似乎没有办法利用有用的(但非标准的)返回子句。

To get the key value, you need to use the order= option in the selectKey declaration. 要获取键值,您需要在selectKey声明中使用order=选项。 This question has a couple of options for PostgreSQL Returning values from MyBatis <insert> mapped methods The technique described will update the id field in the object supplied to the method. 这个问题对于PostgreSQL有几个选项。 从MyBatis“ <插入>”映射的方法返回值 。所描述的技术将更新提供给该方法的对象中的id字段。

I use an Oracle database, so don't feel qualified to recommend an answer for PostgreSQL. 我使用的是Oracle数据库,因此没有资格推荐PostgreSQL的答案。 But as a side note, if the order="BEFORE" option works with PostgreSQL, it is compatable with Oracle. 但要注意的是,如果order="BEFORE"选项适用于PostgreSQL,则它与Oracle兼容。

I found that my issue was that I had two columns with "id" somewhere in them. 我发现我的问题是我有两列带有“ id”的列。 MyBatis was trying to map my column named "CognitoID" to my plain old "ID" column and blowing up when the cognito ID wasn't something that could be converted to an int. MyBatis试图将我的名为“ CognitoID”的列映射到普通的旧“ ID”列,并且在无法将Cognito ID转换为int时崩溃。

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

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