有时我们在往数据库插入数据时,一般并不会将主键值传入,而是让数据库自动生成主键值,有时我们需要拿到该主键值进行后一步操作,该怎么办呢
方案:sql标签指定 useGeneratedKeys="true" keyProperty="pojo主键属性名"
<insert id="add" useGeneratedKeys="true" keyProperty="id">
insert into tb_brand (brand_name, company_name, ordered, description, status)
values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>
插入操作之后,获取插入对象,可以发现id在插入有对象中已经被赋值