利用Mybatis 的 selectKey来获得:
- <!-- 添加部门 返回部门ID -->
- <insert id="addDept" parameterType="com.demo.model.Department" keyProperty="id">
- <selectKey keyProperty='id' resultType='int' order='AFTER' >
- select LAST_INSERT_ID();
- </selectKey>
- insert into department(<include refid="departmentAllField"/>)
- values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});
- </insert>
或
- <insert id="addDept" parameterType="com.demo.model.Department" useGeneratedKeys="true" keyProperty="id">
- insert into department(<include refid="departmentAllField"/>)
- values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});
- </insert>