使用递归展示无极限树

公司需求是 根据当前员工查找他对应整个分公司及部门的名字,思路:根据员工查找出他属于哪个分公司,拿到分公司PID(可以理解为是子公司的父节点ID),再使用递归方法依次查找,(在递归方法中只要你传父节点ID,不管其子下有没有子节点),
效果图:



现在贴代码:

package com.neusoft.talentbase.organization.orgunit.orgunit.vo; import java.util.List; import com.neusoft.talentbase.framework.core.base.vo.BaseVO; public class MyOrgUnitVO extends BaseVO { /** * 组织单元ID */ private long orgid; /** * 组织单元名称 */ private String orgName; private List list; //存放子节点 private String open; // 在前台把当前节点展开 /** * 父节点ID */ private long pid; private int isCost; private int isZhangtao; private int type; public int getType() { return type; } public void setType(int type) { this.type = type; } public int getIsCost() { return isCost; } public void setIsCost(int isCost) { this.isCost = isCost; } public int getIsZhangtao() { return isZhangtao; } public void setIsZhangtao(int isZhangtao) { this.isZhangtao = isZhangtao; } public long getPid() { return pid; } public void setPid(long pid) { this.pid = pid; } public List getList() { return list; } public void setList(List list) { this.list = list; } public long getOrgid() { return orgid; } public void setOrgid(long orgid) { this.orgid = orgid; } public String getOrgName() { return orgName; } public void setOrgName(String orgName) { this.orgName = orgName; } public String getOpen() { return open; } public void setOpen(String open) { this.open = open; } }


package com.neusoft.talentbase.organization.orgunit.orgunit.dao; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.hibernate.Hibernate; import com.neusoft.talentbase.framework.core.base.dao.BaseDAOImpl; import com.neusoft.talentbase.organization.orgunit.orgunit.vo.MyOrgUnitVO; /** * *{} *@author tangjia *@created 2013-10-23 下午01:56:24 *@lastModified *@history */ public class MyOrgUnitDAOImpl extends BaseDAOImpl implements MyOrgUnitDAO{ public List getOrgUnitTree(long unitId,boolean flag) { String sql=""; if(flag) { sql="select o.c_name as name ,u.c_orgunitid as unitid ,i.c_iscost as iscost ,o.c_type as type "+ " from tb_org_unitrelation u, tb_org_orgunit o,tb_iscostcentre i "+ " where u.c_orgunitid = o.c_oid_orgunit and u.c_orgunitid=i.c_unitid "+ " and u.c_parentunitid in " + " (select u.c_parentunitid from tb_org_unitrelation u where u.c_orgunitid="+unitId+")" + "and u.c_status=1 and i.c_status=1 and o.c_status=1 and u.c_orgunitid="+unitId; } else { sql="select o.c_name as name,u.c_orgunitidas unitid ,i.c_iscost as iscost ,o.c_type as type from " + "tb_org_unitrelation u, tb_org_orgunit o ,tb_iscostcentre iwhere u.c_orgunitid = o.c_oid_orgunit"+ " and u.c_orgunitid=i.c_unitid and i.c_status=1 and o.c_status=1and u.c_status=1 and u.c_parentunitid="+unitId; } List list=this.getSession().createSQLQuery(sql) .addScalar("name", Hibernate.STRING) .addScalar("unitid", Hibernate.LONG) .addScalar("iscost", Hibernate.INTEGER).addScalar("type", Hibernate.INTEGER).list(); List orgUnitlist=new ArrayList(); for (int i = 0; i < list.size(); i++) { Object[] obj=(Object[])list.get(i); //查帐套 String s=" select count(*) as counts from rlship_base rl,tb_org_orgunit_append app ,tb_org_orgunit org "+ " where rl.rrolecode = app.c_org_code "+ "and app.c_oid_orgunit = org.c_oid_orgunit "+ "and rl.typeid = '2000772'"+ "and org.c_oid_orgunit ="+obj[1].toString(); List li=this.getSession().createSQLQuery(s).addScalar("counts", Hibernate.INTEGER).list(); MyOrgUnitVO orgUnit=new MyOrgUnitVO(); orgUnit.setOrgName(obj[0].toString()); orgUnit.setOpen("true"); //这里设为true是因为需求中要把所有的节点打开orgUnit.setPid(Long.valueOf(obj[1].toString())); orgUnit.setIsCost(Integer.parseInt(obj[2].toString())); orgUnit.setType(Integer.parseInt(obj[3].toString())); if(Integer.parseInt(li.get(0).toString())>0) { orgUnit.setIsZhangtao(1); //有帐套 } else { orgUnit.setIsZhangtao(0); //没有帐套 } orgUnitlist.add(orgUnit); } for(MyOrgUnitVO unit:orgUnitlist) { List li=getOrgUnitTree(unit.getPid(),false); unit.setList(li); } return orgUnitlist; } }


/** * *{ 获取该用户的组织树} *@param mapping *@param form *@param request *@param response *@return *@throws Exception *@author tangjia *@created 2013-10-18 下午06:07:17 *@lastModified *@history */ public ActionForward myOrgUnit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //获取操作用户权限 long unitOid = 0; SecurityJdbcDAO securityDAO = (SecurityJdbcDAO) getBean("securityJdbcDAOForAction"); HashMap securityMap = (HashMap) securityDAO .getUserFuncHiberarchyAndRootOrgUnit("no_config_func"); if (securityMap == null || securityMap.get("rootNodeId") == null || securityMap.isEmpty()) {OrgUnitCommonService orgUnitCommonService = (OrgUnitCommonService) getBean("orgUnitCommonService"); unitOid = orgUnitCommonService.getDefaultUnit().getOid(); } else { unitOid = Long.parseLong(String.valueOf(securityMap .get("rootNodeId"))); } //总公司员工对应于寿险总公司 if(unitOid==1||unitOid==21266433){ unitOid=245569460; } MyOrgUnitService myOrgUnitDao=(MyOrgUnitService) getBean("myOrgUnitService"); List list=myOrgUnitDao.getOrgUnitTree(unitOid,true); JSONArray jsonObj = JSONArray.fromObject(list); //转换成json格式,前台用的是ztree控件,解析的数据可以为json格式,ztree控件对数据格式有要求,旗下子节点必须是以children 所以我把javabean里面的list替换成children,还有个name是一样的意思String s=jsonObj.toString().replaceAll("list", "children").replaceAll("orgName", "name"); request.setAttribute("jsonList", s); //System.out.println(jsonObj.toString()); //System.out.println(s); return mapping.findForward("myUnit"); }


成本中心 帐套中心

      【使用递归展示无极限树】 到此就把从数据库中查询出来的数据 ,展示在jsp 树上面了

        推荐阅读