首页 > 代码库 > mybatis多对多映射

mybatis多对多映射

数据库里面有角色实体类app_cms_role

 

权限实体类app_cms_right

以及一张中间表app_cms_role_right

 

建立对应的实体类AppCmsRole

package com.qianlong.cms.entity;import java.util.Date;import java.util.Set;public class AppCmsRole {    private Integer id;    private String name;    private Date createTime;    private Date updateTime;    private Integer appId;    private String roleName;    private String rolePrivilege;    private Set<AppCmsRight> rightSet;    public Set<AppCmsRight> getRightSet() {        return rightSet;    }    public void setRightSet(Set<AppCmsRight> rightSet) {        this.rightSet = rightSet;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name == null ? null : name.trim();    }    public Date getCreateTime() {        return createTime;    }    public void setCreateTime(Date createTime) {        this.createTime = createTime;    }    public Date getUpdateTime() {        return updateTime;    }    public void setUpdateTime(Date updateTime) {        this.updateTime = updateTime;    }    public Integer getAppId() {        return appId;    }    public void setAppId(Integer appId) {        this.appId = appId;    }    public String getRoleName() {        return roleName;    }    public void setRoleName(String roleName) {        this.roleName = roleName == null ? null : roleName.trim();    }    public String getRolePrivilege() {        return rolePrivilege;    }    public void setRolePrivilege(String rolePrivilege) {        this.rolePrivilege = rolePrivilege == null ? null : rolePrivilege.trim();    }    @Override    public String toString() {        return "AppCmsRole [id=" + id + ", name=" + name + ", createTime=" + createTime + ", updateTime=" + updateTime                + ", appId=" + appId + ", roleName=" + roleName + ", rolePrivilege=" + rolePrivilege + ", rightSet="                + rightSet + "]";    }}

实体类AppCmsRight.java

package com.qianlong.cms.entity;public class AppCmsRight {    private Integer id;    private String name;    private Integer pid;    private String url;    private Short type;    private Short organization;    /* private List<AppCmsRole> rolesList; */    public Integer getPid() {        return pid;    }    public void setPid(Integer pid) {        this.pid = pid;    }    public String getUrl() {        return url;    }    public void setUrl(String url) {        this.url = url == null ? null : url.trim();    }    public Short getType() {        return type;    }    public void setType(Short type) {        this.type = type;    }    public Short getOrganization() {        return organization;    }    public void setOrganization(Short organization) {        this.organization = organization;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "AppCmsRight [id=" + id + ", name=" + name + ", pid=" + pid + ", url=" + url + ", type=" + type                + ", organization=" + organization + "]";    }}

对应的xml

<resultMap id="BaseResultMap" type="com.qianlong.cms.entity.AppCmsRole">        <id column="id" property="id" jdbcType="INTEGER" />        <result column="name" property="name" jdbcType="VARCHAR" />        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />        <result column="app_id" property="appId" jdbcType="INTEGER" />        <result column="role_name" property="roleName" jdbcType="VARCHAR" />        <result column="role_privilege" property="rolePrivilege"            jdbcType="VARCHAR" />    </resultMap>    <resultMap id="RoleRgiht" type="com.qianlong.cms.entity.AppCmsRole"        extends="BaseResultMap">        <collection property="rightSet" javaType="java.util.Set"<!-- 实体类里面如果是listjavaType就写成java.util.List-->            ofType="com.qianlong.cms.entity.AppCmsRight">            <id column="righrId" property="id" jdbcType="INTEGER" />            <result column="rightName" property="name" jdbcType="VARCHAR" /><!--查询出来的id与role里面的重复,所以改写成别名,防止查询冲突-->            <result column="pid" property="pid" jdbcType="INTEGER" />            <result column="url" property="url" jdbcType="VARCHAR" />            <result column="type" property="type" jdbcType="SMALLINT" />            <result column="organization" property="organization"                jdbcType="SMALLINT" />        </collection>    </resultMap><select id="selectRolePage" parameterType="HashMap" resultMap="RoleRgiht">        select        ro.id, ro.name, ro.create_time, ro.update_time, ro.app_id,        ro.role_name,        ro.role_privilege,ri.id as rightId,ri.name as        rightName,ri.pid,ri.url,ri.type,ri.organization        from app_cms_role ro        left join app_cms_role_right rr on ro.id=rr.role_id        left join        app_cms_right ri on ri.id=rr.right_id</selecy>

 

mybatis多对多映射