mybatis用户角色权限一对多关联查询

mybatis一对多关联查询案例

  • 1.需求
  • 2.数据库说明
  • 3.实体说明
  • 4.mybatis SQL语句编写

一.需求
在做角色权限管理时,需要根据用户名查询其对应的所有角色以及拥有的所有权限


二.数据库说明
数据库有如下几个表

用户表(users)
id,用户名,真是姓名,密码,性别,出生日期,描述,创建日期
角色表(roles)
id,角色描述
权限表(permissions)
id,权限名称,权限资源

除了这三张表外,还要两张表来将三者关联起来:

用户角色关联表(users_roles)
用户id、角色id
角色权限关联表(roles_permissions)
用户角色关联表(users_roles)

这里写图片描述

三.实体说明

1.用户实体说明
用户与角色为多对多的关系,则用户实体中应该包含多个角色,此处用list封装
public class Users {
    private Integer id;
    private String username;
    private String name;
    private List<Educate> educates; 
    private String password;
    private Byte sex;
    @DateTimeFormat(pattern = "yyyy-MM-dd")//前段到后台注解
    private Date birthday;
    private Date createtime;
    private String content;
    private List<Roles> rolesList;
    public List<Educate> getEducates() {
        return educates;
    }

    public void setEducates(List<Educate> educates) {
        this.educates = educates;
    }
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Byte getSex() {
        return sex;
    }

    public void setSex(Byte sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content == null ? null : content.trim();
    }

    public List<Roles> getRolesList() {
        return rolesList;
    }

    public void setRolesList(List<Roles> rolesList) {
        this.rolesList = rolesList;
    }


    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Users [id=" + id + ", username=" + username + ", name=" + name + ", educates=" + educates
                + ", password=" + password + ", sex=" + sex + ", birthday=" + birthday + ", createtime=" + createtime
                + ", content=" + content + ", rolesList=" + rolesList + "]";
    }
}
2.角色实体说明
角色与权限为多对多的关系,即一个角色对应多个权限,此处用list属性进行权限封装
public class Roles {
    private Integer id;

    private String description;

    private List<Permissions> permissionsList;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description == null ? null : description.trim();
    }

    public List<Permissions> getPermissionsList() {
        return permissionsList;
    }

    public void setPermissionsList(List<Permissions> permissionsList) {
        this.permissionsList = permissionsList;
    }

    @Override
    public String toString() {
        return "Roles [id=" + id + ", description=" + description + ", permissionsList=" + permissionsList + "]";
    }
}

3.权限实体说明

public class Permissions {
    private Integer id;

    private String url;

    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url == null ? null : url.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    @Override
    public String toString() {
        return "Permissions [id=" + id + ", url=" + url + ", name=" + name + "]";
    }

}

  • 四.mybatis SQL语句编写

在mapper映射文件中,编写根据用户名查找用户的方法(findUsersByName),将用户对应的角色以及拥有的权限封装

<resultMap id="users" type="Users">
    <result property="id" column="id" />
    <result property="username" column="username" jdbcType="VARCHAR"/>
    <result property="name" column="name" jdbcType="VARCHAR"/>

    <result property="password" column="password" jdbcType="VARCHAR"/>
    <result property="sex" column="sex" jdbcType="VARCHAR"/>
    <result property="birthday" column="birthday" jdbcType="TIMESTAMP"/>
    <result property="createtime" column="createtime" jdbcType="TIMESTAMP" />
  <!--  <result property="isadmin" column="isadmin" jdbcType="VARCHAR"/>-->
    <result property="content" column="content" jdbcType="VARCHAR"/>
</resultMap>




<resultMap type="com.jointem.hrm.entity.Roles" id="RoleMap">
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="description" property="description" jdbcType="VARCHAR" />
    <collection property="permissionsList" column="id" javaType="list" select="selectAllpermissions"></collection>
</resultMap>

<select id="selectAllpermissions" resultType="com.jointem.hrm.entity.Permissions" parameterType="java.lang.Integer">
    select id, url,name  from permissions where id in (select roles_permissions.permission_id from roles_permissions where roles_permissions.role_id=#{id})
</select>

<select id="selectAllRoles" resultMap="RoleMap" parameterType="java.lang.Integer">
    select id, description from roles where id in (select users_roles.role_id from users_roles where users_roles.user_id=#{id})
</select>

<select id="findUsersByName" parameterType="String" resultMap="usersMap">
    select id,username,password,sex,birthday,createtime,content from        users where username=#{username}
</select>
<!-- 关联查询角色集合与权限集合 -->

“`
总结
1.关联查询首先要建立对应的需求实体
2.了解resultmap属性

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
MyBatis-Plus是一款MyBatis的增强工具,它提供了很多实用的功能,比如一对多关联查询。在MyBatis-Plus中,一对多关联查询可以通过使用@TableName注解和@TableField注解来实现。 假设我们有两张表,一张是学生表,另一张是课程表,一个学生可以选多门课程,那么我们就可以用一对多关联查询查询某个学生选的所有课程。 首先,在学生表中定义一个属性List<Course> courses,并使用@TableField注解将该属性与课程表的外键关联起来: ``` public class Student { @TableId private Long id; private String name; @TableField(exist = false) private List<Course> courses; } ``` 然后,在课程表中定义一个属性Long studentId,并使用@TableField注解将该属性与学生表的主键关联起来: ``` public class Course { @TableId private Long id; private String name; @TableField("student_id") private Long studentId; } ``` 最后,我们使用MyBatis-Plus提供的wrapper类进行关联查询: ``` QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", studentId); List<Student> students = studentMapper.selectList(queryWrapper); for (Student student : students) { QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>(); courseQueryWrapper.eq("student_id", student.getId()); List<Course> courses = courseMapper.selectList(courseQueryWrapper); student.setCourses(courses); } ``` 以上就是MyBatis-Plus实现一对多关联查询的方法。如果您还有其他问题或需要进一步的帮助,请随提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值