MyBatis复杂映射开发之多对多查询

Terwer 后端开发 MyBatis评论211字数 2820阅读9分24秒阅读模式
摘要

本文介绍了多对多关系模型中用户表和角色表的关系,以及如何通过查询语句查询所有用户及其对应的角色。通过修改实体类和接口方法,并配置对应的XML文件,展示了查询所有用户及其角色的示例。

多对多查询的模型

用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用。文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

多对多查询的需求:查询所有用户的同时查询出该用户对应的所有角色。文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

@startuml文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

!theme plain
top to bottom direction
skinparam linetype ortho文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

class sys_role {
rolename: varchar(255)
roleDesc: varchar(255)
id: int(11)
}
class sys_user_role {
userid: int(11)
roleid: int(11)
}
class node2 as "user /* 用户表 */" {
username: varchar(50)
password: varchar(50)
birthday: varchar(50)
id: int(11)
}文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

sys_user_role ^-[#595959,plain]- sys_role : "roleid:id"
sys_user_role ^-[#595959,plain]- node2 : "userid:id"
@enduml 文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

如果图片无法查看,请看这里文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

MyBatis复杂映射开发之多对多查询文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

多对多查询的语句

对应的 sql 语句:文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

select u.id,u.username,r.id,r.rolename from user u left join sys_user_role ur on u.id=ur.userid
inner join sys_role r on r.id= ur.roleid

查询结果如下:文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

id username id rolename
1 lucy 1 CTO
2 tom 1 CTO
1 lucy 2 CEO
2 tom 2 CEO

创建 Role 实体,修改 User 实体

User文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

public class User {
    private Integer id;
    private String username;

    // 代表当前用户具备那些订单
    private List<Order> orderList;

    // 代表当前用户具备的那些角色
    private List<Role> roleList;

    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;
    }

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", orderList=" + orderList +
                ", roleList=" + roleList +
                '}';
    }
}

Role文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

public class Role {
    private Integer id;
    private String rolename;
  
  @Override
  public String toString() {
      return "Role{" +
              "id=" + id +
              ", rolename='" + rolename + '\'' +
              '}';
  }
}

UserMapper 添加接口方法

/**
 * 查询所有用户以及对应的角色
 * @return
 */
public List<User> findAllUserAndRole();

配置 UserMapper.xml

<resultMap id="userRoleMap" type="com.terwergreen.pojo.User">
    <result property="id" column="id"></result>
    <result property="username" column="username"></result>

    <collection property="roleList" ofType="com.terwergreen.pojo.Role">
        <result column="rid" property="id"></result>
        <result column="rolename" property="rolename"></result>
    </collection>
</resultMap>

<select id="findAllUserAndRole" resultMap="userRoleMap">
    select u.id,u.username,r.id,r.rolename from user u left join sys_user_role ur on u.id=ur.userid
    inner join sys_role r on r.id=ur.roleid
</select>

测试结果

==>  Preparing: select u.id,u.username,r.id as rid,r.rolename from user u left join sys_user_role ur on u.id=ur.userid inner join sys_role r on r.id=ur.roleid
==> Parameters: 
<==    Columns: id, username, rid, rolename
<==        Row: 1, lucy, 1, CTO
<==        Row: 2, tom, 1, CTO
<==        Row: 1, lucy, 2, CEO
<==        Row: 2, tom, 2, CEO
<==      Total: 4
User{id=1, username='lucy', orderList=null, roleList=[Role{id=1, rolename='CTO'}, Role{id=2, rolename='CEO'}]}
User{id=2, username='tom', orderList=null, roleList=[Role{id=1, rolename='CTO'}, Role{id=2, rolename='CEO'}]}

知识总结

MyBatis 多表配置方式:文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

一对一配置:使用做配置文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

一对多配置:使用 + 做配置文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

多对多配置:使用 + 做配置文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

本文代码地址

mybatis-multitable文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

文章更新历史文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

2024/05/13 同步文章到其他平台文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

2022/05/04 feat:初稿 文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

本文介绍了多对多关系模型中用户表和角色表的关系,以及如何通过查询语句查询所有用户及其对应的角色。通过修改实体类和接口方法,并配置对应的XML文件,展示了查询所有用户及其角色的示例。文章源自浅海拾贝-https://blog.terwergreen.com/mybatis-complex-mapping-development-of-many-to-many-queries.html

相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: