springboot怎样构造树形结构数据并查询?
因为项⽬需要,页⾯上需要树形结构的数据进⾏展⽰(类似下图这样),因此需要后端返回相应格式的数据。
不说废话,直接开⼲
我这⾥⽤的是springboot+mybatis-plus+mysql,⽰例的接⼝是查询⼀级权限以及⼆级权限、三级权限整个权限树…下⾯是导⼊的maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库连接-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis增强⼯具-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.6</version>
</dependency>
下⾯是实体类Permission
public class Permission implements Serializable {
@TableId
private String permissionId;
@NotNull(message ="权限名称不能为空")
private String permissionName;
/**
* 权限标识
*/
@NotNull(message ="权限标识不能为空")
private String permissionCode;
/**
* ⽗菜单ID,如果是-1就表⽰是⼀级权限菜单。
*/
@NotBlank(message ="⽗菜单ID不能为空")
private String parentId;
/**
* 前端URL访问接⼝路径
*/
private String path;
/
**
* 排序值
*/
private Integer sort;
/**
* 创建时间
*/
private LocalDateTime createTime;
/**
* 更新时间
*/
private LocalDateTime updateTime;
/**
* 0--正常 1--删除
*/
private String delFlag;
public Permission(){
this.permissionId = IdUtil.simpleUUID();
}
树形结点类
@Data
public class TreeNode {
protected String id;
protected String parentId;
protected List<TreeNode> children =new ArrayList<TreeNode>(); protected boolean hasChildren;
public void addTreeNode(TreeNode node){
children.add(node);
}
}
树形结点详细信息类
@EqualsAndHashCode(callSuper =true)
public class PermissionTree extends TreeNode implements Serializable {
private String permissionName;
private String permissionCode;
private String path;
private Integer sort;
private String label;
private boolean hasChildren;
public PermissionTree(){
}
}
构建树形结点⼯具类(关键),在这⾥我⽤@UtilityClass注解就表⽰这个类中的⽅法都是静态⽅法:
@UtilityClass
public class TreeUtil {
public<T extends TreeNode> List<T>build(List<T> treeNodes, String root){
List<T> trees =new ArrayList<>();
for(T treeNode : treeNodes){
if(root.ParentId())){
trees.add(treeNode);
}
for(T node : treeNodes){
ParentId().Id())){
treeNode.addTreeNode(node);
treeNode.setHasChildren(true);
}
}
}
return trees;
}
/**
* 通过permission创建树形节点
*
* @param permissionList
* @param root
* @return
*/
public List<PermissionTree>buildTree(List<Permission> permissionList, String root){
System.out.Array()));
List<PermissionTree> treeNodeList =new ArrayList<>();
PermissionTree treeNode = null;
for(Permission permission : permissionList){
treeNode =new PermissionTree();
treeNode.PermissionId());
treeNode.PermissionName());
treeNode.Path());
treeNode.Sort());
treeNode.ParentId());
treeNode.PermissionName());
treeNode.setHasChildren(false);
treeNodeList.add(treeNode);
}
return TreeUtil.build(treeNodeList, root);
}
}
响应消息主体类
/**
* 响应信息主体
*
* @param <T>
*/
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain =true)
public class R<T>implements Serializable {
private static final long serialVersionUID =1L;
private int code;
private String msg;
private T data;
public int getCode(){
return code;
}
public void setCode(int code){
}
public String getMsg(){
return msg;
}
public void setMsg(String msg){
this.msg = msg;
}
public T getData(){
return data;
}
public void setData(T data){
this.data = data;
}
public static<T> R<T>ok(){
return restResult(null, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS); }
public static<T> R<T>ok(T data){
return restResult(data, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS); }
public static<T> R<T>ok(T data, String msg){
return restResult(data, CommonConstants.SUCCESS, msg);
}
public static<T> R<T>failed(){
return restResult(null, CommonConstants.FAIL, null);
}
public static<T> R<T>failed(String msg){
springboot结构return restResult(null, CommonConstants.FAIL, msg);
}
public static<T> R<T>failed(T data){
public static<T> R<T>failed(T data){
return restResult(data, CommonConstants.FAIL, null);
}
public static<T> R<T>failed(T data, String msg){
return restResult(data, CommonConstants.FAIL, msg);
}
private static<T> R<T>restResult(T data,int code, String msg){
R<T> apiResult =new R<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
}
数据查询接⼝mapper类
@Mapper
public interface PermissionMapper extends BaseMapper<Permission>{
}
数据逻辑处理业务接⼝
public interface PermissionService extends IService<Permission>{
/**
* 构建权限树
*
* @param lazy
* @param parentId
* @return
*/
List<PermissionTree>treePermission(boolean lazy, String parentId);
}
数据逻辑处理业务接⼝实现类
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>implements PermissionService {
/**
* 构建权限树:1、不是懒加载情况,查询全部
* 2、是懒加载,根据parentId查询
*
* @param lazy
* @param parentId
* @return
*/
@Override
public List<PermissionTree>treePermission(boolean lazy, String parentId){
if(!lazy){
return TreeUtil.buildTree(
baseMapper.selectList(Wrappers.<Permission>lambdaQuery().orderByAsc(Permission::getSort)),
CommonConstants.PERMISSION_ROOT_ID);
}
String parent = parentId == null ? CommonConstants.PERMISSION_ROOT_ID : parentId;
return TreeUtil.buildTree(
baseMapper.selectList(Wrappers.<Permission>lambdaQuery().eq(Permission::getParentId, parent).orderByAsc(Permission::getSort)), parent );
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论