一、表结构(hr_deptment)
hr_deptment CREATE TABLE `hr_deptment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(256) COLLATE utf8_bin NOT NULL,
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
package com.sms.controller;
二、前台使用jstree(test.jsp)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="js/jstree/style.min.css">
<script src="js/jstree/jquery-3.2.0.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jstree/jstree.min.js"></script>
</head>
<body>
<div id="container"></div>
<script>
$(function() {
alert("ddddd");
$('#container').jstree({
'core' : {
'data' : {
"url" : "/sMsApp/hrdeptment/getHrDeptmentTree",
"dataType" : "json" // needed only if you do not supply JSON headers
}
}
});
});
</script>
</body>
</html>
三、java代码
1.domain(HrDeptment.java)
package com.sms.domain;
public class HrDeptment implements java.io.Serializable {
private Integer id ;
private Integer organizationId ;
private String name;
private Integer parentId ;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getOrganizationId() {
return organizationId;
}
public void setOrganizationId(Integer organizationId) {
this.organizationId = organizationId;
}
public String getName() {
return name;
}
public void setName(String deptmentName) {
this.name = deptmentName;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
}
2.DAO(使用ibatis)(HrDeptmentDao.java)
package com.sms.dao;
import static com.sms.util.common.SmsConstants.HRDEPTMENT;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.sms.domain.HrDeptment;
public interface HrDeptmentDao {
//通过id查询部门
@Select("select * from " + HRDEPTMENT + " where id = #{id}")
HrDeptment selectById(int id) ;
//选择所有根结点
@Select("select * from " + HRDEPTMENT + " where parent_id is null")
List<HrDeptment> selectRoot() ;
//查询所有子结点
@Select("select * from " + HRDEPTMENT + " where parent_id = #{parent_id} ")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="parent_id",property="parentId")
})
List<HrDeptment> selectChilrenNode(int parentId) ;
//查询所有部门
@Select("select * from " + HRDEPTMENT )
List<HrDeptment> selectNodeAll() ;
}
3.service(HrService.java 接口)
public HrDeptment findById(Integer id) ;
public List<HrDeptment> findHrDeptmentChildren(Integer parentId) ;
public List<HrDeptment>findHrDeptmentAll();
public List<HrDeptment> findHrDeptmentRoot() ;
import java.util.Iterator;
import java.util.List;
4.service实现(HrServiceImpl.java)
package com.sms.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.sms.dao.HrDeptmentDao;
import com.sms.domain.HrDeptment;
import com.sms.service.HrmService;
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
@Service("hrmService")
public class HrmServiceImpl implements HrmService{
@Autowired
private HrDeptmentDao hrDeptmentDao ;
//HrDeptment的所有操作
@Transactional(readOnly=true)
@Override
public HrDeptment findById(Integer id) {
return hrDeptmentDao.selectById(id) ;
}
@Transactional(readOnly=true)
@Override
public List<HrDeptment> findHrDeptmentChildren(Integer parentId) {
return hrDeptmentDao.selectChilrenNode(parentId) ;
}
@Override
public List<HrDeptment> findHrDeptmentAll(){
return hrDeptmentDao.selectAll() ;
}
@Override
public List<HrDeptment> findHrDeptmentRoot() {
return hrDeptmentDao.selectRoot() ;
}
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sms.domain.HrDeptment;
import com.sms.service.HrmService;
@Controller
public class HrDeptmentController {
@Autowired
@Qualifier("hrmService")
private HrService hrmService;
@RequestMapping(value="/hrdeptment/getHrDeptmentTree")
public String getHrDeptmentTree(HttpServletRequest request, HttpServletResponse response) throws Exception{
response.setContentType("application/json;charset=utf-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter pw = response.getWriter();
String jsons = getHrDeptmentTreeJson();
pw.write(jsons);
return null ;
}
public String getHrDeptmentTreeJson(){
StringBuffer jsonstr = new StringBuffer() ;
if(hrmService.findHrDeptmentAll().size()!=0){
jsonstr.append("[") ;
List<HrDeptment> roots = hrmService.findHrDeptmentRoot() ;
Iterator<HrDeptment> it = roots.iterator() ;
while(it.hasNext()){
HrDeptment hd = it.next() ;
if(hrmService.findHrDeptmentChildren(hd.getId()).size()!=0){
jsonstr.append(getParentNodeJosn(hd));
getChildrenNode(jsonstr , hd) ;
}else{
jsonstr.append(getParentNodeJosn(hd)) ;
}
}
}else{
return null ;
}
jsonstr.append("]}]") ;
return jsonstr.toString() ;
}
public StringBuffer getChildrenNode(StringBuffer strs ,HrDeptment hrDeptment){
StringBuffer strB = new StringBuffer();
List<HrDeptment> hrDeptments = hrmService.findHrDeptmentChildren(hrDeptment.getId()) ;
Iterator<HrDeptment> iter = hrDeptments.iterator() ;
while(iter.hasNext()){
HrDeptment hdChild = iter.next() ;
if(hrmService.findHrDeptmentChildren(hdChild.getId()).size() != 0){
strs.append(getParentNodeJosn(hdChild)) ;
getChildrenNode(strs,hdChild) ;
strs.append("]},") ;
}else{
strs.append(getLeafNodeJosn(hdChild));
}
}
String tempstr = new String();
tempstr = RemoveComma(strs) ;
strs.delete(0,strs.length()) ;
strs.append(tempstr) ;
return strs ;
}
public String getLeafNodeJosn(HrDeptment hrDeptment){
String str = "\"" ;
return "{" +str + "id" +str + ":" + str + hrDeptment.getId() + str +"," + str + "text" +str +":" + str + hrDeptment.getName() + str + "}," ;
}
public String getParentNodeJosn(HrDeptment hrDeptment){
String str = "\"" ;
return"{" +str + "id" +str + ":" + str + hrDeptment.getId() + str +"," + str + "text" +str +":" + str + hrDeptment.getName() + str + "," + str + "children" + str +":[" ;
}
public String RemoveComma(StringBuffer str){
String s = str.toString() ;
s =str.substring(0, str.length() - 1) ;
return s ;
}
}