MyBatis Insert return primary KEY (sqlserver2008)Mybatisxml configuration, the following two ways to line 1:<insert id= "Insert" parametertype= "Com.user.model.User" > <selectkey resulttype= " Java.lang.Integer "keyproperty=" id "order=" after "> select @ @IDENTITY </selectKey> <! [cdata[INSERT INTOTb_user (Username,password) VALUES (#{username},#{password})]]></insert> mode 2:<insert id= "Insertuser" parametertype= "Com.user.model.User" usegeneratedkeys= "true" keyproperty= "id" ><! [cdata[INSERT INTOTb_user (Username,password) VALUES (#{username},#{password})]]></insert> defines the DAO layer, the service layer's Insert method in action to invoke user user = new user (), User.setusername ("Tom"); User.setpassword ("123456"); int row = Userservice.insert (user);
//Note: INSRT no longer returns a primary key in MyBatis, only returns the number of response rows, which is different from IbatisSystem.out.println ("Number of rows in response:" +row); int id = User.getid ();
//Gets the value of the identity column ID from the object that has been increasedSYSTEM.OUT.PRINTLN ("ID of the newly inserted data:" +id);