用Spring Boot+Vue做微人事项目第六天

前两天做了微人事登录的前端页面和后端接口,第三天则实现了前后端接口的对接,输入正确的用户名和密码之后,成功的跳转到home页。第四天做了Home页的Title制作和下拉菜单,下拉菜单有三个选项,个人中心、设置和注销登录,还做了注销登录,点击注销登录会出现提示:“此操作将注销登录,是否继续”,点是就重新跳转到登录页面,第五天做的是左边的导航菜单,现在是做服务端菜单接口的设计

menu表的各个列的意思:  menu表  服务端的菜单项数据

id:唯一标识   url:做权限控制用的   path:请求路径,对应了前端的请求路径  component:组件的名字  name:

iconCls:组件的图标    keepAlice:标记    是否保护     requrieAuth:标记  进入组件要不要登录     parentId:父菜单    enabled:菜单项是否启动

用Spring Boot+Vue做微人事项目第六天_第1张图片

Menu类

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

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

public class Menu implements Serializable {
    private Integer id;

    private String url;

    private String path;

    private String component;

    private String name;

    private String iconCls;

    private Integer parentId;

    private Boolean enabled;

    private Meta meta;

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

    private static final long serialVersionUID = 1L;

    //省略getter和setter
}

Meta类

public class Meta {

    private Boolean keepAlive;

    private Boolean requireAuth;

    //省略getter和setter
}

在MenuMapper.xml中新增


    
      
      
    

SysConfigController控制类

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

@RestController
@RequestMapping("/system/config")
public class SysConfigController {

    @Autowired
    MenuService menuService;;

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

MenuService业务类

要传入id了,id从哪里来,我们登录的用户信息保存到security里面

SecurityContextHolder里面有一个getContext()方法.getAuthentication()它里面的getPrincipal(),Principal它是当前登录的用户对象,然后强转成Hr对象再获取它里面的id

@Service
public class MenuService {

    @Autowired
    MenuMapper menuMapper;

    public List 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 {
    int deleteByPrimaryKey(Integer id);

    int insert(Menu record);

    int insertSelective(Menu record);

    Menu selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Menu record);

    int updateByPrimaryKey(Menu record);

    List getMenusByHrId(Integer hrid);
}

MenuMapper.xml




  
    
    
    
    
    
    
    
    

    
      
      
    
  
  
    
      
      
      
      
      
      
      
      
      
      
        
        
      
    
  
  
  
    id, url, `path`, component, `name`, iconCls, keepAlive, requireAuth, parentId, enabled
  
  
  
    delete from menu
    where id = #{id,jdbcType=INTEGER}
  
  
    insert into menu (id, url, `path`, 
      component, `name`, iconCls, 
      keepAlive, requireAuth, parentId, 
      enabled)
    values (#{id,jdbcType=INTEGER}, #{url,jdbcType=VARCHAR}, #{path,jdbcType=VARCHAR}, 
      #{component,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{iconcls,jdbcType=VARCHAR}, 
      #{keepalive,jdbcType=BIT}, #{requireauth,jdbcType=BIT}, #{parentid,jdbcType=INTEGER}, 
      #{enabled,jdbcType=BIT})
  
  
    insert into menu
    
      
        id,
      
      
        url,
      
      
        `path`,
      
      
        component,
      
      
        `name`,
      
      
        iconCls,
      
      
        keepAlive,
      
      
        requireAuth,
      
      
        parentId,
      
      
        enabled,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{url,jdbcType=VARCHAR},
      
      
        #{path,jdbcType=VARCHAR},
      
      
        #{component,jdbcType=VARCHAR},
      
      
        #{name,jdbcType=VARCHAR},
      
      
        #{iconcls,jdbcType=VARCHAR},
      
      
        #{keepalive,jdbcType=BIT},
      
      
        #{requireauth,jdbcType=BIT},
      
      
        #{parentid,jdbcType=INTEGER},
      
      
        #{enabled,jdbcType=BIT},
      
    
  
  
    update menu
    
      
        url = #{url,jdbcType=VARCHAR},
      
      
        `path` = #{path,jdbcType=VARCHAR},
      
      
        component = #{component,jdbcType=VARCHAR},
      
      
        `name` = #{name,jdbcType=VARCHAR},
      
      
        iconCls = #{iconcls,jdbcType=VARCHAR},
      
      
        keepAlive = #{keepalive,jdbcType=BIT},
      
      
        requireAuth = #{requireauth,jdbcType=BIT},
      
      
        parentId = #{parentid,jdbcType=INTEGER},
      
      
        enabled = #{enabled,jdbcType=BIT},
      
    
    where id = #{id,jdbcType=INTEGER}
  
  
    update menu
    set url = #{url,jdbcType=VARCHAR},
      `path` = #{path,jdbcType=VARCHAR},
      component = #{component,jdbcType=VARCHAR},
      `name` = #{name,jdbcType=VARCHAR},
      iconCls = #{iconcls,jdbcType=VARCHAR},
      keepAlive = #{keepalive,jdbcType=BIT},
      requireAuth = #{requireauth,jdbcType=BIT},
      parentId = #{parentid,jdbcType=INTEGER},
      enabled = #{enabled,jdbcType=BIT}
    where id = #{id,jdbcType=INTEGER}
  

 

你可能感兴趣的:(Spring,Boot+Vue做微人事项目)