Mybatis里如何在insert后获得主键值

在mybatis的sql配置文件里的insert标签里添加(DB2适用

1
2
3
<selectKey resultClass="long" keyProperty="_id">
SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
</selectKey>

这里的keyProperty指的是传入参数中的属性

Oracle适用

1
2
3
<selectKey resultClass="long" order="BEFORE" keyProperty="_id">
SELECT SEQ_TEST.NEXTVAL FROM DUAL
</selectKey>

Mysql适用

1
2
3
<selectKey resultClass="long" order="BEFORE" keyProperty="_id">
SELECT LAST_INSERT_ID() AS id
</selectKey>