mybatis的association以及collection的用法

  • 前言:
  • 一、association 的三种用法:
    • 第一种用法:association中使用select
    • 第二种方法,嵌套 resultMap
    • 第三种方法:嵌套 resultMap 简化版
  • 二、collection的三种用法:
    • 1、第一种方法:用select,跟association 中使用select类似:
    • 第二种方法:嵌套resultMap
    • 3、第三种方法:嵌套resultMap简化版
  • 三、标签属性介绍
  • 四、特别注意:

前言:

在项目中,某些实体类之间肯定有关联关系,比如一对一,一对多等。mybatis 中使用 association 和 collection 。
association:一对一关联(has one)
collection :一对多关联(has many)

注意:
只有做查询时才会涉及到联表,使用其完成联表查询 。两个标签都可以总结为三种方式。

一、association 的三种用法:

先来看看如下代码(set,get方法没有列出)

public class User {private Integer userId;private String userName;private Integer age;private Card card;//一个人一张身份证,1对1
}
public class Card {private Integer cardId;private String cardNum;//身份证号private String address;//地址
}
public interface UserMapper {/*** 通过userId查询user信息* @param userId* @return*/User queryById(int userId);
}
 <select id="queryById" parameterType="int" resultMap="userMap">SELECT u.user_name,u.age,c.card_id,c.card_num,c.addressFROM tb_user u,tb_card cWHERE u.card_id=c.card_idANDu.user_id=#{userId}</select>

以上是实体类,dao层的设计以及在UserMapper.xml中 queryById 方法的 sql 语句的编写,因为不论用 association的哪种方式,sql语句都是一样的写,sql 语句都是一样的写,不同的只是 userMap 的写法,所以这里先给出这段代码。User 和 Card 是一对一的关系,在数据库中,t_user 表通过外键 card_id 关联 t_card 表。下面分别用 association 的三种用法来实现 queryById 方法。

第一种用法:association中使用select

这种方法需要再定义 CardMapper.java如下:

public interface CardMapper {Card queryCardById(int cardId);
}

在 CardMapper.xml 中实现该方法:

 <select id="queryCardById" parameterType="int" resultType="com.ck.smm.entity.Card">SELECT *FROM tb_cardWHERE card_id=#{cardId}</select>

然后再看 UserMapper.xml 是如何引用这个方法的:

<resultMap type="User" id="userMap"><result property="userName" column="user_name"/><result property="age" column="age"/><association property="card" column="card_id" select="com.ck.ssm.dao.CardMapper.queryCardById"></association>
</resultMap>

在这里直接通过 select 引用 CardMapper 的 queryById 方法。个人感觉这种方法比较麻烦,因为还要在 CardMapper 里定义 queryCardById 方法并且实现再引用才有用,不过这种方法思路清晰,易于理解。

第二种方法,嵌套 resultMap

<resultMap type="com.ck.smm.entity.Card" id="cardMap"><id property="cardId" column="card_id"/><result property="cardNum"  column="card_num"/><result property="address" column="address"/>
</resultMap><resultMap type="User" id="userMap"><result property="userName" column="user_name"/><result property="age" column="age"/><association property="card" resultMap="cardMap"></association>
</resultMap>

第二种方法就是在 UserMapper.xml 中先定义一个 Card 的 resultMap,然后在 User 的r esultMap 的 association 标签中通过 resultMap=“cardMap” 引用。这种方法相比于第一种方法较为简单。

第三种方法:嵌套 resultMap 简化版

<resultMap type="com.ck.smm.entity.User" id="userMap"><result property="userName" column="user_name"/><result property="age" column="age"/><association property="card" column="card_id" javaType="com.ck.smm.entity.Card"><id property="cardId" column="card_id"/><result property="cardNum" column="card_num"/><result property="address" column="address"/></association>
</resultMap>

这种方法就把 Card 的 resultMap 定义在了 association 标签里面,通过 javaType 来指定是哪个类的 resultMap,个人认为这种方法最简单,缺点就是 cardMap 不能复用。具体用哪种方法,视情况而定。

二、collection的三种用法:

一个土豪有多个手机,看如下代码:
User实体类

public class User{private Integer userId;private String userName;private Integer age;private List<MobilePhone> mobilePhone;//土豪,多个手机,1对多
}

