天天看点

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
           

继续阅读