èå表ç»æ
create table sys_auth
(
id bigint auto_increment
primary key,
parent_id bigint default 0 not null comment 'ç¶id',
name varchar(50) not null comment 'å称(èåå)',
code varchar(50) null comment 'æéç¼ç ',
web_code varchar(50) not null comment 'å端æéç¼ç (è·¯ç±å/æé®å)',
auth_type tinyint not null comment 'æéç±»å(èå,ç®å½,æé®,æé)',
sort int null comment 'æåº',
icon varchar(50) null comment 'èåå¾æ ',
path varchar(255) null comment 'è·¯ç±å°å',
component varchar(255) null comment 'ç»ä»¶è·¯å¾',
frame_flag tinyint default 0 not null comment 'æ¯å¦å¤é¾',
frame_url varchar(255) null comment 'å¤é¾å°å',
cache_flag tinyint default 1 not null comment 'æ¯å¦ç¼å',
show_flag tinyint default 1 not null comment 'æ¯å¦æ¾ç¤º',
state tinyint not null comment 'ç¶æ',
creator varchar(50) not null comment 'å建账å·',
create_time datetime not null comment 'å建æ¶é´',
updator varchar(50) null comment 'æ´æ°è´¦å·',
update_time datetime null comment 'æ´æ°æ¶é´',
remark varchar(255) null comment 'å¤æ³¨',
contains_code varchar(255) null comment 'å
å«æé'
)
comment 'ç³»ç»æé' charset=utf8mb4;
å°å¹³è¡çæ°æ®è½¬ææ ç¶ç»ææ°æ®ç»å端å±ç¤º
æ¹æ³1:æ°æ®å®ç°æ¥å£,ç¨å·¥å ·ç±»éåç»è£ æ°æ®
package com.hfw.common.util;
import java.util.List;
/**
* æ°ç»ææ°æ®
* @author farkle
* @date 2022-04-06
*/
public interface ListTree {
Long getId();
Long getParentId();
Integer getSort();
void setSort(Integer sort);
Integer getLevel();
void setLevel(Integer level);
List<? extends ListTree> getChildren();
void setChildren(List<? extends ListTree> children);
}
package com.hfw.common.util;
import java.util.*;
/**
* Listå·¥å
·ç±»
* @author farkle
* @date 2022-04-06
*/
public class ListUtil {
/**
* å°å¹³é¢list转æ¢ææ ç»æ
* @param list
* @return
*/
public static List listTotree(List<? extends ListTree> list) {
Map<Long, List<ListTree>> map = new HashMap<>();
List result = new ArrayList<>();
for (ListTree acl : list) {
if(acl.getSort()==null){
acl.setSort(-1);
}
if (acl.getParentId() == 0) {
acl.setLevel(1);
result.add(acl);
} else {
List children = map.get(acl.getParentId());
if (children == null) {
children = new ArrayList<>();
map.put(acl.getParentId(), children);
}
children.add(acl);
}
}
tree(result, map);
if(map.size()>0){
map.forEach( (id,ele)->{
result.addAll(ele);
});
}
return result;
}
/**
* å°å¹³é¢listéå½è½¬æ¢ææ ç»æ
* @param list
* @param map
*/
private static void tree(List<ListTree> list, Map<Long, List<ListTree>> map) {
list.sort((ListTree m, ListTree n) -> {
if (m.getSort() == null || n.getSort() == null) {
return (int) (m.getId() - n.getId());
}
if(m.getSort()==null){
return 1;
}
if(n.getSort()==null){
return 0;
}
return m.getSort() - n.getSort();
});
for (ListTree acl : list) {
List<ListTree> children = map.remove(acl.getId());
if (children != null) {
for (ListTree c : children) {
c.setLevel(acl.getLevel() + 1);
}
acl.setChildren(children);
tree(children, map);
}
}
}
}
æ¹æ³2(æ¨è): å©ç¨java8stream APIç»è£
package com.hfw.common.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @date 2023-01-31
*/
public class TreeUtils {
/**
* @param list æºæ°æ®
* @param setChildListFn 设置éå½çæ¹æ³
* @param idFn è·åidçæ¹æ³
* @param pidFn è·åç¶idçæ¹æ³
* @param getRootCondition è·åæ ¹èç¹çæ¡ä»¶
* @return å°List 转æ¢æ Tree
*/
public static <M, T> List<M> listToTree(List<M> list,
Function<M, T> idFn,
Function<M, T> pidFn,
BiConsumer<M, List<M>> setChildListFn,
Predicate<M> getRootCondition) {
if(list==null || list.size()<=0){
return null;
}
Map<T, List<M>> listMap = list.stream().collect(Collectors.groupingBy(pidFn));
list.forEach(model -> setChildListFn.accept(model, listMap.get(idFn.apply(model))));
return list.stream().filter(getRootCondition).collect(Collectors.toList());
}
public static <M> List<M> treeToList(List<M> source,
Function<M, List<M>> getChildListFn,
BiConsumer<M, List<M>> setChildListFn,
Predicate<M> getRootCondition) {
List<M> target = new ArrayList<>();
if(source!=null && source.size()>0) {
treeToList(source, target, getChildListFn);
target.forEach(model -> setChildListFn.accept(model, null));
target.addAll(target.stream().filter(getRootCondition).collect(Collectors.toList()));
}
return target;
}
private static <F> void treeToList(List<F> source,
List<F> target,
Function<F, List<F>> getChildListFn) {
if(source!=null && source.size()>0) {
target.addAll(source);
source.forEach(model -> {
List<F> childList = getChildListFn.apply(model);
if(childList!=null && childList.size()>0) {
treeToList(childList, target, getChildListFn);
}
});
}
}
}
ç»è£ èåæ°æ®
@Override
public List<SysAuthDTO> treeList(SysAuth sysAuth){
List<SysAuthDTO> list = sysAuthMapper.treeList(sysAuth);
//return ListUtil.listTotree(list);
return TreeUtils.listToTree(list, SysAuthDTO::getId, SysAuthDTO::getParentId, SysAuthDTO::setChildren,
(dto) -> dto.getParentId()==0 );
}
éæ项ç®å®æ´ç¤ºä¾ä»£ç : ZNEW-ADMIN
https://gitee.com/hongy123/znew-admin
ä¸ä¸ªç®åéç¨çspringboot+vue3åå°ç®¡çç³»ç»