手机类

public class MobilePhone {private Integer mobilePhoneId;private String brand;//品牌private double price;//价格private User user;//主人
}

dao层

public interface UserMapper {/*** 通过userId查询user信息* @param userId* @return*/User queryById(int userId);
}

UserMapper.xml 中的 select 查询语句

<select id="queryById" parameterType="int" resultMap="userMap">SELECT u.user_name,u.age,m.brand,m.priceFROM tb_user u,tb_mobile_phone mWHERE m.user_id=u.user_idANDu.user_id=#{userId}
</select>

数据库中,tb_mobile_phone 中 user_id 作为外键。那么下面来看 resultMap 如何定义:

1、第一种方法:用select,跟association 中使用select类似:

先定义 MobilePhoneMapper.java

public interface MobilePhoneMapper{List<MobilePhone> queryMbByUserId(int userId);
}

然后实现该方法 MobilePhoneMapper.xml

<resultMap type="com.ck.smm.entity.MobilePhone" id="mobilePhoneMap"><id property="mobilePhoneId" column="user_id"/><result property="brand" column="brand"/><result property="price" column="price"/><association property="user" column="user_id" select="com.ck.ssm.dao.UserMapper.queryById"></association>
</resultMap>
<select id="queryMbByUserId" parameterType="int" resultMap="mobilePhoneMap">SELECT brand,priceFROM tb_mobile_phoneWHERE user_id=#{userId}</select>

做好以上准备工作,那就可以在 UserMapper.xml 中引用了

<resultMap type="com.ck.smm.entity.User" id="userMap"><id property="userId" column="user_id"/><result property="userName" column="user_name"/><result property="age" column="age"/><collection property="mobilePhone" column="user_id" select="com.ck.ssm.dao.MobilePhoneMapper.queryMbByUserId"></collection>
</resultMap>

这种方法和 association 的 第一种用法几乎是一样的不同之处就是 mobilePhMap 中用到了association ,queryMbByUserId 中要使用mobilePhoneMap,而不能直接使用 resultType。

第二种方法:嵌套resultMap

<resultMap type="com.ck.smm.entity.MobilePhone" id="mobilephoneMap"><id column="mobile_phone_id" property="mobilePhoneId"/><result column="brand" property="brand" /><result column="price" property="price" />
</resultMap>
<resultMap type="com.ck.smm.entity.User" id="userMap"><result property="userName" column="user_name"/><result property="age" column="age"/><collection property="mobilePhone" resultMap="mobilephoneMap" ></collection>
</resultMap>

定义好这两个 resultMap ,再引用 UserMap 就行了。

3、第三种方法:嵌套resultMap简化版

<resultMap type="com.ck.smm.entity.User" id="userMap"><result property="userName" column="user_name"/><result property="age" column="age"/><collection property="mobilePhone"column="user_id" ofType="com.ck.smm.entity.MobilePhone"><id column="mobile_phone_id" property="mobilePhoneId" /><result column="brand" property="brand" /><result column="price" property="price" /></collection>
</resultMap>

这种方法需要注意,一定要有 ofType,collection 装的元素类型是啥 ofType 的值就是啥,这个一定不能少。

三、标签属性介绍

  • id:命名空间中的唯一标识符,可以被用来引用这条语句

  • parameterType:表示查询语句传入参数的类型的完全限定名或别名,支持基础数 据类型和复杂数据类型。

  • resultType:查询语句返回结果类型的完全限定名或别名。

  • property : 对象属性的名称

  • javaType :对象属性的类型

  • column :所对应的外键字段名称

  • select :使用另一个查询封装的结果

  • ofType :collection中的属性, 用于指定集合中元素的对象类型。

四、特别注意:

表中主键字段要有所区分,不能都写成id,要写成 user_id、card_id,反正要有所区分,不然查询的时候会查不到完整的数据。

参考:https://www.jianshu.com/p/018c0f083501

