要实现图标功能,首先是后端建表
DROP TABLE IF EXISTS `dict`;
CREATE TABLE `dict` (
`name` varchar(255) DEFAULT NULL COMMENT '名称',
`value` varchar(255) DEFAULT NULL COMMENT '内容',
`type` varchar(255) DEFAULT NULL COMMENT '类型'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `dict`(`name`,`value`,`type`) values
('user','el-icon-user','icon'),
('house','el-icon-house','icon'),
('menu','el-icon-menu','icon'),
('s-custom','el-icon-s-custom','icon'),
('s-menu','el-icon-s-grid','icon'),
('file','el-icon-document','icon');
Dict.java
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("dict")
public class Dict {
private String name;
private String value;
private String type;
}
@Repository
public interface DictMapper extends BaseMapper<Dict> {
}
&#64;Service
public class DictService extends ServiceImpl<DictMapper, Dict> {
}
MenuController.java
//查找图标信息
&#64;GetMapping("/icons")
public Result getIcons(){
QueryWrapper<Dict> queryWrapper &#61; new QueryWrapper<>();
//查询type为icon的数据
queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
return Result.success(dictService.list(queryWrapper));
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GJscqE2B-1673355144545)(D:\桌面\Java学习\项目\管理系统–前后端分离\项目截图\46.jpg)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XbCYwnVm-1673355144546)(D:\桌面\Java学习\项目\管理系统–前后端分离\项目截图\47.jpg)]
{{ data.name }}
Menu.vue
confirm-button-text&#61;&#39;确定&#39;
cancel-button-text&#61;&#39;我再想想&#39;
icon&#61;"el-icon-info"
icon-color&#61;"red"
title&#61;"您确定要删除这些内容吗&#xff1f;"
&#64;confirm&#61;"delBatch"
>
confirm-button-text&#61;&#39;确定&#39;
cancel-button-text&#61;&#39;我再想想&#39;
icon&#61;"el-icon-info"
icon-color&#61;"red"
title&#61;"您确定要删除吗&#xff1f;"
&#64;confirm&#61;"handleDelete(scope.row.id)"
>
{{ item.name }}
Role.vue
confirm-button-text&#61;&#39;确定&#39;
cancel-button-text&#61;&#39;我再想想&#39;
icon&#61;"el-icon-info"
icon-color&#61;"red"
title&#61;"您确定要删除这些内容吗&#xff1f;"
&#64;confirm&#61;"delBatch"
>
confirm-button-text&#61;&#39;确定&#39;
cancel-button-text&#61;&#39;我再想想&#39;
icon&#61;"el-icon-info"
icon-color&#61;"red"
title&#61;"您确定要删除吗&#xff1f;"
&#64;confirm&#61;"handleDelete(scope.row.id)"
>
&#64;current-change&#61;"handleCurrentChange"
:current-page&#61;"pageNum"
:page-sizes&#61;"[2, 4, 6, 10]"
:page-size&#61;"pageSize"
layout&#61;"total, sizes, prev, pager, next, jumper"
:total&#61;"total">
:data&#61;"menuData"
show-checkbox
node-key&#61;"id"
ref&#61;"tree"
:default-expanded-keys&#61;"expands"
:default-checked-keys&#61;"checks">
{{ data.name }}
2、实现菜单管理和角色管理关系
实现&#xff1a;角色管理中&#xff0c;分配菜单后&#xff0c;会出现一个role_id对应一个或者多个menu_id &#xff0c;两者id之间的关系可以储存到数据库中&#xff0c;然后下次点开该角色的分配菜单时默认回显出来
USE &#96;management&#96;;
/*Table structure for table &#96;role_menu&#96; */
DROP TABLE IF EXISTS &#96;role_menu&#96;;
CREATE TABLE &#96;role_menu&#96; (
&#96;role_id&#96; int(11) NOT NULL COMMENT &#39;角色id&#39;,
&#96;menu_id&#96; int(11) NOT NULL COMMENT &#39;菜单id&#39;,
PRIMARY KEY (&#96;role_id&#96;,&#96;menu_id&#96;)
) ENGINE&#61;InnoDB DEFAULT CHARSET&#61;utf8 COMMENT&#61;&#39;角色菜单关系表&#39;;
&#64;Data
&#64;TableName("role_menu")
public class RoleMenu {
private Integer roleId;
private Integer menuId;
}
和之前一样&#xff0c;把RoleMenuMapper和RoleMenuService建好&#xff0c;这里就省略
RoleController.java
//绑定角色和菜单的关系。一个角色id可以对应多个菜单id&#xff08;一个角色可以有权限管理多个菜单&#xff09;
//参数&#xff1a; roleId &#xff0c;menuIds
&#64;PostMapping("/roleMenu/{roleId}")
public Result roleMenu(&#64;PathVariable Integer roleId, &#64;RequestBody List<Integer> menuIds){
//复杂的sql操作&#xff0c;需要去service层写业务
roleService.setRoleMenu(roleId,menuIds);
return Result.success();
}
//从数据库中根据roleId查询出与这个角色关联的menuId
&#64;GetMapping("/roleMenu/{roleId}")
public Result getRoleMenu(&#64;PathVariable Integer roleId){
return Result.success(roleService.getRoleMenu(roleId));
}
RoleService.java
&#64;Service
public class RoleService extends ServiceImpl<RoleMapper, Role> {
&#64;Autowired
private RoleMenuService roleMenuService;
&#64;Autowired
private RoleMenuMapper roleMenuMapper;
//加上事务注解&#xff0c;两个操作同时成功才行
&#64;Transactional
public void setRoleMenu(Integer roleId, List<Integer> menuIds) {
QueryWrapper<RoleMenu> queryWrapper &#61; new QueryWrapper<>();
//先删除当前角色id所有的绑定关系
queryWrapper.eq("role_id",roleId);
roleMenuService.remove(queryWrapper);
//再把前端传过来的菜单id数组绑定到当前这个角色id上去
for (Integer menuId:menuIds){
RoleMenu roleMenu &#61; new RoleMenu();
roleMenu.setRoleId(roleId);
roleMenu.setMenuId(menuId);
roleMenuService.save(roleMenu);
}
}
//查询
public List<Integer> getRoleMenu(Integer roleId) {
return roleMenuMapper.selectByRoleId(roleId);
}
}
&#64;Repository
public interface RoleMenuMapper extends BaseMapper<RoleMenu> {
&#64;Select("select menu_id from role_menu where role_id &#61; #{roleId}")
List<Integer> selectByRoleId(&#64;Param("roleId") Integer roleId);
}
saveRoleMenu(){
this.request.post("/role/roleMenu/" &#43; this.roleId,this.$refs.tree.getCheckedKeys()).then(res &#61;> {
if(res.code &#61;&#61;&#61; &#39;200&#39;) {
this.$message.success("绑定成功")
this.menuDialogVisable &#61; false
}else{
this.$message.error(res.msg)
}
})
},
this.request.get("/role/roleMenu/" &#43; roleId).then(res &#61;> {
this.checks &#61; res.data
})
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YXBNwzKL-1673355144547)(D:\桌面\Java学习\项目\管理系统–前后端分离\项目截图\48.jpg)]
这次改动的前端代码就是Role.vue和Menu.vue两个页面&#xff0c;完整代码在前面已经给出了&#xff01;
通过实现&#xff0c;完成这两个功能&#xff0c;就成功了&#xff01;