【Mybatis】前端传入参数传入子查询

业务描述:css

经过用户code查询资源菜单,Mybatis中用子查询查询子级菜单,须要传入用户code进行查询,用户code为登陆token获取前端

表结构:java

表1:资源表 permission_infojson

表2:角色资源中间表 role_permissionurl

表3:用户角色表中间表 member_rolespa

思路:3d

1. 经过用户code连表查询父类菜单codecode

2. 经过父类code查询子菜单blog

3. 经过传入用户code查询再次查询用户code,传入子查询token

4.子查询再经过父类code和用户code查询用户资源

实现:

<!--查询用户资源父类列表-->
 <resultMap id="queryUserPermissionMap" type="com.alibaba.fastjson.JSONObject">
     <result column="permission_url" property="permissionUrl"/>
     <result column="permission_type" property="permissionType"/>
     <result column="permission_description" property="permissionDescription"/>
     <result column="permission_parent_code" property="permissionParentCode"/>
     <result column="permission_code" property="permissionCode"/>
     <collection property="children" column="{permissionParentCode=permission_parent_code,accountCode=accountCode}" javaType="List" select="queryUserPermissionChild">
         <result column="permission_url" property="permissionUrl"/>
         <result column="permission_type" property="permissionType"/>
         <result column="permission_description" property="permissionDescription"/>
         <result column="permission_code" property="permissionCode"/>
     </collection>
 </resultMap>
 <select id="queryUserPermission" parameterType="Map" resultMap="queryUserPermissionMap">
     SELECT
         permission_url,
         permission_type,
         permission_description ,
         permission_parent_code ,
         permission_code,
         (select account_code from member_role where account_code = #{accountCode} Limit 1) as accountCode
     FROM
         permission_info
     WHERE
         state = 1
     AND
         is_menu = 1
     AND
         permission_code
     IN (
         SELECT
             permission_code
         FROM
             role_permission
         WHERE
             role_code
     IN (
         SELECT
             role_code
         FROM
             member_role
         WHERE
             account_code = #{accountCode}))
 </select>

 <!--查询用户资源子类列表-->
 <select id="queryUserPermissionChild" parameterType="Map" resultType="com.alibaba.fastjson.JSONObject">
     SELECT
        permission_url ,
        permission_type ,
        permission_description ,
        permission_code
     FROM
         permission_info
     WHERE
         state = 1
     AND
         is_menu = 1
     AND
         permission_parent_code = #{permissionParentCode}
     AND
         permission_code
     IN (
         SELECT
             permission_code
         FROM
             role_permission
         WHERE
             role_code
     IN (
         SELECT
             role_code
         FROM
             member_role
         WHERE
             account_code = #{accountCode}))

 </select>

结果:

{
    "msg": "成功",
    "errorCode": 400,
    "timestamp": 1603077313,
    "data": [
        {
            "permissionType": 1,
            "permissionParentCode": "0",
            "children": [
                {
                    "permissionType": 1,
                    "permissionUrl": "23232",
                    "permissionCode": "6cc8ee8ae88d49ceadab8e0bba926a05",
                    "permissionDescription": "系统管理"
                },
                {
                    "permissionType": 2,
                    "permissionUrl": "ccccssss",
                    "permissionCode": "40a03ff44c07436091b1982c12daa96e",
                    "permissionDescription": "3"
                }
            ],
            "permissionUrl": "23232",
            "permissionCode": "6cc8ee8ae88d49ceadab8e0bba926a05",
            "accountcode": "c329bf698013445c9eb61bae67c14d80",
            "permissionDescription": "系统管理"
        },
        {
            "permissionType": 2,
            "permissionParentCode": "505c11e9125d425d92aa1689c718b9a2",
            "children": [
                {
                    "permissionType": 2,
                    "permissionUrl": "二级菜单地址111我QQ翁群",
                    "permissionCode": "9d4ffadcf2584752b438c0dbfef551ac",
                    "permissionDescription": "分销管理的名字暗示法as   "
                }
            ],
            "permissionUrl": "二级菜单地址111我QQ翁群",
            "permissionCode": "9d4ffadcf2584752b438c0dbfef551ac",
            "accountcode": "c329bf698013445c9eb61bae67c14d80",
            "permissionDescription": "分销管理的名字暗示法as   "
        },
        {
            "permissionType": 2,
            "permissionParentCode": "0",
            "children": [
                {
                    "permissionType": 1,
                    "permissionUrl": "23232",
                    "permissionCode": "6cc8ee8ae88d49ceadab8e0bba926a05",
                    "permissionDescription": "系统管理"
                },
                {
                    "permissionType": 2,
                    "permissionUrl": "ccccssss",
                    "permissionCode": "40a03ff44c07436091b1982c12daa96e",
                    "permissionDescription": "3"
                }
            ],
            "permissionUrl": "ccccssss",
            "permissionCode": "40a03ff44c07436091b1982c12daa96e",
            "accountcode": "c329bf698013445c9eb61bae67c14d80",
            "permissionDescription": "3"
        }
    ]
}

备注:记录如今的方法,考虑子查询直接获取前端传入参数

相关文章
相关标签/搜索