多对多查询

先决条件一:多对多需要一种中间表建立连接关系;
先决条件二:多对多关系是由两个一对多关系组成的,一对多可以也可以用两种方式实现;

<resultMap type="TUser" id="userRoleInfo" extends="BaseResultMap">
        <collection property="roles" ofType="TRole" columnPrefix="role_">
            <result column="id" property="id" />
            <result column="Name" property="roleName" />
            <result column="note" property="note" />
        </collection>
    </resultMap>
    
    
    <select id="selectUserRole" resultMap="userRoleInfo">
        select a.id, 
              a.user_name,
              a.real_name,
              a.sex,
              a.mobile,
              a.note,
              b.role_id,
              c.role_name,
              c.note role_note
        from t_user a,
             t_user_role b,
             t_role c
        where a.id = b.user_id AND 
              b.role_id = c.id
     </select>    

 

posted @ 2019-12-10 16:34  MartinEDM  阅读(575)  评论(0编辑  收藏  举报