Mybatis 多对多关系映射

做练习时发现,项目中很多实体类之间是多对多的关系,所以记录一下SSM项目中,多对多的配置方法。
比如,我们做权限管理系统,RBAC2,用户与角色,角色与模块之间全部是多对多的关系,正确得配置好它们,可节约开发时间,提高开发效率。
用户实体类:

public class Admin {
    private Integer id;

    private String username;

    private String realname;

    private String password;

    private String email;

    private Date createTime;

    private String mobilePhone;

    private Boolean isSuperadmin;

    private String status;

    private List<Roles>  roles;

角色实体类:

public class Roles {
    private Integer id;

    private String name;

    private List<Admin> admins;   //留有后用

    private List<Menus>  menus;

需要实现的Mapper中的方法

/**
     * 根据登录用户的ID查找其有所有Role
     * @param id
     * @return
     */
    Admin getAdminByIdWithRoles(int id);

对应的配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.crm.auth.dao.AdminAuthMapper">


    <!-- Admin 表中和Role表中都有ID,所以不能用继承 <collection property="roles" resultMap="com.crm.auth.dao.RolesMapper.BaseResultMap"></collection> -->
    <resultMap id="AdminResultWithRoles" type="com.crm.auth.entity.Admin"
        extends="com.crm.auth.dao.AdminMapper.BaseResultMap">
        <collection property="roles" ofType="com.crm.auth.entity.Roles">
            <id column="rid" property="id" jdbcType="INTEGER" />
            <result column="name" property="name" jdbcType="VARCHAR" />

            <collection property="menus" ofType="com.crm.auth.entity.Menus">
                <id column="mid" property="id" jdbcType="INTEGER" />
                <result column="mname" property="name" jdbcType="VARCHAR" />
                <result column="url" property="url" jdbcType="VARCHAR" />
                <result column="function" property="function" jdbcType="VARCHAR" />
                <result column="is_parent" property="isParent" jdbcType="BIT" />
                <result column="mstatus" property="status" jdbcType="VARCHAR" />
            </collection>
        </collection>
    </resultMap>

    <select id="getAdminByIdWithRoles" parameterType="int"
        resultMap="AdminResultWithRoles">
        SELECT admin_user.*,
        roles.id as rid,roles.`name`,
        menus.id as
        mid,menus.`function`,menus.is_parent,menus.`name` as mname,
        menus.`status` as mstatus,menus.url
        FROM admin_user,role_users,roles        ,roles_menus,menus
        where
        admin_user.id=#{id}
        and role_users.user_id=#{id}
        and roles.id=role_users.role_id
        and roles_menus.role_id=roles.id
        and menus.id=roles_menus.menu_id
    </select>


</mapper>

更多知识可学习大神博客:
mybatis高级映射多对多查询实现 - 简书 http://www.jianshu.com/p/58b92011130b

MyBatis系列:Mapper 映射 之 关联对象属性及延迟加载-布布扣-bubuko.com
http://www.bubuko.com/infodetail-939914.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值