我们的菜单是分级的。会通过父id去关联,并且对应的id会有对应的权限
代码层从上往下写:controller/service/Mapper/mapperxml…
菜单分两级,一级菜单下面有子菜单,我们修改下Menu对象。
在返回的时候会返回子菜单,添加子菜单的属性:
@RestController
@RequestMapping("/system/cfg")
public class MenuController {
@Autowired
private IMenuService menuService;
/**
* 虽然是通过用户id查询菜单列表,但是没有传id
* 如果用户正常能登录以后,用户的相关信息一般在后端获取的,而不是前端传进来的,可能出问题
* 通过全局变量获取到,用户id
*/
@ApiOperation(value = "通过用户Id查询菜单列表")
@GetMapping("/menu")
public List<Menu> getMenusByAdminId(){
return menuService.getMenusByAdminId();
}
}
public interface IMenuService extends IService<Menu> {
/**
* 通过用户id查询菜单列表
* @return
*/
List<Menu> getMenusByAdminId();
}
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
@Autowired
private MenuMapper menuMapper;
/**
* 通过用户id查询菜单列表
* @return
*/
@Override
public List<Menu> getMenusByAdminId() {
/*
此时要传admin的id了,那么如何获取呢?
*/
return menuMapper.getMenusByAdminId(
((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
}
@Mapper
public interface MenuMapper extends BaseMapper<Menu> {
/**
* 通过用户id查询菜单列表
*/
List<Menu> getMenusByAdminId(Integer id);
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxxx.server.mapper.MenuMapper">
<resultMap id="BaseResultMap" type="com.xxxx.server.pojo.Menu">
<id column="id" property="id" />
<result column="url" property="url" />
<result column="path" property="path" />
<result column="component" property="component" />
<result column="name" property="name" />
<result column="iconCls" property="iconCls" />
<result column="keepAlive" property="keepAlive" />
<result column="requireAuth" property="requireAuth" />
<result column="parentId" property="parentId" />
<result column="enabled" property="enabled" />
resultMap>
<resultMap id="Menus" type="com.xxxx.server.pojo.Menu" extends="BaseResultMap">
<collection property="children" ofType="com.xxxx.server.pojo.Menu">
<id column="id2" property="id" />
<result column="url2" property="url" />
<result column="path2" property="path" />
<result column="component2" property="component" />
<result column="name2" property="name" />
<result column="iconCls2" property="iconCls" />
<result column="keepAlive2" property="keepAlive" />
<result column="requireAuth2" property="requireAuth" />
<result column="parentId2" property="parentId" />
<result column="enabled2" property="enabled" />
collection>
resultMap>
<sql id="Base_Column_List">
id, url, path, component, name, iconCls, keepAlive, requireAuth, parentId, enabled
sql>
<select id="getMenusByAdminId" resultMap="Menus">
SELECT
DISTINCT
m1.*,
m2.id AS id2,
m2.url AS url2,
m2.path AS path2,
m2.component AS component2,
m2.`name` AS name2,
m2.iconCls AS iconCls2,
m2.keepAlive As keepAlive2,
m2.requireAuth AS requireAuth2,
m2.parentID AS parentId2,
m2.enabled AS enabled2
FROM
t_menu m1, --#父菜单
t_menu m2, --#子菜单
t_admin_role ar, --#用户角色
t_menu_role mr --#菜单角色
WHERE
m1.id = m2.parentId --#父菜单id=子菜单Id
AND m2.id = mr.mid -- #子菜单id=菜单角色的菜单id
AND mr.rid = ar.rid --#菜单角色的用户id=用户角色的用户id
AND ar.adminId = #{id}
AND m2.enabled = TRUE
select>
mapper>