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>
相关推荐
无风听海11 小时前
深入解析 Elasticsearch 的 match_phrase_prefix与 match_bool_prefix
大数据·elasticsearch·mybatis
摆烂z12 小时前
定时任务同步数据--续跑&重试
java·mybatis·ai编程
Wang's Blog1 天前
Java框架快速入门: Spring Security+OAuth2之实现UserDetails与GrantedAuthority深度定制
java·spring·mybatis
阿维的博客日记3 天前
Example 类全场景实战指南
java·mybatis
阿维的博客日记3 天前
Example API指南2
java·mybatis
霸道流氓气质4 天前
MyBatis 与 SQL 层面关键技术详解
windows·sql·mybatis
₍˄·͈༝·͈˄*₎◞ ̑̑码5 天前
MyBatis操作数据库
数据库·mybatis
干到60岁退休的码农5 天前
13.构建登录接口响应数据
java·spring boot·mybatis
SQL-First布道者6 天前
我为什么把 MyBatis 从项目中删了?
java·spring·tomcat·mybatis·mybatis plus·spring jdbc
爱折腾的编程老炮6 天前
潮玩品牌自建抽盒机小程序——商城 + 抽盒 + 一番赏 + 会员 + 社区,一套代码怎么搭
spring boot·小程序·vue·mybatis·uniapp