一星权限目前市场已经没有人再用了,但是也是所有高星权限的一个基础
这个图的理解应该很简单,就是从用户表中拿到menuId,再到menu菜单表中进行查询,但是怎么把查出来的list数据转成json数据就有一点点难理解了。代码如下
package com.fairy.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fairy.entity.TreeNode;
import com.fairy.util.JsonBaseDao;
import com.fairy.util.JsonUtil;
import com.fairy.util.PageBean;
import com.fairy.util.StringUtils;
public class MenuTreeDao extends JsonBaseDao{
public List<Map<String, Object>> menuList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_menu where true";
String menuId=JsonUtil.getparamMap(paramMap, "Menuid");
if (StringUtils.isNotBlank(menuId)) {
sql+=" and parentid="+menuId;
}else{
sql+=" and parentid=-1";
}
return super.executeQuery(sql, null);
}
private void mapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
treeNode.setId(map.get("Menuid").toString());
treeNode.setText(map.get("Menuname").toString());
treeNode.setAttributes(map);
//treeNode.setChildren(children);
Map<String, String[]> paramMap=new HashMap<>();
//把当前节点当作父id,查出所有子节点
paramMap.put("Menuid", new String[] {treeNode.getId()});
List<Map<String, Object>> menuList=this.menuList(paramMap, null);
List<TreeNode> treeNodeList=new ArrayList<>();
mapListToTreeNodeList(menuList, treeNodeList);
treeNode.setChildren(treeNodeList);
}
public void mapListToTreeNodeList(List<Map<String, Object>> list,List<TreeNode> treeNodeList) throws InstantiationException, IllegalAccessException, SQLException {
TreeNode treeNode=null;
for (Map<String, Object> map : list) {
treeNode=new TreeNode();
mapToTreeNode(map, treeNode);
treeNodeList.add(treeNode);
}
}
/**
* 此方法的返回值才是符合easyui树形的组建所需要的json格式
* @param paramMap
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<TreeNode> getEndList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
List<Map<String, Object>> menuList=this.menuList(paramMap, null);
List<TreeNode> treeNodeList=new ArrayList<>();
mapListToTreeNodeList(menuList, treeNodeList);
return treeNodeList;
}
}
调用时的方法:
List<TreeNode> endList = this.mtDao.getEndList(req.getParameterMap(), null);
ObjectMapper om=new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(endList));
ResponseUtil是个方法类:
package com.fairy.util;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class ResponseUtil {
public static void write(HttpServletResponse resp,Object obj) throws IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println(obj.toString());
out.flush();
out.close();
}
}