数据库:
<resultMap id="BaseResultMap" type="com.bas.bean.Menu" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="menu_no" property="menuNo" jdbcType="VARCHAR" />
<result column="pid" property="pid" jdbcType="VARCHAR" />
<result column="url" property="url" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="INTEGER" />
<result column="icon" property="icon" jdbcType="VARCHAR" />
<result column="createTime" property="createtime" jdbcType="VARCHAR" />
<result column="updateTime" property="updatetime" jdbcType="VARCHAR" />
<result column="level" property="level" jdbcType="INTEGER" />
</resultMap>
<select id="getAllMenu" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from menu t order by t.pid asc
</select>
package com.bas.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.bas.bean.Menu;
public class TreeUtil {
/**
* 使用占位符拼接字符串
* 建立等级——等级对应的实体个数
* @param menu
* @return
*/
public static String getTreeJson(List<Menu> menu){
//默认最顶级
StringBuffer sb=new StringBuffer();
Map<String,Menu> param=new HashMap<String,Menu>();
//存放 pid——pid个数
List<String> numlist=new ArrayList<String>();
for(Menu vo: menu){
param.put(vo.getId().toString(), vo);
if(numlist.contains(vo.getPid())){
continue;
}else{
numlist.add(vo.getPid());
}
}
String pid="0";
Boolean flag=true;
int i=1;
for(String num:numlist){
i=1;
flag=true;
String len="&"+num+"&";
StringBuffer sbf=new StringBuffer();
if(num.equals("0")){
Menu pvo=param.get("1");
sb=sb.append("{ text :'"+pvo.getName()+"',id :'"+pvo.getId()+"'&"+pvo.getId()+"&}");
param.remove("1");
}else{
for(Menu vo: param.values()){
pid=vo.getPid();
if(num.equals(pid)){
if(sb.indexOf(len)>0){
if(flag){
sb.insert(sb.indexOf(len), ",nodes: [");
sb.insert( sb.indexOf(len)+len.length(), "]");
flag=false;
}
if(i==1){
String s="{ text :'"+vo.getName()+"',id :'"+vo.getId()+"'&"+vo.getId()+"&}";
sbf.append(s);
i=i+1;
continue;
}if(i>1){
String s=",{ text :'"+vo.getName()+"',id :'"+vo.getId()+"'&"+vo.getId()+"&}";
sbf.append(s);
i=i+1;
continue;
}
}
}
}
if(sb.indexOf(len)>0){
sb.replace(sb.indexOf(len),sb.indexOf(len)+len.length() , sbf.toString());
}
}
}
while(sb.indexOf("&")>0){
sb.delete(sb.indexOf("&"), sb.indexOf("&", sb.indexOf("&")+1)+1);
}
return sb.toString();
}
}
@RequestMapping("/menu")
public void getMenu(HttpServletRequest request,HttpServletResponse response) throws Exception{
List<Menu> tree = menuService.getAllMenu();
String json = TreeUtil.getTreeJson(tree);
response.setCharacterEncoding("utf-8");
response.getWriter().write(json);
response.getWriter().close();
}
<title>首页</title>
<script src="${rc.contextPath}/static/util/js/jquery-3.3.1.min.js"></script>
<link href="${rc.contextPath}/static/util/css/bootstrap.min.css" rel="stylesheet"/>
<link href="${rc.contextPath}/static/util/css/bootstrap-treeview.css" rel="stylesheet"/>
<script src="${rc.contextPath}/static/util/js/bootstrap.min.js"></script>
<script src="${rc.contextPath}/static/util/js/bootstrapValidator.js"></script>
<script src="${rc.contextPath}/static/util/js/bootstrap-treeview.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "Post",
url: "${rc.contextPath}/login/menu",
dataType:"text",
success: function (data) {
var tree = eval('['+data+']');
$('#tree').treeview({
data: tree, // 数据源
showCheckbox: true, //是否显示复选框
highlightSelected: true, //是否高亮选中
//nodeIcon: 'glyphicon glyphicon-user', //节点上的图标
nodeIcon: 'glyphicon glyphicon-globe',
emptyIcon: '', //没有子节点的节点图标
multiSelect: false, //多选
onNodeChecked: function (event,data) {
alert(data.nodeId);
},
onNodeSelected: function (event, data) {
alert(data.nodeId);
}
});
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
alert("树形结构加载失败!")
}
});
})