mybatis新增数据后返回主键id

  1. xml中写法:
	
	
		INSERT INTO post_graduate(
		toSchool,toMajor,allGrade,isOriginalMajor,isDispensing,isExemption,isFulltime,isAccepted,schoolAddress)
		values ( #{toSchool}, #{toMajor},#{allGrade},#{isOriginalMajor}, #{isDispensing},#{isExemption}, #{isFulltime}, #{isAccepted}, #{schoolAddress}
		)
	
  1. Controller中写法。
else{
			//执行学生考研表添加操作,并且得到返回的行数
			studentPostService.createStudentPost(postGraduate);
			
			//主要为以下此行!!!!!!!!
			**Integer postGraduateId = postGraduate.getPostGraduate_id();**
			
			System.out.println("添加返回的主键为:" + postGraduateId);
			//用这个id更新"学生去向表"中学生数据的id
			//第一个Id为考研表的id,第二个id为账户id,需要从session中获取
			StudentAccount student = (StudentAccount)request.getSession().getAttribute("student");
			studentWhereService.updateStudentWherePostid(postGraduateId, student.getStu_id());
		}

你可能感兴趣的:(mybaties)