码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis多对多级联查询

时间:2017-08-18 23:43:10      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:his   this   resultmap   role   分页查询   rtp   result   serial   create   

 

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多对多级联查询

标签:his   this   resultmap   role   分页查询   rtp   result   serial   create   

原文地址:http://www.cnblogs.com/fangts/p/7392405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!