最初のように習慣を身につけてから見てください!!!
1はじめに
この問題は主に、今日のプロジェクトの新しい要件が原因で発生します。各プロジェクトのユーザー、役割、および権限は分離できないため、メインプロセスは次のようになり、データベースで次の図を使用できます。それらの3:
次に、我々は全体の作成プロセスについてお話します。
一般的に言って、私たちは次のプロセスです。
しかし、今のプロジェクトでは、我々はこのようなプロセスは、
その問題がある、どのように我々はとの関連付け、ユーザーを変更することができますロールについては、ユーザーのプライマリキーuserIdとロールのプライマリキーroleIdをユーザーロール関連付けテーブルに挿入することで、ユーザーとロールを関連付けることを知っておく必要があります。最初にディストリビューションで作成したため、取得できます。ユーザーのuserIdですが、作成時に割り当てられるようになりました。また、userIdはデータベースで自動的に増加するため、フロントエンドから渡されたユーザーオブジェクトにはuserIdが含まれていません。
そのため、自己増加IDを取得するのは面倒です。情報を調べたところ、まだ解決方法があることがわかりました。また、共有する方法は2つあり、自分でテストしました。確かに利用可能です。
2.解決策
2.1オプション1
このコードは挿入ステートメントに追加されます
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
主に注意すべき点は次のとおりです。
- keyPropertyで、自分で定義した主キーの名前を入力します。たとえば、userIdの場合は、userIdを入力します。そうでない場合は、エラーが報告されます。
- 順序、順序には前後に2つの値があります。これらの2つの値は、1つは挿入操作を実行する前に主キーIDを取得し、もう1つは挿入操作を実行した後に主キー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>
実際の結果:
データベース内のユーザーデータが正常に挿入さ
れました。ユーザーロール内のデータが挿入されているかどうかを確認し
てみましょう。自己増加型userIdが実際に読み取られ、データが正常に挿入されたことを示すものではありません。。
2.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>
実際の結果:
ユーザーテーブルのデータが正常に挿入されました:
関連テーブルのデータが挿入されているかどうかを確認しましょう:これ
も正常に挿入されています。明らかに、両方とも自己増加するuserIdを読み取ることができます。
私はここでそれを見ました。あなたがそれがあなたに役立つと思うなら、あなたは私の公開アカウントに注意を払うことができます。新参者はあなたのサポートを必要としています!!!