需求
在服务端需要进行角色的创建分配和管理工作,用springboot实现
实现
首先编写实现所需的实体类Role、RoleInfo等
public class Role {
int uid;
String name;
public Role(String name) {
this.name = name;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public RoleInfo() {
}
public RoleInfo(String roleName, List<Integer> files, List<Integer> devices) {
this.roleName = roleName;
this.files = files;
this.devices = devices;
}
通过RoleController接受请求创建角色
@PostMapping("uploadRole")
Msg addRole(@RequestBody List<RoleInfo> roleInfos, @RequestParam int meetingId){
if(roleService.addRole(roleInfos,meetingId)){
return new Msg(200,"ok",null);
}
return null;
}
然后通过RoleService具体实现创建角色的功能
private RoleDao roleDao;
public boolean addRole(List<RoleInfo> roleInfos, int meetingId) {
for (RoleInfo roleInfo : roleInfos) {
Role role = new Role(roleInfo.getRoleName());
roleDao.addRole(role);
deviceRegister(role.getUid(), roleInfo.getDevices(), meetingId);
filePermit(role.getUid(), roleInfo.getFiles());
}
return true;
}
private void deviceRegister(int roleId, List<Integer> devices, int meetingId) {
for (int device : devices) {
roleDao.addDeviceToRole(roleId, device, meetingId);
}
}
private void filePermit(int roleId, List<Integer> files) {
for (int fileId : files) {
roleDao.addFileToRole(roleId, fileId);
}
每个角色都绑定了设备和文件,并在数据库中添加数据。
角色创建也调用dao层,在数据库中增删记录
public interface RoleDao {
int addRole(Role role);
int addDeviceToRole(int roleId,int deviceId,int meetingId);
int addFileToRole(int roleId,int fileId);
}
<insert id="addRole" useGeneratedKeys="true" keyProperty="uid">
insert into `role` values (#{uid}, #{name})
</insert>
<insert id="addDeviceToRole">
insert into `device_role` values (#{roleId},#{deviceId},#{meetingId})
</insert>
<insert id="addFileToRole">
insert into `role_file` values(#{roleId},#{fileId})
</insert>
至此整个角色创建功能就完成了。