1. 关联-association
2. 集合-collection

比如同时有User.java和Card.java两个类

User.java如下:

public class User{

private Card card_one;

private List<Card> card_many;

}

在映射card_one属性时用association标签, 映射card_many时用collection标签.

所以association是用于一对一和多对一,而collection是用于一对多的关系

下面就用一些例子解释下吧

association-一对一

人和身份证的关系

下面是pojo

 
1
2
3
4
5
public class Card implements Serializable{
private Integer id;
private String code;
//省略set和get方法.
}
 
1
2
3
4
5
6
7
8
9
public class Person implements Serializable{
private Integer id;
private String name;
private String sex;
private Integer age;
//人和身份证是一对一的关系
private Card card;
//省略set/get方法.
}

下面是mapper和实现的接口

 
1
2
3
4
5
6
7
package com.glj.mapper;
 
import com.glj.poji.Card;
 
public interface CardMapper {
Card selectCardById(Integer id);
}
 
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.glj.mapper.CardMapper">
<select id="selectCardById" parameterType="int" resultType="com.glj.poji.Card">
select * from tb_card where id = #{id}
</select>
</mapper>
 
1
2
3
4
5
6
7
package com.glj.mapper;
 
import com.glj.poji.Person;
 
public interface PersonMapper {
Person selectPersonById(Integer id);
}
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.glj.mapper.PersonMapper">
<resultMap type="com.glj.poji.Person" id="personMapper">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<association property="card" column="card_id"
select="com.glj.mapper.CardMapper.selectCardById"
javaType="com.glj.poji.Card">
</association>
</resultMap>
<select id="selectPersonById" parameterType="int" resultMap="personMapper">
select * from tb_person where id = #{id}
</select>
</mapper>

PersonMapper.xml 还使用association的分步查询。

同理多对一,也是一样

只要那个pojo出现private Card card_one;

即使用association


collection 一对多和association的多对一关系

学生和班级的一对多的例子

pojo类

 
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.glj.pojo;
 
import java.io.Serializable;
import java.util.List;
 
public class Clazz implements Serializable{
private Integer id;
private String code;
private String name;
        //班级与学生是一对多的关系
private List<Student> students;
//省略set/get方法
}
 
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.glj.pojo;
 
import java.io.Serializable;
 
public class Student implements Serializable {
private Integer id;
private String name;
private String sex;
private Integer age;
        //学生与班级是多对一的关系
private Clazz clazz;
//省略set/get方法
}
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.glj.mapper.ClazzMapper">
<select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">
select * from tb_clazz where id = #{id}
</select>
<resultMap type="com.glj.pojo.Clazz" id="clazzResultMap">
<id property="id" column="id"/>
<result property="code" column="code"/>
<result property="name" column="name"/>
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="students" ofType="com.glj.pojo.Student"
column="id" javaType="ArrayList"
fetchType="lazy" select="com.glj.mapper.StudentMapper.selectStudentByClazzId">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
</collection>
</resultMap>
</mapper>
 
1
2
3
4
5
6
7
package com.glj.mapper;
 
import com.glj.pojo.Clazz;
 
public interface ClazzMapper {
Clazz selectClazzById(Integer id);
}

ClazzMapper使用到了集合-collection 即为一对多,一个班级面对多个学生

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.glj.mapper.StudentMapper">
<select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
select * from tb_clazz c,tb_student s where c.id = s.id and s.id = #{id}
</select>
<select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap">
select * from tb_student where clazz_id = #{id}
</select>
<resultMap type="com.glj.pojo.Student" id="studentResultMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<association property="clazz" javaType="com.glj.pojo.Clazz">
<id property="id" column="id"/>
<result property="code" column="code"/>
<result property="name" column="name"/>
</association>
</resultMap>
</mapper>
 
1
2
3
4
5
6
7
package com.glj.mapper;
 
import com.glj.pojo.Student;
 
public interface StudentMapper {
Student selectStudentById(Integer id);
}

StudentMapper则是与班级为多对一关系,所以使用了关联-association


