026-云E办_操作员
一、简介:
- 可以通过搜索操作员的名字,来单独显示操作员的信息。
- 展示所有操作员的时候,不会把自己当前登录的操作员显示出来。
- 操作员涉及了权限:操作员拥有哪些角色,在根据角色再去拥有哪些菜单的权限。
二、搜索
controller
为什么要写多表查询,是因为现在操作员所拥有的角色。
package com.xxxx.server.controller;
@RestController
@RequestMapping("/system/admin")
public class AdminController操作员 {
@Autowired
private IAdminService iaDminservice;
//搜索:keywords是传参
@ApiOperation(value = "获取所有操作员")
@GetMapping("/")
//获取操作员前提是搜索,所有得传参进来。返回的是操作员列表
public List<Admin> getAllAdmins(String keywords){
return iaDminservice.getAllAdmins(keywords);
}
}
service
/**
* 获取所有的操作员
* @param keywords
* @return
*/
List<Admin> getAllAdmins(String keywords);
/**
* 获取所有操作员
* @param keywords
* @return
* 由于不能显示,当前登录的用户操作员。
* 所有传入:1.keywords 2.当前登录的用户id
*/
@Override
public List<Admin> getAllAdmins(String keywords) {
return adminMapper.getAllAdmins(AdminUtils.getCurrentAdmin().getId(),keywords);
}
抽取一个类:获取当前用户信息:
package com.xxxx.server;
import com.xxxx.server.pojo.Admin;
import org.springframework.security.core.context.SecurityContextHolder;
public class AdminUtils {
/**
* 获取当前登录的操作员.
*/
public static Admin getCurrentAdmin(){
return (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
}
mapper
List<Admin> getAllAdmins(@Param("id") Integer id, @Param("keywords") String keywords);
<resultMap id="AdminWithRole" type="com.xxxx.server.pojo.Admin" extends="BaseResultMap">
<collection property="roles" ofType="com.xxxx.server.pojo.Role">
<id column="rid" property="id"/>
<result column="rname" property="name"/>
<result column="rnameZh" property="nameZh"/>
</collection>
</resultMap>
<!--获取所有操作员
为什么要写SQL语句,是因为现在操作员所拥有的角色。
-->
<select id="getAllAdmins" resultMap="AdminWithRole">
SELECT
a.id,
a.`name`,
a.phone,
a.telephone,
a.address,
a.enabled,
a.username,
a.userface,
a.remark,
r.id AS rid,
r.`name` AS rname,
r.nameZh AS rnameZh
FROM
t_admin AS a
LEFT JOIN t_admin_role AS ar ON ar.adminId = a.id
LEFT JOIN t_role AS r ON ar.rid = r.id
WHERE
a.id != #{id}
<if test="null!=keywords and ''!=keywords">
AND a.name like concat ('%',#{keywords},'%')
</if>
ORDER BY
a.id
</select>
测试
- 现在是用户管理员登录中, 所以没有用户管理员:
三、更新和删除:
controller
@ApiOperation(value = "更新操作员")
@PutMapping("/")
public RespBean updateAdmin(@RequestBody Admin admin){
if (iaDminservice.updateById(admin)){
return RespBean.success("更新成功");
}
return RespBean.error("更新失败");
}
@ApiOperation("删除操作员")
@DeleteMapping("/{id}")
public RespBean deleteAdmin(@PathVariable Integer id){
if(iaDminservice.removeById(id)){
return RespBean.success("删除成功");
}
return RespBean.error("删除失败");
}
文档 更新列表:
{
"id": 2,
"name": "何淑华12test",
"phone": "18875971675",
"telephone": "41413109",
"address": "河北省秀荣市萧山长沙街p座 737268",
"enabled": true,
"username": "taomeng",
"password": null,
"userFace": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585830947922&di=60b35821fb9112d0aad6915efe982c8d&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201703%2F26%2F20170326161532_aGteC.jpeg",
"remark": null
}
三、操作员角色功能
1.获取所有的角色
2.更新操作员的角色
controller
@Autowired
private IRoleService iRoleService;
@ApiOperation(value = "获取所有角色")
@GetMapping("/roles")
public List<Role> getAllRoles(){
return iRoleService.list();
}
@ApiOperation(value = "更新操作员角色")
@PutMapping("/role")
public RespBean updateAdminRole(Integer adminId,Integer[] rids){
return iaDminservice.updateAdminRole(adminId,rids);
}
service
/**
* 跟新操作员角色
* @param adminId
* @param rids
* @return
*/
RespBean updateAdminRole(Integer adminId, Integer[] rids);
---
@Autowired
private AdminRoleMapper adminRoleMapper;
@Override
@Transactional
public RespBean updateAdminRole(Integer adminId, Integer[] rids) {
//删除角色:
adminRoleMapper.delete(new QueryWrapper<AdminRole>().eq("adminId",adminId));
Integer result = adminRoleMapper.updateAdminRole(adminId, rids);
if (rids.length == result){
//受影响的行数是一样的,说明添加成功了
return RespBean.success("更新成功");
}
return RespBean.error("更新失败");
}
mapper
/**
* 跟新操作员角色
* @param adminId
* @param rids
* @return
*/
Integer updateAdminRole(@Param("adminId") Integer adminId, @Param("rids") Integer[] rids);
<!--更新操作员角色
先清空,然后在集体的添加
-->
<update id="updateAdminRole">
insert into t_admin_role(adminId,rid) values
<foreach collection="rids" item="rid" separator=",">
(#{adminId},#{rid})
</foreach>
</update>