Mybatis gets the primary key id from the growth

Develop a habit, like first and then watch!!!

1 Introduction

This problem is mainly caused by a new requirement in today's project. The main process is like this, because the users, roles, and permissions in each project are inseparable, and you can use the following picture in the database. Express the relationship between the three of them:
Insert picture description here
Next, we will talk about the entire creation process.
Generally speaking, we are the following processes:
Insert picture description here
But now in the project, we are such a process.
Insert picture description here
There is a problem. Associating with the role, we must know that we associate user and role by inserting the primary key userId of the user and the primary key roleId of the role into the user-role association table.Because we created it in the distribution first, we can get it completely. The user's userId, but now it is assigned when it is created, and because our userId is automatically increased in the database, the user object passed to us by the front end does not contain the userId.

Therefore, it is more troublesome to obtain the self-increasing Id. After consulting the information, I found that there is still a way to solve it. And there are two ways to share it with you, and I have tested it myself, and it is indeed available.

2. Solution

2.1 Option One

This code is added to your insert statement

<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
  SELECT LAST_INSERT_ID()
</selectKey>

There are mainly these points for attention:

  • keyProperty, fill in the name of the primary key you defined yourself, for example, if yours is userId, fill in userId in it, otherwise an error will be reported
  • Order, order have two values ​​before and after. These two values ​​indicate that one is to take out the primary key id before performing the insert operation, and the other is to take out the primary key id after performing the insert operation. The former uses self-defined self-growth rules. id, the latter is the self-increasing id used in our case

Little chestnuts:

  <insert id="insertSelective" parameterType="ams.web.admin.entity.UserDao">
    <selectKey keyProperty="userId" order="AFTER" resultType="java.lang.Integer">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into tb_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="employeeId != null">
        employee_id,
      </if>
      <if test="loginName != null">
        login_name,
      </if>
      <if test="dispName != null">
        disp_name,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="sex != null">
        sex,
      </if>
      <if test="state != null">
        state,
      </if>
      <if test="departmentId != null">
        department_id,
      </if>
      <if test="telephone != null">
        telephone,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="deleted != null">
        deleted,
      </if>
      <if test="createPersonId != null">
        create_person_id,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updatePersonId != null">
        update_person_id,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="remark != null">
        remark,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="employeeId != null">
        #{
    
    employeeId,jdbcType=INTEGER},
      </if>
      <if test="loginName != null">
        #{
    
    loginName,jdbcType=VARCHAR},
      </if>
      <if test="dispName != null">
        #{
    
    dispName,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{
    
    password,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        #{
    
    sex,jdbcType=TINYINT},
      </if>
      <if test="state != null">
        #{
    
    state,jdbcType=TINYINT},
      </if>
      <if test="departmentId != null">
        #{
    
    departmentId,jdbcType=VARCHAR},
      </if>
      <if test="telephone != null">
        #{
    
    telephone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{
    
    email,jdbcType=VARCHAR},
      </if>
      <if test="deleted != null">
        #{
    
    deleted,jdbcType=VARCHAR},
      </if>
      <if test="createPersonId != null">
        #{
    
    createPersonId,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{
    
    createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updatePersonId != null">
        #{
    
    updatePersonId,jdbcType=INTEGER},
      </if>
      <if test="updateTime != null">
        #{
    
    updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remark != null">
        #{
    
    remark,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>

The actual result: the
Insert picture description here
user data in the database was successfully inserted.Let
Insert picture description here
's go and see if the data in the user-role is inserted. It does not
Insert picture description here
indicate that the self-increasing userId is indeed read, and the data is successfully inserted.

2.2 Option Two

<insert id="insertSelective" parameterType="请求对象" useGeneratedKeys="true" keyProperty="Id">
.............................
</insert>

The same keyProperty here is the same as the above note

Little chestnuts:

  <insert id="insertSelective" parameterType="ams.web.admin.entity.UserDao" useGeneratedKeys="true" keyProperty="userId">
    insert into tb_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="employeeId != null">
        employee_id,
      </if>
      <if test="loginName != null">
        login_name,
      </if>
      <if test="dispName != null">
        disp_name,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="sex != null">
        sex,
      </if>
      <if test="state != null">
        state,
      </if>
      <if test="departmentId != null">
        department_id,
      </if>
      <if test="telephone != null">
        telephone,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="deleted != null">
        deleted,
      </if>
      <if test="createPersonId != null">
        create_person_id,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updatePersonId != null">
        update_person_id,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="remark != null">
        remark,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="employeeId != null">
        #{
    
    employeeId,jdbcType=INTEGER},
      </if>
      <if test="loginName != null">
        #{
    
    loginName,jdbcType=VARCHAR},
      </if>
      <if test="dispName != null">
        #{
    
    dispName,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{
    
    password,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        #{
    
    sex,jdbcType=TINYINT},
      </if>
      <if test="state != null">
        #{
    
    state,jdbcType=TINYINT},
      </if>
      <if test="departmentId != null">
        #{
    
    departmentId,jdbcType=VARCHAR},
      </if>
      <if test="telephone != null">
        #{
    
    telephone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{
    
    email,jdbcType=VARCHAR},
      </if>
      <if test="deleted != null">
        #{
    
    deleted,jdbcType=VARCHAR},
      </if>
      <if test="createPersonId != null">
        #{
    
    createPersonId,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{
    
    createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updatePersonId != null">
        #{
    
    updatePersonId,jdbcType=INTEGER},
      </if>
      <if test="updateTime != null">
        #{
    
    updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remark != null">
        #{
    
    remark,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>

Actual result:
Insert picture description here
The data in the user table is successfully inserted:
Insert picture description here
Let's see if the data in the related table is inserted: It
Insert picture description here
is also successfully inserted, obviously both can read the self-increasing userId

I have seen it here. If you think it is helpful to you, you can pay attention to my public account. The newcomer up needs your support!!!
Insert picture description here

Guess you like

Origin blog.csdn.net/lovely__RR/article/details/109311955