这个问题主要是今天项目中新加的一个需求导致的,主要过程是这样的,因为每个项目里面用户,角色,权限这三者是密不可分的,在数据库中就可以通过下面这张图来表达他们三者之间的关系:
接下来我们就是来说整个的创建流程了 一般来说我们都是以下的流程:
但是现在项目中我们是这样一个流程
这样就有一个问题,我们怎么才能将user与role两者关联起来呢,要知道我们关联user与role就是将user的主键userId与role的主键roleId插入到user-role这个关联表中,之前因为我们是先创建在分配,所以完全可以获取到用户的userId,但是现在是要在创建的时候就分配,又因为我们的userId是在数据库中设置的自动增长,所以前端传给我们的user对象里面是不包含userId的.
所以对于如何取得自增长的Id就比较麻烦.查阅资料后发现,还是有办法解决的.而且有两种方法,这里都分享给大家,并且我自己也都测试了,的确可用.
这段代码加在你的insert语句中
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
主要有这几个注意点:
小栗子:
<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,数据也成功插入了.
<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