I have a table that generates with an auto-generated column but I am unable to retrieve the ID using MyBatis. I have proven through tests that the SQL is working and that the rows are being inserted but I cannot get the ID generated.
The XML configuration is:
<insert id="insertRequestTrackingRow" parameterType="map">
INSERT INTO XML_LOG_T (REQ_SRC_SYS_CDE, REQ_USR_ID, REQ_XML_DOC, ROW_CRT_DTM, ROW_UPDT_DTM)
VALUES (#{sourceSystemCode}, #{userID}, #{message}, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
</insert>
On the XML_LOG_T is a column, REQ_ID, that is auto-generated. My mapper interface is:
public interface UdbDataMapper {
int insertRequestTrackingRow(
@Param("message") String message,
@Param("sourceSystemCode") String sourceSystemCode,
@Param("userID") String userID);
}
What is returned is always 1, which I believe is the number of records inserted. I’m sure I am missing something obvious but cannot see the wood for the trees.
Here’s a pretty good explanation of how to do this.
Basically you have to use the useGeneratedKeys=”true” and keyproperty=”keyfield” attributes in your XML configuration for the INSERT statement. The returned key field value is then inserted into your parameter object and can be retrieved from there. Something like the following code sample should work: