<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.server.mapper.AdminMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.xxx.server.pojo.Admin">
<!-- -->
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="phone" property="phone"/>
<result column="telephone" property="telephone"/>
<result column="address" property="address"/>
<result column="enabled" property="enabled"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="user_face" property="userFace"/>
<result column="remark" property="remark"/>
</resultMap>
<resultMap id="AdminWithRole" type="com.xxx.server.pojo.Admin" extends="BaseResultMap">
<collection property="roles" ofType="com.xxx.server.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
a.*,
r.id AS rid,
r.name AS rname,
r.name_zh AS rnameZh
FROM
t_admin a
LEFT JOIN
t_admin_role ar ON a.id = ar.`admin_id`
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>
</mapper>
返回的对象
package com.xxx.server.pojo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.xxx.server.config.CustomAuthorityDeserialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
/**
* <p>
* 管理员表
* </p>
*
* @author Bing
* @since 2021-01-13
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_admin")
@ApiModel(value = "Admin对象", description = "管理员表")
public class Admin implements Serializable, UserDetails {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "id")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "手机号码")
private String phone;
@ApiModelProperty(value = "住宅电话")
private String telephone;
@ApiModelProperty(value = "联系地址")
private String address;
@ApiModelProperty(value = "是否启用")
@Getter(AccessLevel.NONE) // 不需要生成 get 方法,防止与 UserDetails 重写的 isEnabled 冲突
private Boolean enabled;
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "密码")
private String password;
@ApiModelProperty(value = "用户头像")
private String userFace;
@ApiModelProperty(value = "备注")
private String remark;
@ApiModelProperty(value = "角色")
@TableField(exist = false)
private List<Role> roles;
/**
* 获取权限列表
* role.getName() 权限名字
* 角色里面对应的名字,把它转成 SimpleGrantedAuthority 返回
* @return
*/
@Override
@JsonDeserialize(using = CustomAuthorityDeserialize.class)
public Collection<? extends GrantedAuthority> getAuthorities() {
List<SimpleGrantedAuthority> authorities = roles
.stream()
.map(role -> new SimpleGrantedAuthority(role.getName()))
.collect(Collectors.toList());
return authorities;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return this.enabled;
}
}