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>
相关推荐
阿达和自己4 小时前
springboot学习-springboot引入MyBatis-Plus
spring boot·学习·mybatis
2603_9696948518 小时前
【JavaWeb学习记录】MyBatis核心知识点与实战笔记:从入门到动态SQL
笔记·sql·学习·mysql·mybatis·javaweb·学习方法
wang_shu_mo_ran20 小时前
MyBatis 从入门到进阶(一)
java·spring·mybatis
瑞雪兆丰年兮1 天前
JavaWeb-day14-SpringBootWeb原理篇
java·开发语言·spring boot·spring·bean·mybatis·javaweb
bksczm4 天前
Redis 持久化:RDB 快照与 AOF 日志
数据库·redis·mybatis
CoderYanger5 天前
Java EE 进阶:8.MyBatis 操作数据库(入门)(第二弹)
java·数据库·spring boot·spring·面试·java-ee·mybatis
CoderYanger8 天前
Java EE 进阶:8.MyBatis 操作数据库(入门)(第一弹)
java·数据库·spring·面试·职场和发展·java-ee·mybatis
真上帝的左手10 天前
30. 技术专题-‌应用框架组件-关系型数据库-MySQL-Mybatis
数据库·分布式·mysql·中间件·mybatis
雪芽蓝域zzs10 天前
第 2 章:整合 MySQL + MyBatis,搭建数据库基础、实体类、Mapper、测试查询
数据库·mysql·mybatis
wljt11 天前
公司二-面试题
java·开发语言·mybatis