@RequestMapping("/UserTreeInfo")
public RespBody getUserTreeInfo(Long userId) {
List<MenuTreeVo> userInfo = userInfoServiceimpl.getUserTreeInfo(userId);
if (userInfo != null && userInfo.size() > 0) {
return new RespBody(200,userInfo,"查询成功");
}
return new RespBody(501,null,"查询失败");
}
package com.ekgc.qy.Service.impl;
import com.ekgc.qy.Service.UserInfoService;
import com.ekgc.qy.dao.UserInfoDao;
import com.ekgc.qy.pojo.dto.MenuDto;
import com.ekgc.qy.pojo.vo.MenuTreeVo;
import jakarta.annotation.Resource;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Magic
* @version 1.0
*/
@Service
public class UserInfoServiceimpl implements UserInfoService {
@Resource
private UserInfoDao userInfoDao;
@Override
public List<MenuTreeVo> findGetInfoByUserId(Long userId) {
return userInfoDao.findGetInfoByUserId(userId);
}
/**
* 查询树结构菜单
* @return 返回树结构菜单
*/
@Override
public List<MenuTreeVo> getUserTreeInfo(Long userId) {
//查出所有菜单和目录
List<MenuDto> userTreeInfo = userInfoDao.getUserTreeInfo(userId);
// 先过滤出所有的父菜单目录
List<MenuDto> mulu = userTreeInfo
.stream()
.filter(ml -> ml.getParentId() == 0).toList();
return buildTrees(mulu,userTreeInfo);
}
private ArrayList<MenuTreeVo> buildTrees(List<MenuDto> data, List<MenuDto> menus) {
// 存储树结构的菜单树
ArrayList<MenuTreeVo> trees = new ArrayList<>();
// 封装树结构菜单数据
data.forEach(md -> {
// 封装的MenuTreeVo树结构对象
MenuTreeVo ml = new MenuTreeVo();
// 根据属性名 将数据复制到另一个对象
// 实现MenuDto到MenuTreeVo的转换
try {
BeanUtils.copyProperties(ml, md);//前一个是目标对象,后一个是源对象
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
//遍历目录和菜单找出子菜单并封装子菜单
List<MenuDto> childs = new ArrayList<>();
menus.forEach(m ->{
if (m.getParentId() != null){
if (m.getParentId().equals(md.getId())){
childs.add(m);
}
}
});
// 没有子菜单 不继续执行递归
if (!childs.isEmpty()) {
ml.setChildMenus(buildTrees(childs, menus));
}
trees.add(ml);
});
return trees;
}
}
package com.ekgc.qy.dao;
import com.ekgc.qy.pojo.dto.MenuDto;
import com.ekgc.qy.pojo.vo.MenuTreeVo;
import java.util.List;
/**
* @author Magic
* @version 1.0
*/
public interface UserInfoDao {
List<MenuTreeVo> findGetInfoByUserId(Long userId);
List<MenuDto> getUserTreeInfo(Long userId);
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ekgc.qy.dao.UserInfoDao">
<resultMap id="roleMenus" type="MenuTreeVo">
<id property="id" column="id"/>
<result property="menuName" column="menu_name"/>
<result property="permissions" column="permissions"/>
<collection property="childMenus" ofType="MenuDto" javaType="java.util.ArrayList">
<id property="id" column="childId"/>
<result property="menuName" column="menuName"/>
<result property="menuUrl" column="menuUrl"/>
<result property="permissions" column="auth"/>
<result property="path" column="path"/>
<result property="parentId" column="parent_id"/>
collection>
resultMap>
<select id="findGetInfoByUserId" resultMap="roleMenus">
SELECT ml.id,ml.menu_name,ml.permissions,
sy.id AS childId,sy.menu_name AS menuName,sy.menu_url AS menuUrl,sy.permissions AS auth,sy.path,sy.parent_id FROM sys_menu sy,
(SELECT sm.id,menu_name,permissions FROM sys_menu sm
LEFT JOIN sys_role_menu srm ON sm.id = srm.menu_id
LEFT JOIN sys_role sr ON srm.role_id = sr.id
LEFT JOIN sys_user su ON su.user_type = sr.id
WHERE su.id = #{userId}) AS ml WHERE ml.id = sy.parent_id
select>
<select id="getUserTreeInfo" resultType="com.ekgc.qy.pojo.dto.MenuDto">
SELECT sm.id,menu_name,menu_url,path,permissions,parent_id FROM sys_menu sm
LEFT JOIN sys_role_menu srm ON sm.id = srm.menu_id
LEFT JOIN sys_role sr ON srm.role_id = sr.id
LEFT JOIN sys_user su ON su.user_type = sr.id
WHERE su.id = #{userId} AND menu_name like concat("%",#{menuName},"%")
select>
mapper>
实体类只要有sql查询到的字段(id,menu_name,menu_url,path,permissions,parent_id)有就可以映射,不能缺少某个字段否则映射不了!
package com.ekgc.qy.pojo.vo;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
/**
* @author Magic
* @version 1.0
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class MenuTreeVo {
private Long id;
private String menuName;
private String menuUrl;
private String permissions;
private String path;
private Long parentId;
private ArrayList<MenuTreeVo> childMenus;
}