MyBatis course 3 and mybatis Course
Returned auto-increment primary key values:
First, if your database supports automatic generation of primary key fields (such as MySQL)
<insert id="insertAuthor" parameterType="domain.blog.Author"> <selectKey keyProperty="id" resultType="int" order="AFTER"> select last_insert_id()</selectKey> insert into Author ( username, password, email,bio, favourite_section) values ( #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})</insert>
KeyProperty |
The target attribute that the selectKey statement result should be set. |
ResultType |
The type of the result. MyBatis is can be calculated normally, but there is no problem in writing it. MyBatis allows any simple type to be used as the primary key type, including strings. |
Order |
This can be set to BEFORE or AFTER. If it is set to BEFORE, it will first select the primary key, set the keyProperty, and then execute the insert statement. If it is set to "AFTER", execute the insert statement first, and then the selectKey element-similar to the Oracle database. You can embed sequence calls in the insert statement. |
Non-auto-incrementing primary keys: