mybatis的association以及collection的用法

前言:
在项目中,某些实体类之间肯定有关联关系,比如一对一,一对多等。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联表查询的几种方式相关推荐

  1. mybatis联表查询的几种方式,association和collection的用法

    mybatis的association以及collection的用法 前言: 一.association 的三种用法: 第一种用法:association中使用select 第二种方法,嵌套 resu ...

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

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

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

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

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

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

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

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

  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. 20162329 2017-2018-1 《程序设计与数据结构》第十一周学习总结
  2. dataimagepng php_php用header('content-type: image/png')输出验证码,但响应回来的是text/html...
  3. 微软高管谈微软远程办公思考与实践,值得每个企业看看
  4. 安装部署shipyard
  5. 使用Python批量解压缩文件(zip,rar)
  6. 猪齿鱼_01_环境搭建(一)_微服务支撑组件部署(源码形式)
  7. E45: 'readonly' option is set (add ! to override)报错如何解决
  8. 网易 UI 自动化工具 Airtest
  9. 如何用matlab解异或方程,Matlab-6:解非线性方程组newton迭代法
  10. android 前后同时预览_GitHub 上优质项目整理,不只 Android
  11. Pytorch | 学习笔记(二)
  12. 《Cocos Creator游戏实战》你画我猜中的画板功能
  13. SpringBoot Whitelabel Error Page 错误
  14. 银行可视化大屏后端计算案例
  15. 计算机专业笔记本硬盘256G,老笔记本重获新生 东芝256G固态硬盘体验
  16. matlab如何区分乐器,如何区分各种乐器的声音?(弦乐篇)
  17. iPad越狱搭建java环境_ipad怎么自己越狱?这里提供两种方法,友情提示:慎重!...
  18. super-----this
  19. Windows 10 2019 十一月版官方镜像下载
  20. 三相同步电动机的平衡方程式

热门文章

  1. 专业课计算机专业综合,2009年计算机专业考研专业课大纲综合解析
  2. Linux fing cd 查找文件/文件夹并进入目录命令
  3. 观看中国软件市场,免费的WPS说明了什么?
  4. 隐私加密系列|Grin与BEAM之间技术公开对比
  5. spss连接mysql_通过结合使用 SPSS 与数据库仓库连接开展预测性分析
  6. euclidea教程_euclidea全攻略_euclidea几何构建全关卡通关攻略_玩游戏网
  7. 块级元素 div水平居中 垂直居中
  8. 百度网盘快速下载小工具:ENFI下载器、Speedkoala、PanDownload、SpeedPan
  9. Magic Bullet Suite for Mac(红巨人调色插件套装)
  10. 随笔:高清术野相机项目记录