mybatis联表查询的几种方式,association和collection的用法相关推荐

  1. 使用Mybatis联表查询的几种方式

    mybatis的association以及collection的用法 前言: 在项目中,某些实体类之间肯定有关联关系,比如一对一,一对多等.mybatis 中使用 association 和 coll ...

  2. SQL 联表查询的三种方式:左连接、右连接、内连接、默认连接

    数据库表: blog表: user表: 左连接: 以左表为主表,查询出满足条件的内容.查询到的内容是左表全部的内容,和右表满足要求的内容.可能会出行右表内容为为空的可能. select b.id,b. ...

  3. MyBatis实现模糊查询的三种方式

    MyBatis实现模糊查询的三种方式 准备 模糊查询 方式一 方式二 方式三 由于#{}是占位符本身,自带单引号,所以在模糊查询时需要一些技巧. 准备 数据库表 bean 模糊查询 以查询出所有用户名 ...

  4. mysql多表查询有几种方法_多表查询有几种方式

    多表查询有3种方式,分别是:1.传统方式,包括左外连接查询,右外连接查询 ,完全外链接查询:2.子查询方式,包括单行查询,多行查询:3.聚合查询方式,包括求和,平均查询,记录总数. 多表查询有3种方式 ...

  5. mybatis联表查询且统计数据

    mybatis联表查询且统计数据 表1的数据+表2的数据的统计==>vo接收 前端原型图如下: 涉及到两张表 t_record t_user_record 详情A/B A:未签收人数 B:全部人 ...

  6. mybatis一对多查询的两种方式

    user 和order 对象 package com.itheima.mybatis.pojo;import java.io.Serializable; import java.util.Date;p ...

  7. SpringBoot+MyBatis 基于xml实现多表查询的两种方式

    1.创建SpringBoot项目,导入依赖 <dependencies><dependency><groupId>org.springframework.boot& ...

  8. mybatis一对一联表查询的两种常见方式

    1.一条语句执行查询(代码如下图)  注释:class表(c别名),teacher表(t别名)teacher_id为class表的字段t_id为teacher表的字段,因为两者有主键关联的原因,c_i ...

  9. Mybatis联表查询:多对多(注解实现)

    1.数据库表结构 2.返回结果类封装 CommentWithTag .java @Getter @Setter @ToString @Builder @AllArgsConstructor @NoAr ...

最新文章

  1. 使用深度神经网络进行自动呼叫评分(一)
  2. 【6.2】hibernate_component
  3. 45 张图深度解析 Netty 架构与原理
  4. html5如何绘制饼图,如何在HTML5中创建“饼图”?
  5. 互联网晚报 | 04月07日 星期四 |​ ​​​​刘强东卸任京东集团CEO,徐雷接任;世卫组织:中医药对治疗新冠有效...
  6. Chromium Embedded Framework中文文档之(基本使用)
  7. web面试 new操作符到底干了什么?
  8. 自动化测试中自动化切换网络----解决方案
  9. 因需要**云音乐歌单转到apple music,不满**云音乐下载都是ncm格式,所以想办法转化格式
  10. 惠普hp暗影精灵2 Hackintosh黑苹果安装过程
  11. 第八届泰迪杯数据挖掘赛C题总结
  12. 晨魅--高拍仪二次开发
  13. mysql全称量词_MySQL操作记录的方法集合,供以后查看
  14. 数据可视化工具-Vue-DataV入门
  15. win7 定时开关机命令
  16. Center Loss层
  17. 名企笔试:腾讯2016招聘笔试(微信红包)
  18. oracle分段时间统计总数,oracle中根据年份统计每月的总数?解决思路
  19. 安装android+手机usb+驱动程序,一加手机驱动怎么安装 一加手机USB驱动手动安装详细教程图解...
  20. 【Mybatis】Mybatis学习由浅入深(二)

热门文章

  1. wince 访问共享文件_WINCE6.0建立共享文件夹
  2. NCv5.0 与 IBM公司WAS中间件
  3. Win10 这台计算机中已经安装了 .NET Framework 4.5.2 或版本更高的更新
  4. a a c语言表达式是,c语言中,已知a=12,则表达式a+=a-=a*=a的结果是什么,求步骤
  5. 头脑风暴--原生JS实现汉诺塔游戏
  6. TUTK[Kalay][iOS]对接iOS TPNS推送流程
  7. 如果我们真的发现了外星人?
  8. 【5G通信网络架构与5G基站架构概述】
  9. 基于 Java8 的国产开源 IoT 企业级物联网平台
  10. nCode:DesignLife案例教程二十