嗯,希望我以后又不记得二者的关系时,能感谢现在总结的自己

附上一张mybatis的类型别名图

Mybatis中collection和association的使用区别的更多相关文章

  1. mybatis中collection和association的作用以及用法

    deptDaoMapper.xml 部门对应员工(1对多的关系) <resultMap type="com.hw.entity.Dept" id="deptinfo ...

  2. Mybatis中 collection 和 association 的区别

    public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...

  3. Mybatis中 collection 和 association 的区别?

    public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...

  4. Mybatis中collection与association的区别

    association是多对一的关系 collection是一个一对多的关系

  5. myBatis中 collection 或 association 联合查询 中column 传入多个参数值

    下面是一个树形结构表自连接 联合查询 Demo <resultMap id="BaseResultMap"  type="com.maidan.daas.entit ...

  6. Mybatis之collection与association标签

    collection与association标签的功能就是为了解决查询条件映射到一个类或一个集合上,适用于对于多对一,一对多的映射结果,现在我们就探究其具体使用吧. 环境搭建: 数据库搭建 CREAT ...

  7. mybatis中collection association优化使用及多参数传递

    mybatis都会用,但要优雅的用就不是那么容易了 今天就简单举例,抛砖引玉,供大家探讨 1.主表 CREATE TABLE `test_one` ( `id` int(11) NOT NULL AU ...

  8. MyBatis中collection (一对一,一对多)

    MyBatis学习:http://www.mybatis.org/mybatis-3/zh/index.html 大对象InsuranceDetailsVO: com.quicksure.mobile ...

  9. mybatis中collection子查询注入参数为null

    具体实现参照网上,但是可能遇到注入参数为null的情况,经过查阅及自己测试记录一下: 子查询的参数中,有<if test="">之类,需要指定别名,通过 http:// ...

随机推荐

  1. H264--3--NAL层的处理[6]

    ------------------------------H.264的NAL层处理 ------------------------------ H264以NALU(NAL unit)为单位来支持编 ...

  2. RDA CoreDump 实例

    UMF进程的Coredump问题追踪: 通河code开机DUMP问题 现象: 开机Dump,原因:_MAINAPP_SW_Init()调用了Factory_Ver_Debug()内存溢出. 分析流程: ...

  3. 内核的ramdisk

    ramdisk 内核中的特性之一,使用缓冲和缓存来加速对磁盘上的文件访问,并加载相应的硬件驱. ramdisk --> ramfs,提高速度 CentOS 5: initrd 工具程序:mkin ...

  4. 为什么JavaWeb项目要分层

    首先让我们坐着时光机回到n年前的web开发.那个时候最早都是静态的html页面,后来有了数据库,有了所谓的动态页面,然后程序猿在编码的时候,会把所有的代码都写在页面上,包括数据库连接,包括事务控制,接 ...

  5. Linux安装PHP环境

    简介: PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使用广泛,主要 ...

  6. qW3xT.2挖矿病毒处理方案

    我遇到的是一款qW3xT.2的病毒,网上查了一下,好像是挖矿病毒.在此贴一下我找到的关于病毒的资料.这是我的服务器 这篇文章可谓是出自高手之笔,感觉说的很厉害,但是非专业人士的我有点看不懂,看个大概  ...

  7. Linq 内连接和外连接(转载)

    一.内连接 Model1Container model = new Model1Container(); //内连接 var query = from s in model.Student join ...

  8. 页面置换算法-LRU(Least Recently Used)c++实现

    最近最久未使用(LRU)置换算法 #include <iostream> #include <cstdio> #include <cstring> #include ...

  9. Java 8 (7) 重构、测试和调试

    为改善可读性和灵活性重构代码 看到这里我们已经可以使用lambda和stream API来使代码更简洁,用在新项目上.但大多数并不是全新的项目,而是对现有代码的重构,让它变的更简洁可读,更灵活. 改善 ...

  10. 我要上google

    我要上google 一.下载google浏览器(百度下载) 二.获取和运行xx-net 1.https://github.com/XX-net/XX-Net 2.解压下载的xx-net,运行文件夹中的 ...