zoukankan      html  css  js  c++  java
  • mybatis的collection建立1对多关系 II

    现在是时候做一个复杂点儿的嵌套式(我觉得应该叫串联式)的搜索了.

    首先, 1个user, 有多个role, 也就是说, 一个user对象, 有一个roleList, 然后一个role可能对应多个privilege的url, 即一个role对象, 里面有一个privilegeList, 如下:

    public class SysUser {
    ......
        private List<SysRole> roleList;
    ......
    }
    
    
    public class SysRole {
    ......
        private List<SysPrivilege> privilegeList;
    ......
    }

    然后分别在mapper的xml文件里面, 加上resultMap:

    首先是user的mapper:

        <resultMap id="userRoleListMapWithPrivilege" extends="userMap" type="marc.mybatis.lesson1.model.SysUser">
            <collection property="roleList" columnPrefix="role_" resultMap="marc.mybatis.lesson1.mapper.SysRoleMapper.privilegeMapWithRoleList" />
        </resultMap>

    这个collection里面指向一个roleList, 类型是RoleMapper里面的一个resultMap:

        <resultMap type="marc.mybatis.lesson1.model.SysRole" id="privilegeMapWithRoleList" extends="roleMap">
            <collection property="privilegeList" columnPrefix="privilege_" resultMap="marc.mybatis.lesson1.mapper.PrivilegeMapper.privilegeMap" />
        </resultMap>

    这个collection同样指向model里面的list: privilegeList

    最重要的一件事, select语句里面注意所有的prefix:

        <select id="selectUserWithRolePrivielgeDetail" resultMap="userRoleListMapWithPrivilege">
            select
            u.id,u.user_name,u.user_password,u.user_email,u.user_info,u.head_img,u.create_time,r.id
            role_id,r.role_name role_role_name, r.enabled role_enabled,r.create_by
            role_create_by,r.create_time role_create_time,p.id
            role_privilege_id,p.privilege_name
            role_privilege_privilege_name,p.privilege_url
            role_privilege_privilege_url from sys_user u inner join sys_user_role
            ur on u.id=ur.user_id inner join sys_role r on ur.role_id=r.id inner
            join sys_role_privilege rp on rp.role_id=r.id inner join sys_privilege
            p on p.id=rp.privilege_id where u.id=#{id}
        </select>

    注意highlight出来的一段, column就是通过下划线一点点组合成list的...MyBatis有一套.

  • 相关阅读:
    MySQL distinct 与 group by 去重(where/having)
    mysql 的垂直分表和水平分表
    查看mysql语句运行时间
    PHP的性能优化方法总结
    Apache ab 压测工具使用说明
    LNMP 性能优化之 PHP 性能优化
    [PHP]日志处理error_log()函数和配置使用
    cocos2dx之tolua++全面分析(二):类注册
    在命令行上启动genymotion虚拟机
    在64位ubuntu上安装alienbrain客户端
  • 原文地址:https://www.cnblogs.com/Montauk/p/9786781.html
Copyright © 2011-2022 走看看