进销存系统_角色和资源管理(13)
作者:互联网
项目结构
一、角色管理
1.1、角色授权
- 界面原型
- 角色授权流程
1.2、权限回显
- 界面原型
- 权限回显流程
1.3、代码
IRoleMenuService.java
package com.xbmu.admin.service;
import com.xbmu.admin.pojo.RoleMenu;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* <p>
* 角色菜单表 服务类
* </p>
*
* @author bitaotao
* @since 2021-09-11
*/
public interface IRoleMenuService extends IService<RoleMenu> {
List<Integer> queryRoleHasAllMenusByRoleId(Integer roleId);
List<String> findAuthoritiesByRoleName(List<String> roleNames);
}
RoleMenuServiceImpl.java
package com.xbmu.admin.service.impl;
import com.xbmu.admin.pojo.RoleMenu;
import com.xbmu.admin.mapper.RoleMenuMapper;
import com.xbmu.admin.service.IRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <p>
* 角色菜单表 服务实现类
* </p>
*
* @author bitaotao
* @since 2021-09-11
*/
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {
@Override
public List<Integer> queryRoleHasAllMenusByRoleId(Integer roleId) {
return this.baseMapper.queryRoleHasAllMenusByRoleId(roleId);
}
@Override
public List<String> findAuthoritiesByRoleName(List<String> roleNames) {
return this.baseMapper.findAuthoritiesByRoleName(roleNames);
}
}
RoleMenuMapper.java
package com.xbmu.admin.mapper;
import com.xbmu.admin.pojo.RoleMenu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
/**
* <p>
* 角色菜单表 Mapper 接口
* </p>
*
* @author bitaotao
* @since 2021-09-11
*/
public interface RoleMenuMapper extends BaseMapper<RoleMenu> {
List<Integer> queryRoleHasAllMenusByRoleId(Integer roleId);
List<String> findAuthoritiesByRoleName(List<String> roleNames);
}
src/main/resources/mapper/RoleMenuMapper.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.xbmu.admin.mapper.RoleMenuMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.xbmu.admin.pojo.RoleMenu">
<id column="id" property="id" />
<result column="menu_id" property="menuId" />
<result column="role_id" property="roleId" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, menu_id, role_id
</sql>
<select id="queryRoleHasAllMenusByRoleId" resultType="java.lang.Integer">
SELECT menu_id FROM t_role_menu WHERE role_id = #{roldId}
</select>
<select id="findAuthoritiesByRoleName" resultType="java.lang.String">
SELECT DISTINCT
m.acl_value
FROM
t_role_menu rm
LEFT JOIN t_menu m ON rm.menu_id = m.id
LEFT JOIN t_role r ON rm.role_id = r.id
WHERE
m.is_del = 0
AND r.NAME IN
<foreach collection="list" item="roleName" open="(" separator="," close=")">
#{roleName}
</foreach>
</select>
</mapper>
TreeDto.java
package com.xbmu.admin.dto;
/**
* 树形 数据传输
* @author bitaotao
* @since 2021-09-21
*/
public class TreeDto {
private Integer id;
private Integer pId;
private String name;
private Boolean checked=false;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getpId() {
return pId;
}
public void setpId(Integer pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
}
MenuController.java
package com.xbmu.admin.controller;
import com.xbmu.admin.dto.TreeDto;
import com.xbmu.admin.service.IMenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
/**
* <p>
* 菜单表 前端控制器
* </p>
*
* @author bitaotao
* @since 2021-09-11
*/
@Controller
@RequestMapping("/menu")
public class MenuController {
@Resource
private IMenuService menuService;
/**
* 返回所有菜单数据
* @return
*/
@RequestMapping("queryAllMenus")
@ResponseBody
public List<TreeDto> queryAllMenus(Integer roleId){
return menuService.queryAllMenus(roleId);
}
}
MenuServiceImpl.java
package com.xbmu.admin.service.impl;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xbmu.admin.dto.TreeDto;
import com.xbmu.admin.pojo.Menu;
import com.xbmu.admin.mapper.MenuMapper;
import com.xbmu.admin.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbmu.admin.service.IRoleMenuService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* <p>
* 菜单表 服务实现类
* </p>
*
* @author bitaotao
* @since 2021-09-11
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
@Resource
private IRoleMenuService roleMenuService;
@Override
public List<TreeDto> queryAllMenus(Integer roleId) {
List<TreeDto> treeDtos = this.baseMapper.queryAllMenus();
List<Integer> roleHasMenuIds = roleMenuService.queryRoleHasAllMenusByRoleId(roleId);
if(CollectionUtils.isNotEmpty(roleHasMenuIds)){
treeDtos.forEach(treeDto -> {
if(roleHasMenuIds.contains(treeDto.getId())){
treeDto.setChecked(true);
}
});
}
return treeDtos;
}
}
MenuMapper.java
package com.xbmu.admin.mapper;
import com.xbmu.admin.dto.TreeDto;
import com.xbmu.admin.pojo.Menu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
/**
* <p>
* 菜单表 Mapper 接口
* </p>
*
* @author bitaotao
* @since 2021-09-11
*/
public interface MenuMapper extends BaseMapper<Menu> {
List<TreeDto> queryAllMenus();
}
src/main/resources/mapper/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.xbmu.admin.mapper.MenuMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.xbmu.admin.pojo.Menu">
<id column="id" property="id" />
<result column="icon" property="icon" />
<result column="name" property="name" />
<result column="state" property="state" />
<result column="url" property="url" />
<result column="p_id" property="pId" />
<result column="acl_value" property="aclValue" />
<result column="grade" property="grade" />
<result column="is_del" property="isDel" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, icon, name, state, url, p_id, acl_value, grade, is_del
</sql>
<select id="queryAllMenus" resultType="com.xbmu.admin.dto.TreeDto">
SELECT
id,
p_id AS pId,
`name`
FROM
t_menu
WHERE
is_del = 0
</select>
</mapper>
RoleController.java
/**
* 权限添加页面
* @param roleId
* @param model
* @return
*/
@RequestMapping("toAddGrantPage")
public String toAddGrantPage(Integer roleId,Model model){
model.addAttribute("roleId",roleId);
return "role/grant";
}
/**
* 角色授权接口
* @param roleId
* @param mids
* @return
*/
@RequestMapping("addGrant")
@ResponseBody
public RespBean addGrant(Integer roleId,Integer[] mids){
roleService.addGrant(roleId,mids);
return RespBean.success("角色记录授权成功");
}
RoleServiceImpl.java
@Override
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public void addGrant(Integer roleId, Integer[] mids) {
/**
* 1.参数校验
* roleId 非空 必须存在
* 2.授权
* 2.1 单次授权
* 直接批量添加即可
* 2.2 第2+次授权
* 如果存在原始权限 此时删除原始权限 然后添加新的权限记录
* 如果不存在 直接批量添加即可
*
* 合并2.1 2.2 原始权限不管是否存在,先执行权限记录查询.如果存在,直接删除(根据角色id)
* 执行批量添加(数组非空 数量>0)
*/
Role role = this.getById(roleId);
AssertUtil.isTrue(null == role,"待授权的角色记录不存在!");
int count = roleMenuService.count(new QueryWrapper<RoleMenu>().eq("role_id", roleId));
if(count > 0){
AssertUtil.isTrue(!(roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("role_id",roleId))),"角色授权失败!");
}
if(null != mids && mids.length > 0){
List<RoleMenu> roleMenus = new ArrayList<>();
for (Integer mid:mids) {
RoleMenu roleMenu = new RoleMenu();
roleMenu.setRoleId(roleId);
roleMenu.setMenuId(mid);
roleMenus.add(roleMenu);
}
AssertUtil.isTrue(!(roleMenuService.saveBatch(roleMenus)),"角色授权失败!");
}
}
标签:admin,13,进销存,roleId,public,xbmu,import,com,资源管理 来源: https://blog.csdn.net/btt2013/article/details/120595572