比如同时有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


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

collection和association的区别于关系的更多相关文章

  1. Mybatis中 collection 和 association 的区别

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

  2. Mybatis中 collection 和 association 的区别?

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

  3. Mybatis中collection与association的区别

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

  4. mybatis collection和association使用区别

    1. association-关联,用于一对一(如人与身份证)和多对一(如班级和学生) 2. collection-集合,用于一对多(如学生和班级)的关系

  5. 今天第一天开通博客,随笔总结一下resultType(属性)和resultMap,collection和association,Statement和PreparedStatement各自的区别

    1.resultType(属性)和resultMap(标签引用)的区别? resultType不支持自定义返回结果,会将查询到的结果通过到type中java对象的同名的属性,对象中的属性名必须和数据库 ...

  6. Mybatis中collection和association的使用区别

    1. 关联-association2. 集合-collection 比如同时有User.java和Card.java两个类 User.java如下: public class User{ privat ...

  7. Mybatis中的collection和association一关系

    collection 一对多和association的多对一关系 学生和班级的一对多的例子 班级类: package com.glj.pojo; import java.io.Serializable ...

  8. mybatis collection解析以及和association的区别

    1.collection标签 说到mybatis的collection标签,我们肯定不陌生,可以通过它解决一对多的映射问题,举个例子一个用户对应多个系统权限,通过对用户表和权限表的关联查询我们可以得到 ...

  9. 类图和对象图教程-类(Class)、接口(Interface)、协作(collaboration)、依赖关系(Dependency)、泛化关系(Generalization)、关联关系(Association)以及实现关系(Realization)

    类图的概念 (转) 一.概述 类图(Class Diagram)是描述类.接口.协作以及它们之间关系的图,用来显示系统中各个类的静态结构.类图是定义其他图的基础,在类图基础上,可以使用状态图.协作图. ...

随机推荐

  1. java8 stream lambda 一个例子

    import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Pa ...

  2. JS-构造函数模式代码实战和总结-极客

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 设计模式二之观察者模式(Subject-Observer)

    观察者模式定义了一系列对象之间的一对多关系,当一个主题对象改变状态,其他所有的依赖者都会收到通知. 好了,你可能会觉得上面的描述略微复杂,较难理解,那么现在我们将用一个简单的例子去讲解这个模式. 我们 ...

  4. pypyodbc 的坑

    1, 你要先确认你自己的office的版本, 你要安装access dabase engine. 但是 sb 微软的驱动 32位的不让装, 64位的也不让装, 吐槽微软100次---MS个大SX. 最 ...

  5. 安装mavlink遇到的问题(future找不到)

    从官网下载mavlink(git clone https://github.com/mavlink/mavlink.git) 然后进入mavlink 目录执行 git submodule update ...

  6. [python3.6+opencv] 01 完成读取图片操作

    学习一下opencv3 奈何vs2017配的云里雾里,还是使用python吧 --人生苦短,python来凑 --使用Pycharm操作,面向界面的Pycharm好一些吧(我猜的) 新建Project ...

  7. vimrc配置

    "=========================================================================" DesCRiption 适合 ...

  8. Java中返回类型方法名

    继承父类,子类含有两个分别为boy.Girl类名. 返回是需要返回方法 则返回变量名Person class Person { void eat() {} void speak() {} } clas ...

  9. Linux批量复制文件到文件夹

    echo dirname* | xargs -n 1 cp -v filename 把当前目录下 filename文件拷贝到以dirname开头的不同文件夹里.

  10. Excel 导入时如何下载模板信息(Java)

    大家知道,我们在实现 Excel 上传的时候,会让我们去下载个模板,然后实现导入功能.在此我在这里记录下来,以便后续的使用... 首先思考一个问题是 这个模板这么给前台,还有这个模板是这么来的,刚开始 ...