MyBatis: a data insertion, the primary key and returns this data id

MyBatis: a data insertion, the primary key and returns this data id

Doing development, it often is there such a business needs :
the need to make the operation of the two tables in the database, the operation required for the second table of the first table insert, update data is returned after the operation was successful.
Such as: add meeting minutes, meeting information is first inserted ( confInfo ), then insert summary information corresponding session ( sumInfo ), this case requires just inserted summary records corresponding number Minutes ( Sumida ).

So how in the insert (insert) After the operation, the direct returns the corresponding database field it?

Solution:

Was added in the corresponding SQL tag: useGeneratedKeys = "to true" and keyProperty = "return field" ;

You can!

Example:

<insert id="saveSummary" useGeneratedKeys="true" keyProperty="sumId" parameterType="dbsystem.sun.domain.Summary">
     insert into summary (title, content, updateTime) values (
        #{title}, #{content}, now());
</insert>


Published 69 original articles · won praise 11 · views 8449

Guess you like

Origin blog.csdn.net/qq_40994260/article/details/103704027