At some point, our business needs to execute the INSERT statement before returning the primary key, you can use the primary key mapping
First, define SQL, take MySQL as an example
1 < ID= "AddUser" usegeneratedkeys= "true" keyproperty= "User _id " parametertype=" Com.entity.User ">2 INSERT INTO User (Name,xes,age) VALUES (' Jack ', ' Male ',3</insert>
There are two properties here.
Usegeneratedkeys: Whether to generate a primary key
the value of Keyproperty corresponds to the user_id property of the Com.entity.User class
Writing DAO
Public int addUser (user user);
Call Mapper
New Userdao (); int num = userdao.adduser (); SYSTEM.OUT.PRINTLN (num); // the only number of items that are output here is response // obtained directly from the entity class, Mybatista automatically sets the primary key ID into the user class
USER.GETUSER_ID () Gets the primary key ID value of the map
Mybatista primary key mapping, performing inset return primary key