微服务 教程
1085个小节阅读:196.8k
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
用户需要对其拥有的角色进行添加和删除,这样就需要查询所有的角色以及用户所拥有的角色。
先构建一个实体类——带状态的角色,状态代表着用户是否拥有该角色。
xxxxxxxxxx
// 带有状态的角色,状态表示用户是否拥有该角色
public class RoleWithStatus {
private Integer rid;
private String roleName; // 角色名
private String roleDesc; // 角色介绍
private Boolean adminHas; // 用户是否拥有该角色
}
编写RoleMapper
xxxxxxxxxx
public interface RoleMapper extends BaseMapper<Role> {
// 查询用户拥有的所有角色的id
List<Integer> findRoleIdByAdmin(Integer aid);
}
编写RoleMapper.xml
xxxxxxxxxx
<mapper namespace="com.itbaizhan.travel.mapper.RoleMapper">
<select id="findRoleIdByAdmin" parameterType="int" resultType="int">
SELECT rid FROM admin_role WHERE aid = #{aid}
</select>
</mapper>
编写AdminService
xxxxxxxxxx
// 查询用户的角色情况
public List<RoleWithStatus> findRole(Integer aid){
// 查询所有角色
List<Role> roles = roleMapper.selectList(null);
// 查询用户拥有的所有角色id
List<Integer> rids = roleMapper.findRoleIdByAdmin(aid);
// 带有状态的角色集合
List<RoleWithStatus> roleList = new ArrayList();
// 遍历所有角色
for (Role role:roles){
// 创建带有状态的角色
RoleWithStatus roleWithStatus = new RoleWithStatus();
// 将角色属性复制给带有状态的角色
BeanUtils.copyProperties(role,roleWithStatus);
if(rids.contains(role.getRid())){ //用户拥有该角色,角色状态设置为true
roleWithStatus.setAdminHas(true);
}else { //用户不拥有该角色,角色状态设置为false
roleWithStatus.setAdminHas(false);
}
roleList.add(roleWithStatus);
}
return roleList;
}
编写AdminController
xxxxxxxxxx
"/findRole") (
public ModelAndView findRole(Integer aid) {
List<RoleWithStatus> roles = adminService.findRole(aid);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("roles", roles);
modelAndView.addObject("aid", aid);
modelAndView.setViewName("/backstage/admin_role");
return modelAndView;
}
编写页面admin_role.html
xxxxxxxxxx
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>分配角色</title>
<th:block th:replace="/backstage/common_resources::common_css"></th:block>
<th:block th:replace="/backstage/common_resources::common_js"></th:block>
</head>
<body class="hold-transition skin-purple sidebar-mini">
<header th:replace="~{/backstage/common_header::header}"></header>
<aside th:replace="~{/backstage/common_aside::aside}"></aside>
<div class="wrapper">
<div class="content-wrapper">
<!-- 内容头部 -->
<section class="content-header">
<h1>
用户管理
<small>分配角色</small>
</h1>
<ol class="breadcrumb">
<li><a href="@{/backstage/index}"><i class="fa fa-dashboard"></i> 首页</a></li>
<li><a href="@{/backstage/admin/all}"> 用户管理</a></li>
<li class="active">分配角色</li>
</ol>
</section>
<!-- 内容头部 /-->
<!-- 正文区域 -->
<section class="content">
<!-- .box-body -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">列表</h3>
</div>
<div class="box-body">
<!-- 数据表格 -->
<div class="table-box">
<form th:action="@{/backstage/admin/updateRole}">
<input type="hidden" name="aid" th:value="${aid}">
<!--数据列表-->
<table id="dataList" class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right:0px;">
<input id="selall" type="checkbox" class="icheckbox_square-blue">
</th>
<th>角色名</th>
<th>角色描述</th>
</tr>
</thead>
<tbody>
<tr th:each="role:${roles}">
<td th:if="${role.adminHas}">
<input name="ids" type="checkbox" th:value="${role.rid}"
class="icheckbox_square-blue" checked>
</td>
<td th:if="${!role.adminHas}">
<input name="ids" type="checkbox" th:value="${role.rid}"
class="icheckbox_square-blue">
</td>
<td th:text="${role.roleName}"></td>
<td th:text="${role.roleDesc}"></td>
</tr>
</tbody>
</table>
<div class="box-title text-center">
<button type="submit" class="btn bg-maroon">保存</button>
<button type="button" class="btn bg-default" onclick="history.back(-1);">返回</button>
</div>
</form>
<!--数据列表/-->
</div>
<!-- 数据表格 /-->
</div>
<!-- /.box-body -->
</div>
</section>
<!-- 正文区域 /-->
</div>
</div>
<footer th:replace="~{/backstage/common_footer::footer}"></footer>
</body>
</html>
启动项目,测试功能