天天看點

mybatis使用Mysql新增資料後傳回主鍵id

  1. xml中寫法:
<!-- 添加一條資訊資料 -->
	<insert id="add" parameterType="com.hgcbys.bean.PostGraduate" useGeneratedKeys="true" keyProperty="id">
		INSERT INTO post_graduate  ......
	</insert>
           

主要在Insert标簽中加入:useGeneratedKeys=“true” keyProperty=“id”

  1. Controller中寫法。
//添加一條資料,postGraduate為添加的資料對象
	studentPostService.createStudentPost(postGraduate);
	//主要為以下此行(postGraduate即為剛剛添加的資料對象)
	Integer id= postGraduate.id();     //id即為新增資料的主鍵id
           

繼續閱讀