Mybatis 插入一条记录返回主键

插入一条记录返回主键


  • 情景:向数据库中插入一条数据,要求返回插入数据的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映射文件



<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中实现



 SELECT seq_mybatis_user.nextval FROM dual 
INSERT INTO mybatis_user (user_id,user_name, password, salary, birthday) VALUES (#{id} ,#{name},#{password},#{salary},#{birthday}) 

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());
    }

你可能感兴趣的:(Mybatis)