添加菜单路由接口和单元测试

This commit is contained in:
haoxr 2020-09-14 18:06:54 +08:00
parent c172d903cc
commit 561af63ec8
13 changed files with 180 additions and 30 deletions

View File

@ -97,6 +97,11 @@
<artifactId>minio</artifactId> <artifactId>minio</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -7,5 +7,7 @@ public interface AdminConstant {
int ROOT_DEPT_ID = 0; // 根部门ID int ROOT_DEPT_ID = 0; // 根部门ID
int ROOT_MENU_ID = 0; // 根菜单ID
int ROOT_ROLE_ID = 1; // 超级管理员角色ID int ROOT_ROLE_ID = 1; // 超级管理员角色ID
} }

View File

@ -41,7 +41,7 @@ public class SysMenuController {
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "query", dataType = "Integer"), @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "query", dataType = "Integer"),
@ApiImplicitParam(name = "mode", value = "查询模式: 1-表格数据 2-树形数据", paramType = "query", dataType = "Integer") @ApiImplicitParam(name = "mode", value = "查询模式: 1-表格数据 2-树形数据 3-菜单路由", paramType = "query", dataType = "Integer")
}) })
@GetMapping @GetMapping
public Result list(String name, Integer roleId, Integer mode) { public Result list(String name, Integer roleId, Integer mode) {
@ -71,6 +71,8 @@ public class SysMenuController {
map.put("checkedKeys", checkedKeys); map.put("checkedKeys", checkedKeys);
return Result.success(map); return Result.success(map);
} }
}else if(mode.equals(3)){
list = iSysMenuService.listForRouter();
} else { } else {
list = iSysMenuService.list(baseQuery); list = iSysMenuService.list(baseQuery);
} }

View File

@ -10,9 +10,9 @@ public class SysDict extends BaseEntity {
@TableId @TableId
private Integer id; private Integer id;
private String value; private String name;
private String text; private String value;
private String typeCode; private String typeCode;

View File

@ -1,14 +1,17 @@
package com.youlai.admin.domain.entity; package com.youlai.admin.domain.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.youlai.common.core.entity.BaseEntity; import com.youlai.common.core.entity.BaseEntity;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class SysMenu extends BaseEntity { public class SysMenu extends BaseEntity {
@TableId(type = IdType.AUTO) @TableId
private Integer id; private Integer id;
private String name; private String name;
@ -25,6 +28,11 @@ public class SysMenu extends BaseEntity {
private String path; private String path;
private String component;
private String perms; private String perms;
@TableField(exist = false)
private List<Integer> roles;
} }

View File

@ -0,0 +1,38 @@
package com.youlai.admin.domain.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List;
@Data
public class RouterVO {
@ApiModelProperty(example = "/admin")
private String path;
@ApiModelProperty(example = "Layout")
private String component;
@ApiModelProperty(example = "/admin/user")
private String redirect;
@ApiModelProperty(example = "true")
private boolean alwaysShow;
@ApiModelProperty(example = "Admin")
private String name;
private Meta meta;
@Data
@AllArgsConstructor
public class Meta {
private String title;
private String icon;
private List<Integer> roles;
}
private List<RouterVO> children;
}

View File

@ -13,9 +13,13 @@ public interface SysDictMapper extends BaseMapper<SysDict> {
@Select("<script>" + @Select("<script>" +
" select a.*,b.name as type_name from sys_dict a left join sys_dict_type b on a.type_code=b.code " + " select a.*,b.name as type_name from sys_dict a left join sys_dict_type b on a.type_code=b.code " +
" <if test='dict.name!=null and dict.name.trim() neq \"\"'>" + " where 1=1 " +
" <if test='dict.name != null and dict.name.trim() neq \"\"'>" +
" and a.name like concat('%',#{dict.name},'%')" + " and a.name like concat('%',#{dict.name},'%')" +
" </if>" + " </if>" +
" <if test='dict.typeCode !=null and dict.typeCode.trim() neq \"\"'>" +
" and a.type_code = #{dict.typeCode}" +
" </if>" +
"</script>") "</script>")
List<SysDict> list(Page<SysDict> page, SysDict dict); List<SysDict> list(Page<SysDict> page, SysDict dict);
} }

View File

@ -2,9 +2,25 @@ package com.youlai.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.youlai.admin.domain.entity.SysMenu; import com.youlai.admin.domain.entity.SysMenu;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import java.util.List;
@Mapper @Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu> { public interface SysMenuMapper extends BaseMapper<SysMenu> {
@Select("<script>" +
" select id ,name ,parent_id,path,component,perms,icon,sort,visible,status from sys_menu " +
" where status=1 and type in (0,1)" +
" order by sort asc" +
"</script>")
@Results({
@Result(id=true, column="id", property="id"),
@Result(property = "roles",column="id",many = @Many(select="com.youlai.admin.mapper.SysRoleMenuMapper.listByMenuId"))
})
List<SysMenu> listForRouter();
} }

View File

@ -2,9 +2,16 @@ package com.youlai.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.youlai.admin.domain.entity.SysRoleMenu; import com.youlai.admin.domain.entity.SysRoleMenu;
import org.apache.ibatis.annotations.Select;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import java.util.List;
@Mapper @Mapper
public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenu> { public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenu> {
@Select("<script>" +
" select role_id from sys_role_menu where menu_id=#{menuId} " +
"</script>")
List<Integer> listByMenuId(Integer menuId);
} }

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.admin.domain.entity.SysMenu; import com.youlai.admin.domain.entity.SysMenu;
import com.youlai.admin.domain.vo.MenuVO; import com.youlai.admin.domain.vo.MenuVO;
import com.youlai.admin.domain.vo.RouterVO;
import com.youlai.admin.domain.vo.TreeSelectVO; import com.youlai.admin.domain.vo.TreeSelectVO;
import java.util.List; import java.util.List;
@ -14,4 +15,6 @@ public interface ISysMenuService extends IService<SysMenu> {
List<MenuVO> listForTableData(LambdaQueryWrapper<SysMenu> baseQuery); List<MenuVO> listForTableData(LambdaQueryWrapper<SysMenu> baseQuery);
List<TreeSelectVO> listForTreeSelect(LambdaQueryWrapper<SysMenu> baseQuery); List<TreeSelectVO> listForTreeSelect(LambdaQueryWrapper<SysMenu> baseQuery);
List listForRouter();
} }

