EasyUI——tree组件(后端)

工具类及实体类

基于mvc框架

Menu实体类

package com.rong.entity;

public class Menu {

	private String menuid;
	private String menuname;
	private String menuURL;
	private String parentid;
	
	public String getMenuid() {
		return menuid;
	}
	public void setMenuid(String menuid) {
		this.menuid = menuid;
	}
	public String getMenuname() {
		return menuname;
	}
	public void setMenuname(String menuname) {
		this.menuname = menuname;
	}
	public String getMenuURL() {
		return menuURL;
	}
	public void setMenuURL(String menuURL) {
		this.menuURL = menuURL;
	}
	public String getParentid() {
		return parentid;
	}
	public void setParentid(String parentid) {
		this.parentid = parentid;
	}
	
	public Menu() {
		super();
	}
	public Menu(String menuid, String menuname, String menuURL, String parentid) {
		super();
		this.menuid = menuid;
		this.menuname = menuname;
		this.menuURL = menuURL;
		this.parentid = parentid;
	}
	
	@Override
	public String toString() {
		return "Menu [menuid=" + menuid + ", menuname=" + menuname + ", menuURL=" + menuURL + ", parentid=" + parentid
				+ "]";
	}
	
}

BuildTree工具类

package com.rong.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rong.vo.TreeVo;

public class BuildTree {

	/**
	 * 默认-1为顶级节点
	 * @param nodes
	 * @param <T>
	 * @return
	 */
	public static <T> TreeVo<T> build(List<TreeVo<T>> nodes) {
		if (nodes == null) {
			return null;
		}
		List<TreeVo<T>> topNodes = new ArrayList<TreeVo<T>>();
		for (TreeVo<T> children : nodes) {
			String pid = children.getParentId();
			if (pid == null || "-1".equals(pid)) {
				topNodes.add(children);
				continue;
			}
			for (TreeVo<T> parent : nodes) {
				String id = parent.getId();
				if (id != null && id.equals(pid)) {
					parent.getChildren().add(children);
					children.setHasParent(true);
					parent.setChildren(true);
					continue;
				}
			}
		}
		//问题:数据库没有顶级节点
		TreeVo<T> root = new TreeVo<T>();
		if (topNodes.size() == 1) {
			//有顶级节点
			root = topNodes.get(0);
		} else {//没有顶级节点
			root.setId("000");
			root.setParentId("-1");
			root.setHasParent(false);
			root.setChildren(true);
			root.setChecked(true);
			root.setChildren(topNodes);
			root.setText("顶级节点");
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			root.setState(state);
		}
		return root;
	}

	/**
	 * 指定idparam为顶级节点
	 * @param nodes
	 * @param idParam
	 * @param <T>
	 * @return
	 */
	public static <T> List<TreeVo<T>> buildList(List<TreeVo<T>> nodes, String idParam) {
		if (nodes == null) {
			return null;
		}
		List<TreeVo<T>> topNodes = new ArrayList<TreeVo<T>>();

		for (TreeVo<T> children : nodes) {

			String pid = children.getParentId();
			if (pid == null || idParam.equals(pid)) {
				topNodes.add(children);

				continue;
			}

			for (TreeVo<T> parent : nodes) {
				String id = parent.getId();
				if (id != null && id.equals(pid)) {
					parent.getChildren().add(children);
					children.setHasParent(true);
					parent.setChildren(true);

					continue;
				}
			}

		}
		return topNodes;
	}

}

dao层调用

TreeVo

