基于SpringMvc的组织树的实现
Controller 层代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.chengpai.person.bean.bumenMidObject;
import com.chengpai.person.service.bumenService;
import util.tree.Node;
@Controller
public class bumenController {
@Autowired
private bumenService bs;
@RequestMapping(value = "/bumen/testdata1.do")
@ResponseBody
public String testdata1(HttpServletRequest request, HttpServletResponse response) {
List dataList = bs.selectAll();
//dataList.clear();
// 节点列表(散列表,用于临时存储节点对象)
HashMap nodeList = new HashMap();
// 根节点
Node root = null;
// 根据结果集构造节点列表(存入散列表)
for (Iterator it = dataList.iterator(); it.hasNext();) {
Map dataRecord = (Map) it.next();
Node node = new Node();
node.id = (String) dataRecord.get("id");
node.text = (String) dataRecord.get("text");
node.parentId = (String) dataRecord.get("parentId");
nodeList.put(node.id, node);
}
// 构造无序的多叉树
Set entrySet = nodeList.entrySet();
for (Iterator it = entrySet.iterator(); it.hasNext();) {
Node node = (Node) ((Map.Entry) it.next()).getValue();
if (node.parentId == null || node.parentId.equals("")) {
root = node;
} else {
((Node) nodeList.get(node.parentId)).addChild(node);
}
}
// root.sortChildren();
System.out.println("["+root.toString()+"]");
return "["+root.toString()+"]";
}
}
Service层代码:
import java.util.List;
import com.chengpai.person.bean.bumenMidObject;
import com.chengpai.person.bean.bumen;
public interface bumenService {
List<bumen> selectAll();
List<Integer> listRoot();
List<Integer> listRooti(int i);
List<bumenMidObject> Findnodes(int i);
}
ServiceImp层代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chengpai.person.bean.bumenMidObject;
import com.chengpai.person.bean.bumen;
import com.chengpai.person.mapper.bumenMapper;
import com.chengpai.person.service.bumenService;
@Service("bumenService")
public class bumenServiceImp implements bumenService {
@Autowired
private bumenMapper bm;
@Override
public List selectAll() {
// TODO Auto-generated method stub
List<bumen> listbumen= bm.selectAll();
List dataList = new ArrayList();
for (int i = 0; i < listbumen.size(); i++) {
HashMap dataRecord = new HashMap();
if(listbumen.get(i).getFid()==0){
dataRecord.put("id", listbumen.get(i).getId()+"");
dataRecord.put("text", listbumen.get(i).getName());
dataRecord.put("parentId", null);
dataList.add(dataRecord);
}else{
dataRecord.put("id", listbumen.get(i).getId()+"");
dataRecord.put("text", listbumen.get(i).getName());
dataRecord.put("parentId", listbumen.get(i).getFid()+"");
dataList.add(dataRecord);
}
}
return dataList;
}
@Override
public List<Integer> listRoot() {
// TODO Auto-generated method stub
List<Integer> listRoot= new ArrayList<Integer>();
List<bumen> listbumen= bm.selectAll();
for (int i = 0; i < listbumen.size(); i++) {
if(!listRoot.contains(listbumen.get(i).getFid())){
listRoot.add(listbumen.get(i).getFid());
}
}
return listRoot;
}
@Override
public List<Integer> listRooti(int i) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<bumenMidObject> Findnodes(int i) {
// TODO Auto-generated method stub
return null;
}
}
Mapper 层代码实现
import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.chengpai.person.bean.bumen;
public interface bumenMapper {
@Select("select * from bumen")
List<bumen> selectAll();
}
部门实体类:
public class bumen {
private int id;
private String name;
private int fid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
}