java - MyBatis 3 + PostgreSQL - get primary key value on insertion -
i have maven project uses java spring, mybatis, , mybatis-spring map objects postgresql database. want able query value of primary key @ same time insert new record, , have yet find method works. current implementation not return correct value; appears returning 1.
this mapper's xml configuration query:
<insert id="registernewuser" parametertype="com.hunter.databasejar.user"> <selectkey keyproperty="id" resulttype="int"> select currval('"users_id_seq"') </selectkey> insert "users" ("username", "firstname", "lastname") values (#{username}, #{firstname}, #{lastname}) </insert>
in java, write following, , value of 1.
int = sqlsession.insert("usermapper.registernewuser", user);
i have tried altering xml config try "returning" syntax sql, got value of -1.
<insert id="registernewuser" parametertype="com.hunter.databasejar.user"> insert "users" ("username", "firstname", "lastname") values (#{username}, #{firstname}, #{lastname}) returning "id" </insert>
my project using mybatis 3.2.4.
i think need change case of keyproperty id suitable setter
setid(int value);
afaik, there no way create javabeans-compatible setter upper-case field.
but return value seeing number of rows inserted, not allocated primary key value. there not appear way mybatis harness useful (but non-standard) returning clause.
to key value, need use order=
option in selectkey
declaration. question has couple of options postgresql returning values mybatis <insert> mapped methods technique described update id
field in object supplied method.
i use oracle database, don't feel qualified recommend answer postgresql. side note, if order="before"
option works postgresql, compatable oracle.
Comments
Post a Comment