package com.rong.vo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class TreeVo<T> {
	/**
	 * 节点ID
	 */
	private String id;
	/**
	 * 显示节点文本
	 */
	private String text;
	/**
	 * 节点状态,open closed
	 */
	private Map<String, Object> state;
	/**
	 * 节点是否被选中 true false
	 */
	private boolean checked = false;
	/**
	 * 节点属性
	 */
	private Map<String, Object> attributes;

	/**
	 * 节点的子节点
	 */
	private List<TreeVo<T>> children = new ArrayList<TreeVo<T>>();

	/**
	 * 父ID
	 */
	private String parentId;
	/**
	 * 是否有父节点
	 */
	private boolean hasParent = false;
	/**
	 * 是否有子节点
	 */
	private boolean hasChildren = false;

	public String getId() {
		return id;
	}

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

	public String getText() {
		return text;
	}

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

	public Map<String, Object> getState() {
		return state;
	}

	public void setState(Map<String, Object> state) {
		this.state = state;
	}

	public boolean isChecked() {
		return checked;
	}

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

	public Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

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

	public void setChildren(List<TreeVo<T>> children) {
		this.children = children;
	}

	public boolean isHasParent() {
		return hasParent;
	}

	public void setHasParent(boolean isParent) {
		this.hasParent = isParent;
	}

	public boolean isHasChildren() {
		return hasChildren;
	}

	public void setChildren(boolean isChildren) {
		this.hasChildren = isChildren;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public TreeVo(String id, String text, Map<String, Object> state, boolean checked, Map<String, Object> attributes,
                  List<TreeVo<T>> children, boolean isParent, boolean isChildren, String parentID) {
		super();
		this.id = id;
		this.text = text;
		this.state = state;
		this.checked = checked;
		this.attributes = attributes;
		this.children = children;
		this.hasParent = isParent;
		this.hasChildren = isChildren;
		this.parentId = parentID;
	}

	public TreeVo() {
		super();
	}

}

助手类

都是些MVC的util类,可看前些天的博客。
在这里插入图片描述

dao层

MenuDao

package com.rong.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rong.entity.Menu;
import com.rong.util.BaseDao;
import com.rong.util.BuildTree;
import com.rong.util.PageBean;
import com.rong.vo.TreeVo;

public class MenuDao extends BaseDao<Menu>{
	
	public List<Menu> list(Menu menu,PageBean pagebean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_menu";
		return super.executeQuery(sql, Menu.class, pagebean);
	}
	
	public TreeVo<Menu> topNode(Menu menu,PageBean pagebean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Menu> list=this.list(menu, pagebean);
		//通过工具类完成指定格式输出
		List<TreeVo<Menu>> nodes=new ArrayList<TreeVo<Menu>>();
		//将List<T>转换成List<TreeVo<T>>
		TreeVo treeVo=null;
		for (Menu m: list) {
			treeVo=new TreeVo<>();
			treeVo.setId(m.getMenuid()+"");
			treeVo.setText(m.getMenuname());
			treeVo.setParentId(m.getParentid()+"");
			Map<String, Object> attributes=new HashMap<String, Object>();
			treeVo.setAttributes(attributes);
			nodes.add(treeVo);
		}
		return BuildTree.build(nodes);
	}
	
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, SQLException, JsonProcessingException {
		MenuDao pdao=new MenuDao();
		List<Menu> list=pdao.list(null, null);
		//通过工具类完成指定格式输出
		List<TreeVo<Menu>> nodes=new ArrayList<TreeVo<Menu>>();
		//将List<T>转换成List<TreeVo<T>>
		TreeVo treeVo=null;
		for (Menu m: list) {
			treeVo=new TreeVo<>();
			treeVo.setId(m.getMenuid()+"");
			treeVo.setText(m.getMenuname());
			treeVo.setParentId(m.getParentid()+"");
			Map<String, Object> attributes=new HashMap<String, Object>();
			treeVo.setAttributes(attributes);
			nodes.add(treeVo);
		}
		TreeVo<Menu> parent=BuildTree.build(nodes);
		
		ObjectMapper om=new ObjectMapper();
		String jsonstr=om.writeValueAsString(parent);
		System.out.println(jsonstr);
	}
}

MenuDao运行测试

看到这样一串字符串就获取到了数据库值

{"id":"000","text":"菜单管理","state":null,"checked":false,"attributes":{},"children":[{"id":"004","text":"统一配置","state":null,"checked":false,"attributes":{},"children":[{"id":"004004","text":"人员管理","state":null,"checked":false,"attributes":{},"children":[{"id":"004004001","text":"角色维护","state":null,"checked":false,"attributes":{},"children":[],"parentId":"004004","hasParent":true,"hasChildren":false},{"id":"004004004","text":"权限信息维护","state":null,"checked":false,"attributes":{},"children":[],"parentId":"004004","hasParent":true,"hasChildren":false},{"id":"004004003","text":"人员信息维护","state":null,"checked":false,"attributes":{},"children":[],"parentId":"004004","hasParent":true,"hasChildren":false},{"id":"004004002","text":"工作组维护","state":null,"checked":false,"attributes":{},"children":[],"parentId":"004004","hasParent":true,"hasChildren":false}],"parentId":"004","hasParent":true,"hasChildren":true},{"id":"004001","text":"数据字典","state":null,"checked":false,"attributes":{},"children":[],"parentId":"004","hasParent":true,"hasChildren":false},{"id":"004003","text":"权限管理","state":null,"checked":false,"attributes":{},"children":[{"id":"004003003","text":"用户权限管理","state":null,"checked":false,"attributes":{},"children":[],"parentId":"004003","hasParent":true,"hasChildren":false},{"id":"004003002","text":"组权限管理","state":null,"checked":false,"attributes":{},"children":[],"parentId":"004003","hasParent":true,"hasChildren":false},{"id":"004003001","text":"角色权限管理","state":null,"checked":false,"attributes":{},"children":[],"parentId":"004003","hasParent":true,"hasChildren":false}],"parentId":"004","hasParent":true,"hasChildren":true},{"id":"004002","text":"系统参数配置","state":null,"checked":false,"attributes":{},"children":[],"parentId":"004","hasParent":true,"hasChildren":false}],"parentId":"000","hasParent":true,"hasChildren":true},{"id":"002","text":"后勤管理","state":null,"checked":false,"attributes":{},"children":[{"id":"002004","text":"宿舍日常报销","state":null,"checked":false,"attributes":{},"children":[{"id":"002004002","text":"大件购置","state":null,"checked":false,"attributes":{},"children":[],"parentId":"002004","hasParent":true,"hasChildren":false},{"id":"002004001","text":"维修","state":null,"checked":false,"attributes":{},"children":[],"parentId":"002004","hasParent":true,"hasChildren":false}],"parentId":"002","hasParent":true,"hasChildren":true},{"id":"002001","text":"宿舍管理","state":null,"checked":false,"attributes":{},"children":[],"parentId":"002","hasParent":true,"hasChildren":false},{"id":"002005","text":"后勤统计","state":null,"checked":false,"attributes":{},"children":[],"parentId":"002","hasParent":true,"hasChildren":false},{"id":"002002","text":"水电费","state":null,"checked":false,"attributes":{},"children":[],"parentId":"002","hasParent":true,"hasChildren":false},{"id":"002003","text":"房屋租金","state":null,"checked":false,"attributes":{},"children":[{"id":"002003002","text":"租金信息","state":null,"checked":false,"attributes":{},"children":[],"parentId":"002003","hasParent":true,"hasChildren":false},{"id":"002003001","text":"租房合同","state":null,"checked":false,"attributes":{},"children":[],"parentId":"002003","hasParent":true,"hasChildren":false}],"parentId":"002","hasParent":true,"hasChildren":true}],"parentId":"000","hasParent":true,"hasChildren":true},{"id":"003","text":"财务","state":null,"checked":false,"attributes":{},"children":[{"id":"003003","text":"住宿费","state":null,"checked":false,"attributes":{},"children":[{"id":"003003002","text":"水电费","state":null,"checked":false,"attributes":{},"children":[],"parentId":"003003","hasParent":true,"hasChildren":false},{"id":"003003001","text":"租金","state":null,"checked":false,"attributes":{},"children":[],"parentId":"003003","hasParent":true,"hasChildren":false}],"parentId":"003","hasParent":true,"hasChildren":true},{"id":"003002","text":"成考费","state":null,"checked":false,"attributes":{},"children":[],"parentId":"003","hasParent":true,"hasChildren":false},{"id":"003001","text":"学费","state":null,"checked":false,"attributes":{},"children":[{"id":"003001002","text":"升学学费","state":null,"checked":false,"attributes":{},"children":[],"parentId":"003001","hasParent":true,"hasChildren":false},{"id":"003001001","text":"开学学费","state":null,"checked":false,"attributes":{},"children":[],"parentId":"003001","hasParent":true,"hasChildren":false}],"parentId":"003","hasParent":true,"hasChildren":true}],"parentId":"000","hasParent":true,"hasChildren":true},{"id":"001","text":"学生管理","state":null,"checked":false,"attributes":{},"children":[{"id":"001005","text":"就业信息","state":null,"checked":false,"attributes":{},"children":[],"parentId":"001","hasParent":true,"hasChildren":false},{"id":"001003","text":"缴费信息","state":null,"checked":false,"attributes":{},"children":[],"parentId":"001","hasParent":true,"hasChildren":false},{"id":"001001","text":"学生相关信息","state":null,"checked":false,"attributes":{},"children":[],"parentId":"001","hasParent":true,"hasChildren":false},{"id":"001004","text":"表现相关信息","state":null,"checked":false,"attributes":{},"children":[],"parentId":"001","hasParent":true,"hasChildren":false},{"id":"001002","text":"班级相关信息","state":null,"checked":false,"attributes":{},"children":[],"parentId":"001","hasParent":true,"hasChildren":false}],"parentId":"000","hasParent":true,"hasChildren":true}],"parentId":"-1","hasParent":false,"hasChildren":true}

猜你喜欢

转载自blog.csdn.net/ZhiCun_java/article/details/106908389