I have a Java project that uses MyBatis to access a PostgreSQL database. PostgreSQL allows to return fields of a newly created row after an INSERT
statement, an
The return type of mapped insert method can be void
or int
(in which case it will return the number of the inserted row). You can do the following mechanism to return the generated id:
SELECT currval('my_seq')
INSERT INTO mytable(col1, col2) VALUES (#{val1}, #{val2})
This will set generated id
column to id
property of your parameter class. After that, object you passed as parameter will have generated id
set in its property.