使用Java编程和springboot框架实现菜单权限表的树状结构接口,给出相应的controller层,service层,daoc层及mapper,
时间: 2024-03-10 18:46:24 浏览: 83
Java 基于springboot高校表白墙社交系统创建基本的controller,service和model层
好的,下面是完整的代码实现。
先定义一个菜单权限的实体类`MenuPermission`,包含菜单ID、菜单名称、父级菜单ID等属性:
```java
public class MenuPermission {
private Long menuId;
private String menuName;
private Long parentId;
// 省略getter和setter方法
}
```
定义一个菜单权限的Mapper接口`MenuPermissionMapper`,使用MyBatis注解实现SQL语句的映射。其中,`selectAll()`方法用于查询全部菜单权限数据:
```java
@Mapper
public interface MenuPermissionMapper {
@Select("SELECT * FROM menu_permission")
List<MenuPermission> selectAll();
}
```
定义一个菜单权限的服务类`MenuPermissionService`,用于获取全部菜单权限数据:
```java
@Service
public class MenuPermissionService {
// 假设菜单权限数据存储在MySQL数据库中
@Autowired
private MenuPermissionMapper menuPermissionMapper;
/**
* 获取全部菜单权限数据
*/
public List<MenuPermission> getAllMenuPermissions() {
return menuPermissionMapper.selectAll();
}
}
```
定义一个菜单权限的控制器`MenuPermissionController`,用于提供树状结构的菜单权限数据接口`/menu/tree`。其中,`buildMenuPermissionTree()`方法用于构建树状结构,`getMenuPermissionTree()`方法用于返回树状结构的菜单权限数据:
```java
@RestController
@RequestMapping("/menu")
public class MenuPermissionController {
@Autowired
private MenuPermissionService menuPermissionService;
@GetMapping("/tree")
public List<MenuPermission> getMenuPermissionTree() {
// 获取全部菜单权限数据
List<MenuPermission> allMenuPermissions = menuPermissionService.getAllMenuPermissions();
// 构建树状结构
return buildMenuPermissionTree(allMenuPermissions);
}
/**
* 构建树状结构的菜单权限数据
*/
private List<MenuPermission> buildMenuPermissionTree(List<MenuPermission> menuPermissions) {
// 构建菜单ID到菜单权限的映射表
Map<Long, MenuPermission> menuIdMap = new HashMap<>();
for (MenuPermission menuPermission : menuPermissions) {
menuIdMap.put(menuPermission.getMenuId(), menuPermission);
}
// 构建父级菜单ID到子菜单列表的映射表
Map<Long, List<MenuPermission>> parentIdMap = new HashMap<>();
for (MenuPermission menuPermission : menuPermissions) {
Long parentId = menuPermission.getParentId();
if (parentId != null) {
List<MenuPermission> childMenuPermissions = parentIdMap.computeIfAbsent(parentId, k -> new ArrayList<>());
childMenuPermissions.add(menuPermission);
}
}
// 遍历菜单权限数据,构建树状结构
List<MenuPermission> rootMenuPermissions = new ArrayList<>();
for (MenuPermission menuPermission : menuPermissions) {
Long parentId = menuPermission.getParentId();
if (parentId == null) {
// 当前菜单是根菜单,直接添加到根菜单列表中
rootMenuPermissions.add(menuPermission);
} else {
// 当前菜单是子菜单,添加到父级菜单的子菜单列表中
MenuPermission parentMenuPermission = menuIdMap.get(parentId);
if (parentMenuPermission != null) {
List<MenuPermission> childMenuPermissions = parentIdMap.get(parentId);
if (childMenuPermissions == null) {
childMenuPermissions = new ArrayList<>();
parentIdMap.put(parentId, childMenuPermissions);
}
childMenuPermissions.add(menuPermission);
}
}
}
return rootMenuPermissions;
}
}
```
最后,我们需要在`application.properties`文件中配置数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
以上就是完整的代码实现。需要注意的是,我们需要在`pom.xml`文件中添加相关依赖,如下所示:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
```
阅读全文