在项目中,某些实体类之间肯定有关键关系,比如一对一,一对多等。mybatis 中就用associationcollection

注意,只有在做select查询时才会用到这两个标签,都有三种用法,且用法类似。

一、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 UserDao {/*** 通过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层的设计以及在UserDao.xml中queryById方法的sql语句的编写,因为不论用association的哪种方式,sql语句都是一样的写,不同的只是userMap的写法,所以这里先给出这段代码。User询Card是一对一关系,在数据库中,tb_user表通过外键card_id关联tb_card表。下面分别用association的三种用法来实现queryById方法。

1、第一种用法:association中使用select

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

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

在CardDao.xml中实现该方法:

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

然后再看UserDao.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.zhu.ssm.dao.CardDao.queryCardById"></association>
</resultMap>

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

2、第二种方法,嵌套resultMap

<resultMap type="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>

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

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

<resultMap type="User" id="userMap"><result property="userName" column="user_name"/><result property="age" column="age"/><association property="card"column="card_id" javaType="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 UserDao {/*** 通过userId查询user信息* @param userId* @return*/User queryById(int userId);
}

UserDao.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类似:

先定义 MobilePhoneDao.java

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

然后实现该方法 MobilePhoneDao.xml

<resultMap type="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.zhu.ssm.dao.UserDao.queryById"></association>
</resultMap>
<select id="queryMbByUserId" parameterType="int" resultMap="mobilePhoneMap">SELECT brand,priceFROM tb_mobile_phoneWHERE user_id=#{userId}</select>

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

<resultMap type="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.zhu.ssm.dao.MobilePhoneDao.queryMbByUserId"></collection>
</resultMap>

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

2、第二种方法:嵌套resultMap

<resultMap type="MobilePhone" id="mobilephoneMap"><id column="mobile_phone_id" property="mobilePhoneId"/><result column="brand" property="brand" /><result column="price" property="price" /></resultMap><resultMap type="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="User" id="userMap"><result property="userName" column="user_name"/><result property="age" column="age"/><collection property="mobilePhone"column="user_id" ofType="MobilePhone"><id column="mobile_phone_id" property="mobilePhoneId" /><result column="brand" property="brand" /><result column="price"property="price" /></collection>
</resultMap>

这种方法需要注意,一定要有ofType,collection 装的元素类型是啥ofType的值就是啥,这个一定不能少。
注意:
所有resultMap中的type、select 标签中的resultType以及association中的javaType,collection中的ofType,这里只写了类名,是因为在mybatis-config.xml中配置了typeAliases,否则就要写该类的全类名。配置如下:

<typeAliases>
<packagename="com.zhu.smm.entity"/>
</typeAliases>

总结:

1、association表示的是has one的关系,一对一时使用。user has one card,所以在user的resultMap中接收card时应该用association;

2、collection表示的是has many的关系,一对多时使用。user has many mobilePhone,所以在user的resultMap中接收mobilePhone时应该用collection 。

3、都有三种用法,且非常类似,resultMap要复用建议第二种方法,不需要复用建议第三种方法。

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

mybatis的association以及collection的用法相关推荐

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

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

  2. mybatis中association 和collection 的区别

    mybatis中association 和collection 的区别:https://zhidao.baidu.com/question/1240407172484106299.html 两个实体类 ...

  3. mybatis中association和collection的column传入多个参数值

    在使用 association和collection 进行关联查询的时候 column 参数可能会有多个,如下: 注意: parameterType 一定要是 java.util.Map 转载于:ht ...

  4. 关于mybatis的association和collection主键映射问题

    多对一:association 第一种方式 嵌套方式: 基本数据 Mapper接口,因为简写,就没有写另一个mapper了,写在一个里面,如果要写另一个mapper里面,引用的时候,从名称空间来引用 ...

  5. Mybatis之association和collection

    1.单个关联查询association 1.1实体之间的关联表示 package com.worldly.config.entity;import java.io.Serializable;/*** ...

  6. mybatis 配置文件中,collection 和 association 的对应关系

    mybatis 配置文件中,collection 和 association 的对应关系  如下图所示:

  7. 对与association和collection的理解:

    文章目录 前言 一.多对一关系 -- association 二.一对多关系 -- collection 前言 对与association和collection的理解: 我们经常会遇到两组对象一对多或 ...

  8. mybatis解析-association实现原理详解

    本文基于mybatis-spring 1.3.1和mybatis 3.4.4版本 mybatis中的标签association主要用于解决"有一个"类型的关系,它表示一个对象至多有 ...

  9. Mybatis | Mybatis标签association一对一的使用

    Mybatis标签association一对一的使用 一.association 二.使用方法 1. 方法一: 嵌套结果映射 2. 方法二: 嵌套select 查询 三.colleciton 一对多 ...

最新文章

  1. 【项目上线】详细步骤03:一键安装lnmp环境,配置Nginx+Node+MongoDB+MySQL+PHP环境...
  2. Python 2.7终结于7个月后,这是你需要了解的3.X炫酷新特性
  3. Windows 7 搭建 Mobile 6 真机调试开发环境
  4. 深圳网络推广谈论网站收录后的内容还能不能修改?
  5. kohana中的路由规则
  6. Java实战应用50篇(一)-Java并发编程:volatile关键字解析
  7. VS2010/MFC编程入门之二十三(常用控件:按钮控件的编程实例)
  8. JDK 伪异步编程(线程池)
  9. apache   和Tomcat的区别
  10. 【ArcGIS风暴】中国756个气象台站分布Shapefile数据下载
  11. 今日份PS练习|玻璃材质背景练习
  12. 报错及解决:Kernel does not exist: 96b59d42-d81e-471c-8ef9-63a61a963a16
  13. 客运综合管理系统项目—售票管理(售票)
  14. maven使用slf4j输出日志到文件
  15. 4个基本不等式的公式高中_不等式链(高中4个基本不等式链推导)
  16. openssl加密与模拟CA签证和颁发
  17. python爬虫遇到验证码、输入验证码后提醒验证码错误_爬虫遇到头疼的验证码?Python实战讲解弹窗处理和验证码识别...
  18. 钱币兑换问题 HDU 1284
  19. python支持中文
  20. 湖工大计算机网络试卷,安工大计算机网络试卷A

热门文章

  1. 黑盒测试用例设计方法【转】
  2. 风影ASP.NET基础教学 10 DetilsView
  3. A005:查找文件之find, locate, whereis, which, type
  4. dsoframer-在线编辑office文档,一款开源的由微软提供
  5. unity VR实现相机完美旋转
  6. 致大学生——成为博主半年了,谈谈博客对于就业和考研的重要性
  7. Linux基础命令与知识点
  8. 搜索引擎收录查询,是什么影响了网站被搜索引擎收录
  9. 从零搭建符合自己需求的开发环境
  10. 小程序数据怎么传输到服务器,微信小程序怎么将数据传输到Java后台