Mybatis 插入一條記錄返回主鍵
阿新 • • 發佈:2019-02-06
插入一條記錄返回主鍵
- 情景:向資料庫中插入一條資料,要求返回插入資料的id
- 主鍵必須為整型,而且支援自增
- 需求實現:在儲存完成後執行SELECT LAST_INSERT_ID()即可
- 實體設計
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) &&
Objects.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 +
", name='" + name + '\'' +
", password='" + password + '\'' +
", salary=" + salary +
", birthday=" + birthday +
'}';
}
}
mapper介面
void saveUser(User user);
注意:需求每次插入需要返回該資料的id,當資料插入後,則當前物件的id屬性將會有值;介面設計不需要返回值
mapper對映檔案
<?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>
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層
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();
}
}
測試方法
@Before
public void before(){
this.userService = new UserServiceImpl();
}
@Test
public void saveUser(){
User user = new User();
user.setName("小ad");
user.setPassword("12312");
user.setSalary(121.0F);
user.setBirthday(new Date());
this.userService.addUser(user);
// 插入物件後 則這個物件的user就會有值
System.out.println(user.getId());
}