Insert a record return primary key
Scenario:Inserting a piece of data into the database requires that the ID primary key that returns the inserted data must be integer and supports the self-increment requirement implementation: Execute SELECT LAST_INSERT_ID () after saving is complete
Entity Design
public class User implements Serializable {private Integer ID;
private String name;
private String password;
Private Float salary;
Private Date birthday;
Public String GetPassword () {return password;
} public void SetPassword (String password) {this.password = password;
} public Integer GetId () {return id;
} public void SetId (Integer id) {this.id = ID;
} public String GetName () {return name;
} public void SetName (String name) {this.name = name;
} public Float Getsalary () {return salary;
} public void Setsalary (Float salary) {this.salary = salary;
Public Date Getbirthday () {return birthday;
} public void Setbirthday (Date birthday) {this.birthday = birthday;
} @Override public boolean equals (Object o) {if (this = = O) return true; if (o = = NULL | | getclass ()! = O.getclass ()) return false;
User user = (user) O; return objects.equals (ID, user.id) && objects.equals (name, user.name) && O
Bjects.equals (Salary, User.salary) && objects.equals (Birthday, user.birthday);
} @Override public int hashcode () {return Objects.hash (ID, name, salary, birthday); } @Override Public String toString () {return "user{" + "id=" + ID + ", n
Ame= ' + name + ' \ ' + ', password= ' + password + ' \ ' + ', salary= ' + salary +
", birthday=" + Birthday + '} '; }
}
Mapper Interface
void Saveuser (user user);
Note: The requirement for each insertion needs to return the ID of the data, and when the data is inserted, the ID property of the current object will have a value; interface design does not require a return value
Mapper Mapping File
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Mapper Public
"-//mybatis.org//dtd mapper 3.0//en"
"HTTP://MYBATIS.ORG/DTD/MYBATIS-3-MAPPER.DTD" >
<mapper namespace= "Com.bjlemon.mybatis.mapper.UserMapper" >
<cache type= " Org.mybatis.caches.ehcache.EhcacheCache "/>
<insert id=" Saveuser " parametertype=" User ">
<selectkey keyproperty= "id" resulttype= "int" order= "after" >
select last_insert_id ();
</selectKey>
INSERT into
mybatis_in_action_user (name,password,salary,birthday)
VALUES
(#{name},#{password},#{salary},#{birthday})
</insert>
</mapper>
implemented in Orcal
<insert parametertype= "User" id= "Saveuser" >
<selectkey order= "before" resulttype= "int" keyproperty= "id "> select Seq_mybatis_user.nextval from Dual </selectKey>
INSERT into Mybatis_user (User_id,user_name, Password, salary, birthday) VALUES (#{id}, #{name},#{password},#{salary},#{birthday})
</insert>
Service Layer
void AddUser (user user);
@Override public
void AddUser (user user) {
sqlsession sqlsession = null;
try {
sqlsession = mybatisutils.getsqlsession ();
Usermapper mapper = Sqlsession.getmapper (usermapper.class);
if (user = = null) {
throw new IllegalArgumentException ();
}
Mapper.saveuser (user);
Sqlsession.commit ();
} catch (Exception e) {
e.printstacktrace ();
Sqlsession.rollback ();
} finally {
mybatisutils.clolsesqlsession ();
}
}
test Method
@Before public void before () {this.userservice = new Userserviceimpl ();
} @Test public void Saveuser () {User user = new User ();
User.setname ("small ad");
User.setpassword ("12312");
User.setsalary (121.0F);
User.setbirthday (New Date ());
This.userService.addUser (user);
When the object is inserted, the user of the object will have the value System.out.println (User.getid ()); }