MySQL, using the usegeneratedkey attribute:
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="studId"> INSERT INTO STUDENTS(NAME, EMAIL, PHONE) VALUES(#{name},#{email},#{phone})</insert>
In other ways, use the selectkey sub-Tag:
Attribute order = "before" indicates that mybatis takes the next value of the sequence as the primary key value, and sets the value to the studid Attribute before executing the insert SQL statement.
<insert id="insertStudent" parameterType="Student"> <selectKey keyProperty="studId" resultType="int" order="BEFORE"> SELECT ELEARNING.STUD_ID_SEQ.NEXTVAL FROM DUAL </selectKey> INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL, PHONE) VALUES(#{studId},#{name},#{email},#{phone})</insert>
We can also use triggers to set the primary key value when getting the next value of the sequence, and set the value to the primary key column before executing the insert SQL statement. If you use this method, the corresponding insert ing statement is as follows:
<insert id="insertStudent" parameterType="Student"> INSERT INTO STUDENTS(NAME,EMAIL, PHONE) VALUES(#{name},#{email},#{phone}) <selectKey keyProperty="studId" resultType="int" order="AFTER"> SELECT ELEARNING.STUD_ID_SEQ.CURRVAL FROM DUAL </selectKey></insert
This article from the "unruly Wind" blog, please be sure to keep this source http://fengcl.blog.51cto.com/9961331/1875484
Mybatis automatically generates the primary key