I'm using postgres for a project that I just started to work on, and I realized that Mybatis provide support to retrieve the autogenerated id keys for many databases but unfortunately postgres is not one of them, I modified a little bit the generated sql mappers and: - changing select instead of insert in the generated xml - add "RETURNING id" as last line of each sentence
so it gonna look like:
<select id="insert" keyColumn="id" keyProperty="id" parameterType="com.myproject.Order" ...
insert into ...
...
RETURNING id
</select>
with that change it works like a charm but the problem is that as soon as the generator is executed again the changes should be applied again manually, I was looking for a plugin to help me to automate the changes but I did not found any that seems to help, did you do any time before something similar? what would be the recommendation?
For the explanation, I created a table
users
in a schemambtest
.The below is the
<table />
element in the generator config. (in the comment, I wrotesqlStatementType
, but it should besqlStatement
)The generated 'UserMapper.xml' contains the following insert statement. Note that
useGeneratedKeys
,keyColumn
andkeyProperty
populated by the<generatedKey />
above.(As the WARNING says, you should not modify the generated statements)
The
insert
statement will set the generated key to theid
property ofUser
.Here is a demo project so that you can verify yourself.