If you want to insert a record and do something else immediately, you need to get the primary key (usually the ID) of the record, and the MyBatis is handled in the following way.
The DAO layer interface is defined as follows:
void Importuser (@Param ("user") user user);
Note: this cannot be caused by a string or int that defines the return value of the interface to return the primary key.
XML configuration file:
< Span style= "color: #800000;" >insert id = "Importuser" Usegeneratedkeys = "true" Keyproperty = "User.ID" parametertype = "Com.ywlaker.model.User" > INSERT into user (user_name) VALUES (#{user.username,jdbctype=varchar}) </ Span style= "color: #800000;" >insert >
Usegeneratedkeys = "true" means return primary key
keyproperty= "user.id" indicates that the primary key is received with the ID property of the parameter user object
This method executes the primary key of the new record, inserts the user object, and wants to return a primary key that can be defined at the previous level
@Override Public throws serviceexception { try {This} . Usermapper.importuser (user); returncatch (Exception e) { throwNew Serviceexception ("Database Error! " ); }}
Of course, it is also possible to return directly to the user object.
MyBatis returns the primary key of the inserted record