利用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>
-
- public void testDao(){
- deptDao = session.getMapper(DepartmentDao.class);
- Department department = new Department();
- department.setDepartmentName("ares");
- department.setDepartmentManagerName("tom");
- department.setDepartmentId(32);
- department.setCompanyId(6201);
- deptDao.addDept(department);
- System.out.println("新部门ID:"+department.getId());
- }