1. 通过Mybatis框架提供的selectKey获得
如:
<insert id="add" parameterType="com.lym.pojo.CheckGroup">
<!--通过selectKey获得自增的id -->
<selectKey keyProperty="id" resultType="java.lang.Integer" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_checkgroup(code,name,sex,helpCode,remark,attention)
values
(#{code},#{name},#{sex},#{helpCode},#{remark},#{attention})
</insert>
- keyProperty:需与parameterType中CheckGroup的唯一标识id保持一致,因为keyProperty会将返回的id封装到CheckGroup中的id对应属性上,CheckGroup对象就可以通过get方法获取自增的id值了
- order=“AFTER”:应先执行insert 再执行SELECT才能获得自增的id,所以这里填AFTER