MyBatis Version: 3.2.8
Actually very simple, before want to complicate ...
MyBatis after inserting a record of the self-increment ID, the incoming Pojo automatically updates the value of the ID to the inserted result
(currently only the ID is measured, other fields if not given the value, do not know whether updates, pending testing)
The records are as follows:
User.xml
<insert id= "Create" parametertype= "User" usegeneratedkeys= "true" keyproperty= "id" > INSERT into T_user (< Include refid= "All_property"/>) VALUES (#{createtime}, #{mobile}, #{email},#{qq}) </insert>
Iusermapper.java
void Create (user user);
Test Code
@Test public void Testcreate () {sqlsession session = Dbutil.getsqlsession (); Iusermapper usermapper = Session.getmapper (Iusermapper.class); User user = Usermapper.find ("18910000012");//User.setid (0L); User.setmobile ("18900121033"); Usermapper.create (user); System.out.println (User.getid ()); Session.commit (); Dbutil.close (session); }
Result (ID returned):
Other circumstances how to get the ID is still to be experimented with, this kind of incoming Pojo is very simple, do not need <selectkey/> (I did not get ID through this tag)
MyBatis insert into MySQL with Auto_increment key