前言
我们在使用mybatis插入数据时,如果该表的主键id自增,我们如何在插入数据时,同时获取到id的值呢?
方式一
useGeneratedKeys=“true” , keyProperty位实体类字段,用来存储自增id
<insert id="save" useGenerateKey="true" keyProperty="id">
insert into tb_brand .......
</insert>
方式 二
msql的函数
LAST_INSERT_ID 即为最后插入的ID值
<insert id="save">
<selectKey keyProperty="id">
select last_insert_id()
</selectKey>
insert into tb_brand .......
</insert>