使用Springboot和mybatis框架开发,程序可能用到的表为菜单权限表t_sys_menu,实现菜单权限表的树状结构接口,给出Controller层,Service层,Service实现类,Dao层
时间: 2024-03-28 16:42:00 浏览: 77
好的,我理解了你的问题。下面是给出的代码示例:
Controller层:
```java
@RestController
@RequestMapping("/menu")
public class MenuController {
@Autowired
private MenuService menuService;
@GetMapping("/getTreeMenu")
public List<Menu> getTreeMenu() {
return menuService.getMenuTree();
}
}
```
Service层:
```java
public interface MenuService {
List<Menu> getMenuTree();
}
```
Service实现类:
```java
@Service
public class MenuServiceImpl implements MenuService {
@Autowired
private MenuMapper menuMapper;
@Override
public List<Menu> getMenuTree() {
List<Menu> menuList = menuMapper.getMenuList();
List<Menu> rootMenus = new ArrayList<>();
Map<Long, Menu> menuMap = new HashMap<>();
for (Menu menu : menuList) {
menuMap.put(menu.getId(), menu);
if (menu.getParentId() == 0) {
rootMenus.add(menu);
}
}
for (Menu menu : menuList) {
if (menu.getParentId() != 0) {
Menu parentMenu = menuMap.get(menu.getParentId());
if (parentMenu != null) {
parentMenu.addChild(menu);
}
}
}
return rootMenus;
}
}
```
Dao层:
```java
@Mapper
public interface MenuMapper {
List<Menu> getMenuList();
}
```
注意:这里的Menu实体类需要自己定义并实现addChild方法,用于将子菜单添加到父菜单中。
另外,这里只是提供一个简单的示例,实际情况中需要根据具体需求进行调整。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)