mybatis多对多级联查询

Posted 方大帝的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis多对多级联查询相关的知识,希望对你有一定的参考价值。

 

1.实体

package com.govmade.govdata.modules.sys.pojo;

import java.util.List;

import javax.persistence.Table;
import javax.persistence.Transient;

import com.google.common.collect.Lists;
import com.govmade.govdata.common.persistence.BasePo;

@Table(name = "sys_role")
public class Role extends BasePo<Role> {
    private static final long serialVersionUID = 1L;

    private Integer companyId;

    @Transient
    private String companyName;

    private String name;

    private String enname;

    private Integer useable;

    @Transient
    private String menuIds;

    @Transient
    private List<Menu> menuList = Lists.newArrayList(); // 拥有菜单列表

    public Role() {
        super();
    }

    public Integer getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Integer companyId) {
        this.companyId = companyId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEnname() {
        return enname;
    }

    public void setEnname(String enname) {
        this.enname = enname;
    }

    public Integer getUseable() {
        return useable;
    }

    public void setUseable(Integer useable) {
        this.useable = useable;
    }

    public String getMenuIds() {
        return menuIds;
    }

    public void setMenuIds(String menuIds) {
        this.menuIds = menuIds;
    }
    
    public List<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList) {
        this.menuList = menuList;
    }
}

 

2.RoleService定义

/**
 * 根据分页查询角色列表
 * 
 * @param page
* @return
 */
public List<Role> findList(Page<Role> page) {
    Map<String, Object> params = Maps.newHashMap();
    params.put("delFlag", Global.DEL_FLAG_NORMAL);
    params.put("orderByClause", "id desc");
    page.startPage();
    page.setParams(params);

    List<Role> roleList = roleDao.findList(page);
    return roleList;
}

3.RoleDao定义

public List<Role> findList(Page<Role> page);

4.XML定义

<resultMap id="roleResult" type="Role">
    <id property="id" column="id" />
    <result property="companyId" column="companyId" />
    <result property="companyName" column="companyName" />
    <result property="name" column="name" />
    <result property="enname" column="enname" />
    <result property="remarks" column="remarks" />
    <result property="useable" column="useable" />
    <collection property="menuList" ofType="Menu">
        <id property="id" column="menuList.id" />
    </collection>
</resultMap>

<sql id="roleColumns">
    a.id,
    a.company_id AS "companyId",
    a.name,
    a.enname,
    a.useable,
    a.create_by AS "createBy",
    a.create_date AS "createDate",
    a.update_by AS "updateBy",
    a.update_date AS "updateDate",
    a.remarks,
    a.del_flag AS "delFlag",
    c.name AS "companyName"
</sql>

<select id="findList" resultMap="roleResult">
    SELECT
    <include refid="roleColumns" />,
    rm.menu_id AS "menuList.id"
    FROM (select * from sys_role order by #{params.orderByClause} limit #{startRow}, #{endRow}) a
    LEFT JOIN sys_company c ON c.id = a.company_id
    LEFT JOIN sys_role_menu rm ON rm.role_id = a.id
    WHERE a.del_flag = #{params.delFlag}
</select>  

 

 

 

 

 

 

 

 

 

 

 

 

以上是关于mybatis多对多级联查询的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis一对多级联查询

MyBatis一对一级联查询

java,mybatis 一对多级联查询,怎么给多的一方添加条件啊???

实体框架 CTP5,代码优先。多对多级联删除

MyBatis-记录

Mybatis_多对一关联查询