MyBatis多表关联查询(一对多/多对一/多对多)

一、多表设计概述

一对一:可以设计成一张表结构

一对多:一个用户对应多个账户

多对一:多个账户属于同一个用户

多对多:用户与角色之间的关系

二、环境搭建

创建 account 表:

复制代码
CREATE TABLE `account` (
  `ID` int(11) NOT NULL COMMENT '编号',
  `UID` int(11) default NULL COMMENT '用户编号',
  `MONEY` double default NULL COMMENT '金额',
  PRIMARY KEY  (`ID`),
  CONSTRAINT `FK_Reference_8` FOREIGN KEY (`UID`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into `account`(`ID`,`UID`,`MONEY`) values 
(1,1,1000),(2,2,1000),(3,2,2000);

创建 Account 实体类:

复制代码
public class Account implements Serializable {
    private Integer id;
    private Integer uid;
    private Double money;

//自己补充get/set方法,toString方法
}

三、多对一查询(一对一)

需求:查询账户信息时,同时查询出所属用户的信息。

在 Account 类中添加 User 属性:

复制代码
private User user;

<resultMap id="accountMap" type="Account">
    <result property="id" column="id"/>
    <result property="uid" column="uid"/>
    <result property="money" column="money"/>
    <association property="user" javaType="User">
        <result property="username" column="username"/>
        <result property="address" column="address"/>
    </association>
</resultMap>

<select id="findAll" resultMap="accountMap">
    select a.*, u.username, u.address 
    from account a, user u 
    where a.uid = u.id
</select>

四、一对多查询

需求:查询用户信息时,同时查询该用户的所有账户。

在 User 类中添加 List<Account> 属性:

复制代码
private List<Account> accounts;

<resultMap id="userMap" type="User">
    <result property="id" column="id"/>
    <result property="username" column="username"/>
    <collection property="accounts" ofType="Account">
        <result property="money" column="money"/>
    </collection>
</resultMap>

<select id="findOneToMany" resultMap="userMap">
    select u.*, a.money 
    from user u left join account a on u.id = a.uid
</select>

五、多对多查询

创建角色表和用户-角色关联表:

复制代码
CREATE TABLE `role` (
  `ID` int(11) NOT NULL COMMENT '编号',
  `ROLE_NAME` varchar(30) default NULL COMMENT '角色名称',
  `ROLE_DESC` varchar(60) default NULL COMMENT '角色描述',
  PRIMARY KEY  (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `user_role` (
  `UID` int(11) NOT NULL COMMENT '用户编号',
  `RID` int(11) NOT NULL COMMENT '角色编号',
  PRIMARY KEY  (`UID`,`RID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

public class Role implements Serializable {
    private Integer id;
    private String role_name;
    private String role_desc;
    private List<User> users;
    // getter/setter,toString补充上
}

<select id="findAll" resultMap="roleMap">
    SELECT r.*, u.username 
    FROM USER u, user_role ur, role r 
    WHERE u.id = ur.UID AND ur.RID = r.ID
</select>

<resultMap id="roleMap" type="role">
    <id property="id" column="id"/>
    <result property="role_name" column="role_name"/>
    <result property="role_desc" column="role_desc"/>
    <collection property="users" ofType="user">
        <result property="username" column="username"/>
    </collection>
</resultMap>
相关推荐
shuoshuohaohao1 天前
MyBatis 小知识点
java·spring·mybatis
CarIise2 天前
MyBatis动态SQL:从条件拼接到底层原理实战
mybatis
CarIise3 天前
Spring Boot图片展示与统一返回格式:从资源映射到泛型封装实战
mybatis
钝挫力PROGRAMER3 天前
开发踩坑记:MyBatis selectKey、空 SQL
数据库·sql·mybatis
自强的小白3 天前
核心功能(Service接口)
java·mybatis
自强的小白3 天前
mybatis-plus 的扩展功能
mybatis·mybatis_plus
自强的小白3 天前
MYbatis-plus(自定义sql)
java·sql·mybatis
\光辉岁月/4 天前
6.mybatisplus学习-BaseMapper、Service、常用注解
java·mybatis·mybatisplus
\光辉岁月/4 天前
1.mybatis学习-基础
学习·mybatis
代码什么用5 天前
在WEB中的应用
mybatis