View File

@ -18,6 +18,6 @@ public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> impl
public IPage<SysDict> list(Page<SysDict> page, SysDict dict) { public IPage<SysDict> list(Page<SysDict> page, SysDict dict) {
List<SysDict> list = this.baseMapper.list(page,dict); List<SysDict> list = this.baseMapper.list(page,dict);
page.setRecords(list); page.setRecords(list);
return null; return page;
} }
} }

View File

@ -1,14 +1,17 @@
package com.youlai.admin.service.impl; package com.youlai.admin.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.admin.common.AdminConstant; import com.youlai.admin.common.AdminConstant;
import com.youlai.admin.domain.entity.SysMenu; import com.youlai.admin.domain.entity.SysMenu;
import com.youlai.admin.domain.vo.MenuVO; import com.youlai.admin.domain.vo.MenuVO;
import com.youlai.admin.domain.vo.RouterVO;
import com.youlai.admin.domain.vo.TreeSelectVO; import com.youlai.admin.domain.vo.TreeSelectVO;
import com.youlai.admin.mapper.SysMenuMapper; import com.youlai.admin.mapper.SysMenuMapper;
import com.youlai.admin.service.ISysMenuService; import com.youlai.admin.service.ISysMenuService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
@ -21,39 +24,73 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
@Override @Override
public List<MenuVO> listForTableData(LambdaQueryWrapper<SysMenu> baseQuery) { public List<MenuVO> listForTableData(LambdaQueryWrapper<SysMenu> baseQuery) {
List<SysMenu> deptList = this.baseMapper.selectList(baseQuery); List<SysMenu> menuList = this.baseMapper.selectList(baseQuery);
List<MenuVO> list = recursionForTableData(AdminConstant.ROOT_DEPT_ID, deptList); List<MenuVO> list = recursionForTableData(AdminConstant.ROOT_MENU_ID, menuList);
return list; return list;
} }
@Override @Override
public List<TreeSelectVO> listForTreeSelect(LambdaQueryWrapper<SysMenu> baseQuery) { public List<TreeSelectVO> listForTreeSelect(LambdaQueryWrapper<SysMenu> baseQuery) {
List<SysMenu> deptList = this.baseMapper.selectList(baseQuery); List<SysMenu> menuList = this.baseMapper.selectList(baseQuery);
List<TreeSelectVO> list = recursionForTreeSelect(AdminConstant.ROOT_DEPT_ID, deptList); List<TreeSelectVO> list = recursionForTreeSelect(AdminConstant.ROOT_MENU_ID, menuList);
return list; return list;
} }
@Override
public List listForRouter() {
List<SysMenu> menuList = this.baseMapper.listForRouter();
List<RouterVO> list = recursionForRouter(AdminConstant.ROOT_MENU_ID, menuList);
return list;
}
private List<RouterVO> recursionForRouter(int parentId, List<SysMenu> menuList) {
List<RouterVO> list = new ArrayList<>();
Optional.ofNullable(menuList).orElse(new ArrayList<>())
.stream()
.filter(menu -> menu.getParentId().equals(parentId))
.forEach(menu -> {
RouterVO routerVO = new RouterVO();
routerVO.setName(menu.getName());
routerVO.setPath(menu.getPath());
String component = StrUtil.isNotBlank(menu.getComponent()) ? menu.getComponent() : "Layout";
routerVO.setComponent(component);
RouterVO.Meta meta = routerVO.new Meta(
menu.getName(),
menu.getIcon(),
menu.getRoles()
);
routerVO.setMeta(meta);
if (AdminConstant.ROOT_MENU_ID == parentId) {
routerVO.setAlwaysShow(Boolean.TRUE);
routerVO.setRedirect("noRedirect");
}
List<RouterVO> children = recursionForRouter(menu.getId(), menuList);
routerVO.setChildren(children);
list.add(routerVO);
});
return list;
}
/** /**
* 递归生成部门表格数据 * 递归生成部门表格数据
*
* @param parentId * @param parentId
* @param deptList * @param menuList
* @return * @return
*/ */
public static List<MenuVO> recursionForTableData(int parentId, List<SysMenu> deptList) { public static List<MenuVO> recursionForTableData(int parentId, List<SysMenu> menuList) {
List<MenuVO> list = new ArrayList<>(); List<MenuVO> list = new ArrayList<>();
Optional.ofNullable(deptList).orElse(new ArrayList<>()) Optional.ofNullable(menuList).orElse(new ArrayList<>())
.stream() .stream()
.filter(dept -> dept.getParentId().equals(parentId)) .filter(menu -> menu.getParentId().equals(parentId))
.forEach(dept -> { .forEach(menu -> {
MenuVO deptVO = new MenuVO(); MenuVO menuVO = new MenuVO();
BeanUtil.copyProperties(dept, deptVO); BeanUtil.copyProperties(menu, menuVO);
List<MenuVO> children = recursionForTableData(dept.getId(), deptList); List<MenuVO> children = recursionForTableData(menu.getId(), menuList);
deptVO.setChildren(children); menuVO.setChildren(children);
list.add(deptVO); list.add(menuVO);
}); });
return list; return list;
} }
@ -61,20 +98,21 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
/** /**
* 递归生成部门树形下拉数据 * 递归生成部门树形下拉数据
*
* @param parentId * @param parentId
* @param deptList * @param menuList
* @return * @return
*/ */
public static List<TreeSelectVO> recursionForTreeSelect(int parentId, List<SysMenu> deptList) { public static List<TreeSelectVO> recursionForTreeSelect(int parentId, List<SysMenu> menuList) {
List<TreeSelectVO> list = new ArrayList<>(); List<TreeSelectVO> list = new ArrayList<>();
Optional.ofNullable(deptList).orElse(new ArrayList<>()) Optional.ofNullable(menuList).orElse(new ArrayList<>())
.stream() .stream()
.filter(dept -> dept.getParentId().equals(parentId)) .filter(menu -> menu.getParentId().equals(parentId))
.forEach(dept -> { .forEach(menu -> {
TreeSelectVO treeSelectVO = new TreeSelectVO(); TreeSelectVO treeSelectVO = new TreeSelectVO();
treeSelectVO.setId(dept.getId()); treeSelectVO.setId(menu.getId());
treeSelectVO.setLabel(dept.getName()); treeSelectVO.setLabel(menu.getName());
List<TreeSelectVO> children = recursionForTreeSelect(dept.getId(), deptList); List<TreeSelectVO> children = recursionForTreeSelect(menu.getId(), menuList);
treeSelectVO.setChildren(children); treeSelectVO.setChildren(children);
list.add(treeSelectVO); list.add(treeSelectVO);
}); });

View File

@ -0,0 +1,27 @@
package com.youlai.admin;
import cn.hutool.core.lang.Assert;
import com.youlai.admin.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class AdminApplicationTests {
@Autowired
private ISysMenuService iSysMenuService;
@Test
public void testListForRouter() {
List list = iSysMenuService.listForRouter();
Assert.isTrue(list.size()>0);
}
}