紧接着上一篇,这篇实现我们的操作员管理
目录
- 1.操作员管理
- 1.1.功能实现
- 1.2.测试
有关对管理员的操作无非也就是增删改(角色、操作员)查(角色、操作员),但是查包括精确查询和模糊查询,所以SQL语句需要我们自己写。还有就是我们在Admin实体类中使用了
@Data
注解,它会自动生成Getter
和Setter
方法,但是这与UserDetails
中的isEnabled()
方法冲突,系统无法分辨,所以我们需要规定该用哪一个。在enabled
属性上添加@Getter(AccessLevel.NONE)
注解让其不会生成Getter
方法。
1.1.1.修改实体类
Admin.java
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_admin")
@ApiModel(value="Admin对象", description="")
public class Admin implements Serializable , UserDetails {
......
@ApiModelProperty(value = "联系地址")
private String address;
@ApiModelProperty(value = "是否启用")
@Getter(AccessLevel.NONE)
private Boolean enabled;
......
}
1.1.1.修改对应映射文件
AdminMapper.xml
<resultMap id="AdminWithRole" type="com.kt.pojo.Admin" extends="BaseResultMap">
<collection property="roles" ofType="com.kt.pojo.Role">
<id column="rid" property="id"/>
<result column="rname" property="name"/>
<result column="rnameZh" property="nameZh"/>
collection>
resultMap>
<select id="getAllAdmins" resultMap="AdminWithRole">
SELECT DISTINCT
a.*,
r.id AS rid,
r.`name` AS rname,
r.nameZh AS rnameZh
FROM
t_admin a
LEFT JOIN t_admin_role ar ON a.id = ar.adminId
LEFT JOIN t_role r ON r.id = ar.rid
WHERE
a.id != #{id}
<if test="null!=keywords and ''!=keywords">
AND a.`name` LIKE CONCAT( '%', #{keywords}, '%' )
if>
ORDER BY
a.id
select>
AdminRoleMapper.xml
<update id="updateAdminRole">
insert into t_admin_role(adminId, rid) values
<foreach collection="rids" item="rid" separator=",">
(#{adminId}, #{rid})
foreach>
update>
1.1.2.Mapper接口
Admin.java
public interface AdminMapper extends BaseMapper<Admin> {
/**
* 获取所有操作员
* @param id
* @param keywords
* @return
*/
List<Admin> getAllAdmins(@Param("id") Integer id, @Param("keywords") String keywords);
}
AdminRoleMapper.java
public interface AdminRoleMapper extends BaseMapper<AdminRole> {
/**
* 更新操作员角色
* @param adminId
* @param rids
* @return
*/
Integer updateAdminRole(@Param("adminId") Integer adminId, @Param("rids") Integer[] rids);
}
1.1.3.服务类
IAdminService.java
public interface IAdminService extends IService<Admin> {
/**
* 登录之后返回Token
* @param username
* @param password
* @param code
* @param request
* @return
*/
RespBean login(String username, String password, String code, HttpServletRequest request);
/**
* 根据用户名获取用户
* @param username
* @return
*/
Admin getAdminByUserName(String username);
/**
* 根据用户id查询角色列表
* @param adminId
* @return
*/
List<Role> getRoles(Integer adminId);
/**
* 获取所有操作员
* @param keywords
* @return
*/
List<Admin> getAllAdmins(String keywords);
/**
* 更新操作员角色
* @param adminId
* @param rids
* @return
*/
RespBean updateAdminRole(Integer adminId, Integer[] rids);
}
1.1.4.服务实现类
AdminServiceImpl.java
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
......
@Override
public Admin getAdminByUserName(String username) {
// 用户名、启用状态
return adminMapper.selectOne(new QueryWrapper<Admin>().eq("username", username).eq("enabled", true));
}
@Override
public List<Role> getRoles(Integer adminId) {
return roleMapper.getRoles(adminId);
}
@Override
public List<Admin> getAllAdmins(String keywords) {
return adminMapper.getAllAdmins(AdminUtils.getCurrentAdmin().getId(), keywords);
}
@Override
@Transactional(rollbackFor = Exception.class)
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("更新失败!");
}
}
1.1.5.控制类
AdminController.java
@RestController
@RequestMapping("/system/admin")
public class AdminController {
@Resource
private IAdminService adminService;
@Resource
private IRoleService roleService;
@ApiOperation(value = "获取所有操作员")
@GetMapping("/")
public List<Admin> getAllAdmins(String keywords) {
return adminService.getAllAdmins(keywords);
}
@ApiOperation(value = "更新操作员")
@PutMapping("/")
public RespBean updateAdmin(@RequestBody Admin admin) {
if (adminService.updateById(admin)) {
return RespBean.success("更新成功!");
}
return RespBean.error("更新失败!");
}
@ApiOperation(value = "删除操作员")
@DeleteMapping("/{id}")
public RespBean deleteAdmin(@PathVariable Integer id) {
if (adminService.removeById(id)) {
return RespBean.success("删除成功!");
}
return RespBean.error("删除失败!");
}
@ApiOperation(value = "获取所有角色")
@GetMapping("/roles")
public List<Role> getAllRoles() {
return roleService.list();
}
@ApiOperation(value = "更新操作员角色")
@PutMapping("/role")
public RespBean updateAdminRole(Integer adminId, Integer[] rids) {
return adminService.updateAdminRole(adminId, rids);
}
}
获取所有操作员:
更新操作员(我直接copy上面的数据进行更新,将安淑华123
改为华
,密码不修改,不要“authority”参数):
{
"id": 3,
"name": "安淑华123",
"phone": "14588110811",
"telephone": "50603155",
"address": "山东省凤英县长寿银川街l座",
"enabled": true,
"username": "naqiao",
"userFace": "http://desiy.oss-cn-chengdu.aliyuncs.com/img/testavatar.jpg",
"remark": null,
"roles": [
{
"id": 3,
"name": "ROLE_recruiter",
"nameZh": "招聘主管"
},
{
"id": 4,
"name": "ROLE_train",
"nameZh": "培训主管"
}
],
"accountNonLocked": true,
"credentialsNonExpired": true,
"accountNonExpired": true
}
删除操作员功能这里就不演示了。
获取所有角色:
更新操作员角色:
更新前
操作后:
到这里操作员管理模块就完成了,下一篇博客实现员工管理:
Mybatis-Plus实现分页展示数据