Mybatis new data back to the main key which records

Sometimes we need to insert a later data, hoping to return it to a primary key information, provides us MyBatis underlying this approach
1: tags implemented by selectKey

<insert id="insertStudent">
   insert into student(name,email,age)
   values(#{name},#{email},#{age})
   <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() as id
   </selectKey>
</insert>

This method should be noted, the incoming parameter is not available @Param modified

2: to achieve by insert tags

<insert id="insertStudent" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
   insert into student(name,email,age)
   values(#{name},#{email},#{age})
</insert>

Such a method is more concise

Published 27 original articles · won praise 1 · views 842

Guess you like

Origin blog.csdn.net/weixin_44971379/article/details/105083397