Mybatis获取自增长的主键id

养成习惯,先赞后看!!!

1.前言

这个问题主要是今天项目中新加的一个需求导致的,主要过程是这样的,因为每个项目里面用户,角色,权限这三者是密不可分的,在数据库中就可以通过下面这张图来表达他们三者之间的关系:
在这里插入图片描述
接下来我们就是来说整个的创建流程了
一般来说我们都是以下的流程:
在这里插入图片描述
但是现在项目中我们是这样一个流程
在这里插入图片描述
这样就有一个问题,我们怎么才能将user与role两者关联起来呢,要知道我们关联user与role就是将user的主键userId与role的主键roleId插入到user-role这个关联表中,之前因为我们是先创建在分配,所以完全可以获取到用户的userId,但是现在是要在创建的时候就分配,又因为我们的userId是在数据库中设置的自动增长,所以前端传给我们的user对象里面是不包含userId的.

所以对于如何取得自增长的Id就比较麻烦.查阅资料后发现,还是有办法解决的.而且有两种方法,这里都分享给大家,并且我自己也都测试了,的确可用.

2.解决方案

2.1方案一

这段代码加在你的insert语句中

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

主要有这几个注意点:

  • keyProperty,这里面填写的是你自己定义的主键名称,比如说你的是userId,里面就填userId,否则会报错
  • order,order有两个值before,after,这两个值分别表示一个是在执行插入操作之前再取出主键id,一个是执行插入操作之后再取出主键Id.前者使用与自己定义的自增长规则的id,后者就是用与我们的情况即自增长的id

小栗子:

  <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>

实际结果:
在这里插入图片描述
数据库中用户数据成功插入
在这里插入图片描述
我们再去看看user-role里面的数据插入了没有
在这里插入图片描述
说明的确是读取到了自增长的userId,数据也成功插入了.

2.2方案二

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

同样的这里的keyProperty也和上述的注意点一样

小栗子:

  <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>

实际结果:
在这里插入图片描述
user表中的数据成功插入:
在这里插入图片描述
再看看关联表中数据插入了没有:
在这里插入图片描述
也成功插入了,显然两者都能读取到自增长的userId

都看到这儿了,如果觉得对你有帮助的话,可以关注我的公众号,新人up需要你的支持!!!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lovely__RR/article/details/109311955