SpringBoot 02 shiro框架查询用户权限与角色

发布时间 2023-10-17 16:53:01作者: OYそ

 

 实体类 

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_user")
public class RUser {
    @TableId(value="id",type = IdType.AUTO)
    private Integer usrId;
    private String usrName;
    private  String usrAccount;
    private  String  usrPassword;
    private  String  usrSalt;
    private  String  usrClazz;
@TableField(exist = false)
private Set<Role> roleSet;
}
@AllArgsConstructor
@NoArgsConstructor
@Data
@TableName("t_role")
public class Role {
    @TableId(value="ro_id",type = IdType.AUTO) //告诉主键
    private Integer roId;//默认驼峰命名

    private String roLabel;
    @TableField(exist = false)
private Set<Perm> permSet;
}
@AllArgsConstructor
@NoArgsConstructor
@Data
@TableName("t_perm")
public class Perm {
    @TableId(value="pm_id",type = IdType.AUTO)
    private Integer pmId;
    private String pmLabel;
}

 

Mapper

@Mapper
public interface RUserMapper extends BaseMapper<RUser> {
    List<RUser> selectListByAccount(String usrAccount);
}
@Mapper
public interface RoleMapper extends BaseMapper<Role> {//只有单表查询的方法,多表查询要自己写
    Role selectByLable(String roLabel);
}

Mapper.xml

RUserMapper.xml

<?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.mapper.RUserMapper">
    <select id="selectListByAccount" resultMap="rmUser">
        select * from t_user u,t_user_role ur,t_role r where ur.ur_ro_id=r.ro_id
                                                         and u.usr_id=ur.ur_usr_id
                                                         and usr_account=#{usrAccount}
    </select>
        <resultMap id="rmUser" type="RUser">
        <id property="usrId" column="usr_id"/>
        <result property="usrName" column="usr_name"/>
        <result property="usrAccount" column="usr_account"/>
        <result property="usrSalt" column="usr_salt"/>
        <result property="usrClazz" column="usr_clazz"/>
        <result property="usrPassword" column="usr_password"/>
        <collection property="roleSet" ofType="Role"><!--property:实体类的集合属性名 ofType:集合泛型-->
            <id property="roId" column="ro_id"/>
            <result property="roLabel" column="ro_label"/>
        </collection>
    </resultMap>


</mapper>

RoleMapper.xml

<?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.mapper.RoleMapper">
      <select id="selectByLable" resultMap="labRole">
        select * from t_role r,t_role_perm rp,t_perm p
        where r.ro_id=rp.rp_ro_id
          and rp.rp_pm_id=p.pm_id
          and ro_label in (#{roLabel})
    </select>
    <resultMap id="labRole" type="Role">
        <id property="roId" column="ro_id"/>
        <result property="roLabel" column="ro_label"/>
        <collection property="permSet" ofType="Perm">
            <id property="pmId" column="pm_id"/>
            <result property="pmLabel" column="pm_label"/>
        </collection>
    </resultMap>
</mapper>

 

test