环境:Stringmvc + Mybatis
需求:插入时获取数据库自增长的id
方法一,插入其他字段数据,然后,根据其他字段select id。显然,需要insert 和select操作。太笨不用;
方法二,我通过查询,发现可以在Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”
<insert id="insertSelective" parameterType="EnterpriseCase" useGeneratedKeys="true" keyProperty="caseId">
insert into xxx
</insert>
这样呢,就能在执行insert后,就能从bean中get出id了,我用的就是这个方案,但是。。
因为我的mapper是通过generator自动生成的,so。。每次重新执行的时候都会把我添加的“useGeneratedKeys”和“keyProperty”覆盖掉,痛苦的手动添加的两遍后,还是放弃了;