用Spring Boot+Vue做微人事项目系列目录
前两天做了微人事登录的前端页面和后端接口,第三天则实现了前后端接口的对接,输入正确的用户名和密码之后,成功的跳转到home页。第四天做了Home页的Title制作和下拉菜单,下拉菜单有三个选项,个人中心、设置和注销登录,还做了注销登录,点击注销登录会出现提示:“此操作将注销登录,是否继续”,点是就重新跳转到登录页面,第五天做的是左边的导航菜单,现在是做服务端菜单接口的设计
menu表的各个列的意思: menu表 服务端的菜单项数据
id:唯一标识 url:做权限控制用的 path:请求路径,对应了前端的请求路径 component:组件的名字 name:
iconCls:组件的图标 keepAlice:标记 是否保护 requrieAuth:标记 进入组件要不要登录 parentId:父菜单 enabled:菜单项是否启动
Menu类
该类本来还有KeepAlive和requireAuth两个属性,但是这两个属性不常用,所以就放到新建的Meta类里面去了,Menu新添加了一个private List<Menu> 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<Menu> 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中新增
<!-- 一对多的 -->
<association property="meta" javaType="com.lqg.vhr.model.Meta">
<result column="keepAlive" jdbcType="BIT" property="keepAlive" />
<result column="requireAuth" jdbcType="BIT" property="requireAuth" />
</association>
SysConfigController控制类
在controller文件夹里面新建一个SysConfigController类,该类是系统设置类
@RestController
@RequestMapping("/system/config")
public class SysConfigController {
@Autowired
MenuService menuService;;
@GetMapping("/menu")
public List<Menu> getMenusByHrId(){
return menuService.getMenusByHrId();
}
}
MenuService业务类
要传入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 {
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<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.lqg.vhr.mapper.MenuMapper">
<resultMap id="BaseResultMap" type="com.lqg.vhr.model.Menu">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="url" jdbcType="VARCHAR" property="url" />
<result column="path" jdbcType="VARCHAR" property="path" />
<result column="component" jdbcType="VARCHAR" property="component" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="iconCls" jdbcType="VARCHAR" property="iconCls" />
<result column="parentId" jdbcType="INTEGER" property="parentId" />
<result column="enabled" jdbcType="BIT" property="enabled" />
<!-- 一对多的 -->
<association property="meta" javaType="com.lqg.vhr.model.Meta">
<result column="keepAlive" jdbcType="BIT" property="keepAlive" />
<result column="requireAuth" jdbcType="BIT" property="requireAuth" />
</association>
</resultMap>
<resultMap id="Menus2" type="com.lqg.vhr.model.Menu" extends="BaseResultMap">
<collection property="children" ofType="com.lqg.vhr.model.Menu">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="url" jdbcType="VARCHAR" property="url" />
<result column="path" jdbcType="VARCHAR" property="path" />
<result column="component" jdbcType="VARCHAR" property="component" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="iconCls" jdbcType="VARCHAR" property="iconCls" />
<result column="parentId" jdbcType="INTEGER" property="parentId" />
<result column="enabled" jdbcType="BIT" property="enabled" />
<!-- 一对多的 -->
<association property="meta" javaType="com.lqg.vhr.model.Meta">
<result column="keepAlive" jdbcType="BIT" property="keepAlive" />
<result column="requireAuth" jdbcType="BIT" property="requireAuth" />
</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.hrid=#{hrid} and hrr.rid=mr.rid and mr.mid=m2.id AND m2.enabled=TRUE
ORDER BY m1.id,m2.id
</select>
<sql id="Base_Column_List">
id, url, `path`, component, `name`, iconCls, keepAlive, requireAuth, parentId, enabled
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from menu
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from menu
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.lqg.vhr.model.Menu">
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>
<insert id="insertSelective" parameterType="com.lqg.vhr.model.Menu">
insert into menu
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="url != null">
url,
</if>
<if test="path != null">
`path`,
</if>
<if test="component != null">
component,
</if>
<if test="name != null">
`name`,
</if>
<if test="iconcls != null">
iconCls,
</if>
<if test="keepalive != null">
keepAlive,
</if>
<if test="requireauth != null">
requireAuth,
</if>
<if test="parentid != null">
parentId,
</if>
<if test="enabled != null">
enabled,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="url != null">
#{url,jdbcType=VARCHAR},
</if>
<if test="path != null">
#{path,jdbcType=VARCHAR},
</if>
<if test="component != null">
#{component,jdbcType=VARCHAR},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="iconcls != null">
#{iconcls,jdbcType=VARCHAR},
</if>
<if test="keepalive != null">
#{keepalive,jdbcType=BIT},
</if>
<if test="requireauth != null">
#{requireauth,jdbcType=BIT},
</if>
<if test="parentid != null">
#{parentid,jdbcType=INTEGER},
</if>
<if test="enabled != null">
#{enabled,jdbcType=BIT},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.lqg.vhr.model.Menu">
update menu
<set>
<if test="url != null">
url = #{url,jdbcType=VARCHAR},
</if>
<if test="path != null">
`path` = #{path,jdbcType=VARCHAR},
</if>
<if test="component != null">
component = #{component,jdbcType=VARCHAR},
</if>
<if test="name != null">
`name` = #{name,jdbcType=VARCHAR},
</if>
<if test="iconcls != null">
iconCls = #{iconcls,jdbcType=VARCHAR},
</if>
<if test="keepalive != null">
keepAlive = #{keepalive,jdbcType=BIT},
</if>
<if test="requireauth != null">
requireAuth = #{requireauth,jdbcType=BIT},
</if>
<if test="parentid != null">
parentId = #{parentid,jdbcType=INTEGER},
</if>
<if test="enabled != null">
enabled = #{enabled,jdbcType=BIT},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.lqg.vhr.model.Menu">
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}
</update>
</mapper>
觉得文章对自己有用,想要继续学下去的可以
可以长按下方二维码
关注【javalingfeng】哦
公众号正在更新vue.js和springboot的详细教程
还有超多免费Java,Python,Android等系列教程
等你来领