1 先引入工具类
public class Tree {
/**
* 节点ID
*/
private String id;
/**
* 显示节点文本
*/
private String text;
/**
* 节点状态,open closed
*/
private Map state;
/**
* 节点是否被选中 true false
*/
private boolean checked = false;
/**
* 节点属性
*/
private Map attributes;
/**
* 节点的子节点
*/
private List> children = new ArrayList>();
/**
* 父ID
*/
private String parentId;
/**
* 是否有父节点
*/
private boolean hasParent = false;
/**
* 是否有子节点
*/
private boolean hasChildren = false;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Map getState() {
return state;
}
public void setState(Map state) {
this.state = state;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public Map getAttributes() {
return attributes;
}
public void setAttributes(Map attributes) {
this.attributes = attributes;
}
public List> getChildren() {
return children;
}
public void setChildren(List> children) {
this.children = children;
}
public boolean isHasParent() {
return hasParent;
}
public void setHasParent(boolean isParent) {
this.hasParent = isParent;
}
public boolean isHasChildren() {
return hasChildren;
}
public void setChildren(boolean isChildren) {
this.hasChildren = isChildren;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public Tree(String id, String text, Map state, boolean checked, Map attributes,
List> children, boolean isParent, boolean isChildren, String parentID) {
super();
this.id = id;
this.text = text;
this.state = state;
this.checked = checked;
this.attributes = attributes;
this.children = children;
this.hasParent = isParent;
this.hasChildren = isChildren;
this.parentId = parentID;
}
public Tree() {
super();
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}
注: 需要在pom文件中引入json
com.alibaba
fastjson
2 service
//根据ids 查询多条权限
List queryAuthorityByIds(String ids);
3 serviceimpl
//根据ids查询多条权限
@Override
public List queryAuthorityByIds(String ids) {
return authorityMapper.queryAuthorityByIds(ids);
}
4 dao
//根据Authorityids 查询多条权限
List queryAuthorityByIds(String ids);
5 mapper
6 controller
@RequestMapping(value = "/getAuthorityList", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
public List> getAuthorityList() {
int sysId = 103;
Role role = new Role();
role.setSysId(sysId);
List role1 = roleService.getRoleBySysId(role);
StringBuilder sb = new StringBuilder();
ListToString listToString = new ListToString();
for(int i =0 ; i< role1.size(); i ++){
sb.append(",").append(listToString.listToString(role1.get(i).getAuthorityIdList(),','));
}
String authList = sb.substring(1);
RemovalDuplicate removalDuplicate = new RemovalDuplicate();
String authorityIds = removalDuplicate.removalDuplicate(authList);
List> trees = new ArrayList>();
List AuthorityLists = authorityService.queryAuthorityByIds(authorityIds);
for (Authority sysAuthority : AuthorityLists) {
Tree tree = new Tree();
tree.setId(sysAuthority.getId().toString());
tree.setParentId(sysAuthority.getParentId().toString());
tree.setText(sysAuthority.getAutName());
Map attributes = new HashMap(16);
attributes.put("url",sysAuthority.getAutUrl());
attributes.put("icon",sysAuthority.getAutClass());
tree.setAttributes(attributes);
trees.add(tree);
}
List> list = BuildTree.buildList(trees,"0");
return list;
}
7 去重类
public class RemovalDuplicate {
public String removalDuplicate(String str){
String R_Str="";
String[] OID_Arr=str.split(",");
for(int i=0;i
9 String 转 List类
public class StringToList {
public List stringToList(String str){
List result = Arrays.asList(str.split(","));
return result;
}
}
10, List转 String 类
public class ListToString {
public String listToString(List list, char separator) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
if (i == list.size() - 1) {
sb.append(list.get(i));
} else {
sb.append(list.get(i));
sb.append(separator);
}
}
return sb.toString();
}
}
11 bean
public class Authority {
/**
* 主键Id
*/
private Integer id;
/**
* 权限名称
*/
private String autName;
/**
* 父级权限Id
*/
private Integer parentId;
/**
* 权限对应的url
*/
private String autUrl;
/**
* 权限对应样式
*/
private String autClass;
/**
* 1:折叠菜单 2:模块 3:功能
*/
private Integer level;
/**
* 设置左侧菜单的顺序,默认为0(值越大越高级)只对菜单权限和没有菜单权限的模块权限有效
*/
private Integer sequence;
/**
* 备注
*/
private String common;
/**
* 权限状态1:启用 2:停用
*/
private Integer autState;
/**
* 更新时间
*/
private Date updateTime;
/**
* 创建时间
*/
private Date createTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAutName() {
return autName;
}
public void setAutName(String autName) {
this.autName = autName;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getAutUrl() {
return autUrl;
}
public void setAutUrl(String autUrl) {
this.autUrl = autUrl;
}
public String getAutClass() {
return autClass;
}
public void setAutClass(String autClass) {
this.autClass = autClass;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public Integer getSequence() {
return sequence;
}
public void setSequence(Integer sequence) {
this.sequence = sequence;
}
public String getCommon() {
return common;
}
public void setCommon(String common) {
this.common = common;
}
public Integer getAutState() {
return autState;
}
public void setAutState(Integer autState) {
this.autState = autState;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
12 controller api
@ApiOperation(value = "【账号管理】- 获取权限")
@RequestMapping(value = "/authority", method = RequestMethod.GET)
public String getAccountAuthorityList(HttpServletRequest request, HttpServletResponse response) {
String name = (String) request.getParameter("name");//获取上个方法添加的参数
List> Authoritylist = restTemplate.getForObject("http://server-account:8776/account/getAuthorityList", List.class);
request.setAttribute("Authoritylist",Authoritylist);
request.setAttribute("name",name);
return "index";
}
13 界面