過去2日間で、元のシステムインターフェイスを照合するときに、接続されたテーブルの関連付けられたクエリ操作を実行しないバックエンドの担当者ロールメニュー権限インターフェイスに遭遇しました。フロントエンドがデータを取得するのは非常に不便です。一度に必要なデータを取得
元の単一テーブルの単純なSQL:
<select id = "queryList" resultType = "com.framework.entity.SysRoleEntity">
select * from sys_role order by role_id asc
< if test = "offset!= null and limit!= null">
制限#{offset}、#{limit}
</ if >
</ select>
ネストされたリストの結果セットを返すようにクエリを変更します(キーポイント:resultMapでコレクションタグを使用):
selectのresultTypeがresultMapに変更されていることに注意してください
<resultMap id = "BaseResultMap" type = "com.framework.entity.SysRoleEntity">
<id column = "role_id" property = "roleId" jdbcType = "INTEGER"> </ id>
<result column = "role_name" property = "roleName" jdbcType = "VARCHAR"> </ result>
<result column = "remark" property = "remark" jdbcType = "VARCHAR"> </ result>
<result column = "create_time" property = "createTime" jdbcType = "TIMESTAMP"> </ result>
<collection property = "menuIdList" resultMap = "menuIdListMap" />
</ resultMap>
<resultMap id = "menuIdListMap" type = "java.lang.Long">
<id column = "menu_id" property = "id" javaType = "Long"> </ id>
</ resultMap>
<select id = "queryList" resultMap = "BaseResultMap">
選択する
r.role_id、
r.role_name、
r.remark、
r.create_time、
rm.menu_id
sys_role rから
左結合sys_role_menu rm on r.role_id = rm.role_id
r.role_id ascによる注文
< if test = "offset!= null and limit!= null">
制限#{offset}、#{limit}
</ if >
</ select>
これで、結果セットにネストされたリストを返す効果が得られました
しかし、同時に、別の問題も発生します:ページングパラメーターは左結合後の制限に対応します。これは、メインテーブルに対してのみ期待されるページング制限ではないため、SQLステートメントをさらに改善する必要があります。
where条件を使用してページングパラメーターをクエリのサブセットとして扱い、次にキーワードINを使用して達成します(INキーワードは制限と同じステートメントで使用できないため、一時テーブルを作成する必要があるため)
SQL最終結果:
<select id = "queryList" resultMap = "BaseResultMap">
選択する
r.role_id、
r.role_name、
r.remark、
r.create_time、
rm.menu_id
sys_role rから
左結合sys_role_menu rm on r.role_id = rm.role_id
<where>
<if test="offset != null and limit != null">
r.role_id IN (SELECT temp.role_id from (SELECT role_id FROM sys_role limit #{offset}, #{limit}) AS temp)
</if>
</where>
order by r.role_id asc
<!--<if test="offset != null and limit != null">
limit #{offset}, #{limit}
</if>-->
</select>
最终完成修改,发布测试,这里mark一下修改大致过程,希望能够帮助有需要的同学