10.菜单项接口

菜单项接口介绍

该类本来还有KeepAlive和requireAuth两个属性,但是这两个属性不常用,所以就放到新建的Mena类里面去了,Menu新添加了一个private List

children;children里面放是是List集合的Menu

public class Menu {
    private Integer id;

    private String url;

    private String path;

    private String component;

    private String name;

    private String iconcls;

    private Meta meta;

    private Integer parentid;

    private Boolean enabled;

    private List<Menu> children; //children里面放的是List集合的Menu
      
      //set get
}

Meta类

public class Meta {
 
    private Boolean keepAlive;
 
    private Boolean requireAuth;
 
    //省略getter和setter
}

在MenuMapper.xml中新增

<resultMap id="BaseResultMap" type="com.qwl.vhr.model.Menu">
    <id column="id" property="id" jdbcType="INTEGER"/>
    <result column="url" property="url" jdbcType="VARCHAR"/>
    <result column="path" property="path" jdbcType="VARCHAR"/>
    <result column="component" property="component" jdbcType="VARCHAR"/>
    <result column="name" property="name" jdbcType="VARCHAR"/>
    <result column="iconCls" property="iconcls" jdbcType="VARCHAR"/>
    <result column="parentId" property="parentid" jdbcType="INTEGER"/>
    <result column="enabled" property="enabled" jdbcType="BIT"/>
    <association property="meta" javaType="com.qwl.vhr.model.Meta">
        <result column="keepAlive" property="keepalive" jdbcType="BIT"/>
        <result column="requireAuth" property="requireauth" jdbcType="BIT"/>
    </association>
</resultMap>

SysControllerController控制类

在conteller文件夹里面新建一个SysConfigController类,该类是系统的设置类

@RestController
@RequestMapping("/system/config")
public class SystemConfigController {
    @Autowired
    MenuService menuService;

    @GetMapping("/menu")
    public List<Menu> getMenusByHrId() {
        return menuService.getMenusByHrId();
    }
}

要传入id了,id从哪里来,我们登陆的用户信息保存到security里面
SecurityContextHolder里面有一个getContext()方法。getAuthentication()它里面的getPrincipal(),Principal它是当前登陆的用户对象,然后强制转成Hr对象再获取它里面的id。

@Service
public class MenuService {
    @Autowired
    MenuMapper menuMapper;

    public List<Menu> getMenusByHrId() {
        //要传入id了,id从哪里来,我们登录的用户信息保存到security里面
        return menuMapper.getMenusByHrId(
                ((Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
        //SecurityContextHolder里面有一个getContext()方法.getAuthentication()它里面的
        // getPrincipal(),Principal它是当前登录的用户对象,然后强转成Hr对象再获取它里面的id
    }
}

MenuMapper接口

@Repository
public interface MenuMapper {
    List<Menu> getMenusByHrId(Integer hrId);
}

MenuMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.qwl.vhr.mapper.MenuMapper">

    <resultMap id="BaseResultMap" type="com.qwl.vhr.model.Menu">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="url" property="url" jdbcType="VARCHAR"/>
        <result column="path" property="path" jdbcType="VARCHAR"/>
        <result column="component" property="component" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="iconCls" property="iconcls" jdbcType="VARCHAR"/>
        <result column="parentId" property="parentid" jdbcType="INTEGER"/>
        <result column="enabled" property="enabled" jdbcType="BIT"/>
        <association property="meta" javaType="com.qwl.vhr.model.Meta">
            <result column="keepAlive" property="keepalive" jdbcType="BIT"/>
            <result column="requireAuth" property="requireauth" jdbcType="BIT"/>
        </association>
    </resultMap>

    <resultMap id="Menus2" type="com.qwl.vhr.model.Menu" extends="BaseResultMap">
        <collection property="children" ofType="com.qwl.vhr.model.Menu">
            <id column="id2" property="id" jdbcType="INTEGER"/>
            <result column="url2" property="url" jdbcType="VARCHAR"/>
            <result column="path2" property="path" jdbcType="VARCHAR"/>
            <result column="component2" property="component" jdbcType="VARCHAR"/>
            <result column="name2" property="name" jdbcType="VARCHAR"/>
            <result column="iconCls2" property="iconcls" jdbcType="VARCHAR"/>
            <result column="parentId2" property="parentid" jdbcType="INTEGER"/>
            <result column="enabled2" property="enabled" jdbcType="BIT"/>
            <association property="meta" javaType="com.qwl.vhr.model.Meta">
                <result column="keepAlive2" property="keepalive" jdbcType="BIT"/>
                <result column="requireAuth2" property="requireauth" jdbcType="BIT"/>
            </association>
        </collection>
    </resultMap>
    <select id="getMenusByHrId" resultMap="Menus2">
    select distinct m1.*,m2.`id` as id2,m2.`component` as component2,m2.`enabled` as enabled2,m2.`iconCls` as iconCls2,
    m2.`keepAlive` as keepAlive2,m2.`name` As name2,m2.`parentId` As parentId2,m2.`requireAuth` As requireAuth2,
    m2.`path` As path2 from menu m1,menu m2,hr_role hrr,menu_role mr where m1.`id`=m2.`parentId` and hrr.`rid`=mr.`rid`
    and hrr.`hrid`=#{hrid} and mr.`mid`=m2.`id` and m2.`enabled`=true order by m1.`id`,m2.`id`
    </select>
</mapper>
posted @ 2020-08-06 22:22  柒丶月  阅读(215)  评论(0编辑  收藏  举报