今天为大家介绍一下一星权限,现在市面上已经没有什么人在用一星权限了,只不过后面介绍权限难度的递增,为了大家好理解,我就从一星权限开始介绍:
从图理解开始理解:
首先创建两个数据库表, 用户表中需要有一个menuId,当你登陆时获取到这个menuId,在菜单表中查询是否有这个值,如果有就将查询到的菜单表的数据以json的格式展示在easyui中,最难的部分是转换这里,代码如下:
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是我写的一个方法类
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();
}
}