mybatis 树形结构参数传递

多参数传递

第一、定义实体类

package com.jsh.erp.datasource.vo;

import java.util.List;

/**
 * Description
 *
 * @Author: qiankunpingtai
 * @Date: 2019/3/13 18:10
 */
public class TreeNodeEx {
    /**
     * id主键
     * */
    private Long id;
    /**
     * text显示的文本
     * */
    private String text;
    /**
     *state节点状态,'open' 或 'closed',默认:'open'。如果为'closed'的时候,将不自动展开该节点。
     * */
    private String state="open";
    /**
     *iconCls 节点图标id
     * */
    private String iconCls;
    /**
     * checked 是否被选中
     * */
    private boolean checked;
    /**
     *attributes 自定义属性
     * */
    private NodeAttributes attributes;
    /**
     * children 子节点
     * */
    private List<TreeNode> children;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getIconCls() {
        return iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public NodeAttributes getAttributes() {
        return attributes;
    }

    public void setAttributes(NodeAttributes attributes) {
        this.attributes = attributes;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }
}
package com.jsh.erp.datasource.vo;

/**
 * Description
 *
 * @Author: qiankunpingtai
 * @Date: 2019/3/13 18:11
 */
public class NodeAttributes {
    //编号
    private String no;
    //类型
    private Integer type;

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }
}

第二、定义 map

List<TreeNodeEx> getNodeTree();
List<TreeNodeEx> getNextNodeTree(Map<String, Object> parameterMap);
<resultMap id="BaseTreeResultMap" type="com.jsh.erp.datasource.vo.TreeNodeEx">
    <result column="id" property="id"/>
    <result column="text" property="text"/>
    <association property="attributes" javaType="com.jsh.erp.datasource.vo.NodeAttributes">
        <id column="orgNo" property="no"></id>
        <result column="type" property="type"></result>
    </association>
    <collection column="{orgId=id,orgNo=orgNo}" property="children" javaType="java.util.ArrayList"
                ofType="com.jsh.erp.datasource.vo.TreeNode" select="getNextNodeTree"/>
</resultMap>

<resultMap id="NextTreeResultMap" type="com.jsh.erp.datasource.vo.TreeNodeEx">
    <result column="id" property="id"/>
    <result column="text" property="text"/>
    <result column="iconCls" property="iconCls"/>
    <association property="attributes" javaType="com.jsh.erp.datasource.vo.NodeAttributes">
        <id column="orgNo" property="no"></id>
        <result column="type" property="type"></result>
    </association>
    <collection column="{orgId=id,orgNo=orgNo}" property="children" javaType="java.util.ArrayList"
                ofType="com.jsh.erp.datasource.vo.TreeNode" select="getNextNodeTree"/>
</resultMap>

<select id="getNextNodeTree" resultMap="NextTreeResultMap">
    select id ,    text,orgNo,sort ,iconCls,type from (
        SELECT
        org.id, org.org_abr as text,org.org_no as orgNo,org.sort as sort,null as iconCls,'0' as type
        FROM jsh_organization org
        WHERE org.org_parent_no = #{orgNo}
        and org.org_stcd !='5'
        union all
        select
        user.id,user.username as text, null as orgNo,rel.user_blng_orga_dspl_seq as sort,'icon-user' as iconCls,'1' as type
        from jsh_user user,jsh_orga_user_rel rel
        where
        1=1
        and user.id=rel.user_id
        and rel.orga_id=#{orgId}
        and rel.delete_flag !='1'
        and user.status not in ('1','2')
      ) node
      order by sort asc
</select>

<select id="getNodeTree" resultMap="BaseTreeResultMap">
    SELECT
     id, org_abr as text,org_no as orgNo,'0' as type
    FROM jsh_organization
    WHERE org_parent_no = -1
    and org_stcd !='5'
    order by sort asc
</select>

第三、将 list 转化为 json

@RequestMapping("/getOrganizationUserTree")
public JSONArray getOrganizationUserTree()throws Exception{
    JSONArray arr=new JSONArray();
    List<TreeNodeEx> organizationUserTree= userService.getOrganizationUserTree();
    if(organizationUserTree!=null&&organizationUserTree.size()>0){
        for(TreeNodeEx node:organizationUserTree){
            String str=JSON.toJSONString(node);
            JSONObject obj=JSON.parseObject(str);
            arr.add(obj) ;
        }
    }
    return arr;
}

初始参数直传到底

第一、controller 传入初始参数

@RequestMapping(value = "/getMaterialCategoryTree")
public JSONArray getMaterialCategoryTree(@RequestParam("id") Long id) throws Exception{
   JSONArray arr=new JSONArray();
   List<TreeNode> materialCategoryTree = materialCategoryService.getMaterialCategoryTree(id);
   if(materialCategoryTree!=null&&materialCategoryTree.size()>0){
       for(TreeNode node:materialCategoryTree){
           String str=JSON.toJSONString(node);
           JSONObject obj=JSON.parseObject(str);
           arr.add(obj) ;
       }
   }
    return arr;
}

第二、接口封装参数

List<TreeNode> getNodeTree(@Param("currentId")Long currentId);
List<TreeNode> getNextNodeTree(Map<String, Object> parameterMap);

第三、map 接收参数并在查询时传递参数

<resultMap id="BaseTreeResultMap" type="com.jsh.erp.datasource.vo.TreeNode">
    <result column="id" property="id"/>
    <result column="name" property="text"/>
    <collection column="{currentId=currentId,id=id}" property="children" javaType="java.util.ArrayList"
                ofType="com.jsh.erp.datasource.vo.TreeNode" select="getNextNodeTree"/>
</resultMap>

<resultMap id="NextTreeResultMap" type="com.jsh.erp.datasource.vo.TreeNode">
    <result column="id" property="id"/>
    <result column="name" property="text"/>
    <collection column="{currentId=currentId,id=id}" property="children" javaType="java.util.ArrayList"
                ofType="com.jsh.erp.datasource.vo.TreeNode" select="getNextNodeTree"/>
</resultMap>

<sql id="Base_Column_List">
    id, name
</sql>

<select id="getNextNodeTree" resultMap="NextTreeResultMap">
    SELECT
    <include refid="Base_Column_List"/>,#{currentId} as currentId
    FROM jsh_materialcategory
    WHERE ParentId = #{id}
    <if test="currentId != null">
        and id !=#{currentId}
    </if>
    and status !='2'
    order by sort asc
</select>

<select id="getNodeTree" resultMap="BaseTreeResultMap">
    SELECT
    <include refid="Base_Column_List"/>,#{currentId} as currentId
    FROM jsh_materialcategory
    WHERE ParentId = -1
    and status !='2'
    <if test="currentId != null">
        and id !=#{currentId}
    </if>
    order by sort asc
</select>