欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

网上书城(权限管理)

程序员文章站 2024-01-24 23:12:10
权限管理分析实现代码效果图分析就是传入id in (…);1、通过当前用户的账号获取角色类别String sql2 = “select * from t_easyui_role_permission where rid = ?”;上面就获取到了pid的集合2、给sql1做拼接id in (pids);//pids是通过pid的集合拼成的“,”分割的字符串实现代码权限实体类package com.luojun.entity;public class Permission { p...

分析

就是传入id in (…);
1、通过当前用户的账号获取角色类别
String sql2 = “select * from t_easyui_role_permission where rid = ?”;
上面就获取到了pid的集合
2、给sql1做拼接id in (pids);//pids是通过pid的集合拼成的“,”分割的字符串

实现代码

权限实体类

package com.luojun.entity;


public class Permission {
    private long id;
    private String name;
    private String description;
    private String url;
    private long pid;
    private int ismenu;
    private long displayno;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public long getPid() {
		return pid;
	}
	public void setPid(long pid) {
		this.pid = pid;
	}
	public int getIsmenu() {
		return ismenu;
	}
	public void setIsmenu(int ismenu) {
		this.ismenu = ismenu;
	}
	public long getDisplayno() {
		return displayno;
	}
	public void setDisplayno(long displayno) {
		this.displayno = displayno;
	}
	@Override
	public String toString() {
		return "Permission [id=" + id + ", name=" + name + ", description=" + description + ", url=" + url + ", pid="
				+ pid + ", ismenu=" + ismenu + ", displayno=" + displayno + "]";
	}
	public Permission(long id, String name, String description, String url, long pid, int ismenu, long displayno) {
		super();
		this.id = id;
		this.name = name;
		this.description = description;
		this.url = url;
		this.pid = pid;
		this.ismenu = ismenu;
		this.displayno = displayno;
	}
	public Permission() {
		super();
	}
    
}

角色中间表实体类

package com.luojun.entity;


public class RolePermission {
    private long rid;
    private long pid;
	public long getRid() {
		return rid;
	}
	public void setRid(long rid) {
		this.rid = rid;
	}
	public long getPid() {
		return pid;
	}
	public void setPid(long pid) {
		this.pid = pid;
	}
	@Override
	public String toString() {
		return "RolePermission [rid=" + rid + ", pid=" + pid + "]";
	}
	public RolePermission() {
		super();
	}
	public RolePermission(long rid, long pid) {
		super();
		this.rid = rid;
		this.pid = pid;
	}

}

权限方法实现类

package com.luojun.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.luojun.entity.Permission;
import com.luojun.entity.RolePermission;
import com.luojun.utils.BaseDao;
import com.luojun.utils.BuildTree;
import com.luojun.vo.TreeVo;


public class PermissionDao extends BaseDao<Permission> {

//    树形菜单加载
    public List<Permission> menuTree() throws Exception {
        String sql = "select * from t_easyui_permission where true ";
        return super.executeQuery(sql, null, Permission.class);
    }

//    通过ids查出ids指定的树形菜单
    public List<Permission> menuTree(String ids) throws Exception {
        String sql = "select * from t_easyui_permission where id in ("+ids+") ";
        return super.executeQuery(sql, null, Permission.class);
    }

//    树形菜单加载
    public List<TreeVo<Permission>> menuTreeHandler() throws Exception {
        List<Permission> permissions = this.menuTree();
        List<TreeVo<Permission>> nodes = new ArrayList<>();
        TreeVo<Permission> node;
        for (Permission permission : permissions) {
            node = new TreeVo<>();
            node.setId(String.valueOf(permission.getId()));
            node.setText(permission.getName());
//            node.setAttributes();
            node.setParentId(String.valueOf(permission.getPid()));
            Map map = new HashMap();
            map.put("self",permission);
            node.setAttributes(map);
            nodes.add(node);
        }
//        BuildTree.
        return BuildTree.buildList(nodes,"0");
    }

    public List<TreeVo<Permission>> menuTreeHandler(String pids) throws Exception {
        List<Permission> permissions = this.menuTree(pids);
        List<TreeVo<Permission>> nodes = new ArrayList<>();
        TreeVo<Permission> node;
        for (Permission permission : permissions) {
            node = new TreeVo<>();
            node.setId(String.valueOf(permission.getId()));
            node.setText(permission.getName());
//            node.setAttributes();
            node.setParentId(String.valueOf(permission.getPid()));
            Map map = new HashMap();
            map.put("self",permission);
            node.setAttributes(map);
            nodes.add(node);
        }
//        BuildTree.
        return BuildTree.buildList(nodes,"0");
    }

}

获取对应的权限id

package com.luojun.dao;

import java.util.List;

import com.luojun.entity.Permission;
import com.luojun.entity.RolePermission;
import com.luojun.utils.BaseDao;


public class RolePermissionDao extends BaseDao<RolePermission> {
    //    获取角色对应的权限id
    public List<RolePermission> getPids(long rid) throws Exception {
        String sql = "select * from t_easyui_role_permission where rid = " + rid;
        return super.executeQuery(sql, null, RolePermission.class);
    }
}

权限*控制类

package com.luojun.web;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.luojun.dao.PermissionDao;
import com.luojun.dao.RolePermissionDao;
import com.luojun.entity.Permission;
import com.luojun.entity.RolePermission;
import com.luojun.entity.User;
import com.luojun.framework.ActionSupport;
import com.luojun.framework.ModelDriven;
import com.luojun.utils.ResponseUtil;
import com.luojun.vo.TreeVo;

import java.util.List;

public class PermissionAction extends ActionSupport implements ModelDriven<Permission> {
    private Permission permission = new Permission();
    private PermissionDao permissionDao = new PermissionDao();
    private RolePermissionDao rolePermissionDao = new RolePermissionDao();

    public String menuTree(HttpServletRequest request, HttpServletResponse response){
        ObjectMapper om = new ObjectMapper();
        try {
            User currentUser = (User) request.getSession().getAttribute("currentUser");
            List<RolePermission> pids = rolePermissionDao.getPids(currentUser.getType());
            StringBuilder sb = new StringBuilder();
            for (RolePermission pid : pids) {
                sb.append(",").append(pid.getPid());
            }
//            rolePermissionDao
            List<TreeVo<Permission>> treeVos = this.permissionDao.menuTreeHandler(sb.substring(1));
            String json = om.writeValueAsString(treeVos);
            ResponseUtil.write(response,json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Permission getModel() {
        return permission;
    }
}

效果图

网上书城(权限管理)
网上书城(权限管理)

本文地址:https://blog.csdn.net/qq_46765422/article/details/107145015