这两天在整理原有系统接口时,遇到后端的人员-角色-菜单的权限接口没有进行连表的关联查询操作,前端拿数据非常不方便,现在将接口相关sql进行修改并让前端可以一次性拿到想要的数据

原有的单表简单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">
limit #{offset}, #{limit}
</if>
</select>

修改为嵌套list返回结果集的查询(关键点:使用resultMap中的collection标签):

注意将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">
select
r.role_id,
r.role_name,
r.remark,
r.create_time,
rm.menu_id
from sys_role r
left join sys_role_menu rm on r.role_id=rm.role_id
order by r.role_id asc
<if test="offset != null and limit != null">
limit #{offset}, #{limit}
</if>
</select>

现在这样子是已经达到返回结果集中嵌套list的效果

但是同时也带来另外一个问题:分页参数对应的是left join后的限制,并不是我们预期只对主表的分页限制,所以sql语句还需要进一步完善:

使用where条件,将分页参数当做一个查询子集,然后再利用 关键字IN 实现(由于IN关键字不可与limit在同一个语句使用,所以需要创建一个临时表)

sql最终结果:

     <select id="queryList" resultMap="BaseResultMap">
select
r.role_id,
r.role_name,
r.remark,
r.create_time,
rm.menu_id
from sys_role r
left join 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一下修改大致过程,希望能够帮助有需要的同学

最新文章

  1. div居中方法
  2. django1.9 创建数据表
  3. 我的AutoCAD二次开发之路 (一)
  4. JS如何将CST格式的日期转换为制定格式String
  5. 如何开启多用户同时远程连接(Windows2008 Windows2012)
  6. Python学习入门教程,字符串函数扩充详解
  7. Couchbase忘记登录密码怎么办
  8. FTP方式发布webservice
  9. css块居中
  10. request.getParameter和request.setAttribute/request.getAttribute
  11. 【资料下载区】【iCore、 iCore2相关资料】更新日期2017/1/11
  12. Android代码安全工具集
  13. 等比数列二分求和(logn复杂度)
  14. 【Python】【有趣的模块】【Requests】无状态 &amp; 无连接
  15. PetaPoco源代码学习--3.Sql类
  16. java基础知识文章汇总
  17. 关于Java开发一职的经验
  18. oracle 之创建用户,表空间,授权,修改用户密码
  19. RPM 安装oracle18c 修改字符集的方法
  20. TypeError: to_categorical() got an unexpected keyword argument &#39;nb_classes&#39;

热门文章

  1. html2canvas.js插件截图空白问题
  2. [工具推荐]001.FlipPDF使用教程
  3. [PHP学习教程 - 网络]001.下载/采集远程文件到本地(Download File)
  4. MVC案例之通过配置切换底层存储源(面向接口)
  5. lunix如何查看防火墙是否关闭和关闭开启防火墙命令
  6. 个人工具,编辑器visual studio code
  7. Java 异常(一) 异常概述及其架构
  8. 自制基于python的DoU log分析脚本
  9. Java实现 LeetCode 781 森林中的兔子(分析题)
  10. java实现第四届蓝桥杯剪格子