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>
相关推荐
yio_yin19 小时前
MyBatis
java·前端·mybatis
無a伟20 小时前
Redis事务详解:原理、实战、坑点与实践
mybatis
豆角焖肉1 天前
MyBatis延迟加载、缓存机制与注解开发
java·spring·mybatis
就改了2 天前
MyBatis核心类用法详解
java·spring boot·后端·mybatis
momo4 天前
Java+WebAI黑马知识点
java·spring boot·mybatis
snow@li4 天前
MyBatis:动态 SQL 全景梳理
数据库·sql·mybatis
就改了4 天前
Mybatis快速入门大全(详细版)
java·spring boot·mybatis
qq_171538854 天前
深入浅出 MyBatis XML:从入门到精通
xml·java·mybatis
万亿少女的梦1684 天前
基于SSM、JSP与MySQL的秦皇岛旅游服务管理系统设计与实现
mysql·mybatis·ssm·jsp·spring mvc