collection和association的区别于关系
比如同时有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的区别于关系的更多相关文章
- Mybatis中 collection 和 association 的区别
public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...
- Mybatis中 collection 和 association 的区别?
public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...
- Mybatis中collection与association的区别
association是多对一的关系 collection是一个一对多的关系
- mybatis collection和association使用区别
1. association-关联,用于一对一(如人与身份证)和多对一(如班级和学生) 2. collection-集合,用于一对多(如学生和班级)的关系
- 今天第一天开通博客,随笔总结一下resultType(属性)和resultMap,collection和association,Statement和PreparedStatement各自的区别
1.resultType(属性)和resultMap(标签引用)的区别? resultType不支持自定义返回结果,会将查询到的结果通过到type中java对象的同名的属性,对象中的属性名必须和数据库 ...
- Mybatis中collection和association的使用区别
1. 关联-association2. 集合-collection 比如同时有User.java和Card.java两个类 User.java如下: public class User{ privat ...
- Mybatis中的collection和association一关系
collection 一对多和association的多对一关系 学生和班级的一对多的例子 班级类: package com.glj.pojo; import java.io.Serializable ...
- mybatis collection解析以及和association的区别
1.collection标签 说到mybatis的collection标签,我们肯定不陌生,可以通过它解决一对多的映射问题,举个例子一个用户对应多个系统权限,通过对用户表和权限表的关联查询我们可以得到 ...
- 类图和对象图教程-类(Class)、接口(Interface)、协作(collaboration)、依赖关系(Dependency)、泛化关系(Generalization)、关联关系(Association)以及实现关系(Realization)
类图的概念 (转) 一.概述 类图(Class Diagram)是描述类.接口.协作以及它们之间关系的图,用来显示系统中各个类的静态结构.类图是定义其他图的基础,在类图基础上,可以使用状态图.协作图. ...
随机推荐
- ubuntu 16.04 安装 opencv +contrib (3.2.0) + python 3.5
环境: - ubuntu 16.04 - OpenCV + contrib 3.2.0 (文中附下载链接) - Python 3.5 基于其他环境的配置应该大同小异. 没时间解释了,直接上车. 更新下 ...
- SQL-54 查找排除当前最大、最小salary之后的员工的平均工资avg_salary。
题目描述 查找排除当前最大.最小salary之后的员工的平均工资avg_salary.CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL,`sala ...
- oracle入门之对表数据查询(三)
oracle表复杂查询--子查询 什么是子查询? 子查询是指嵌入在其它sql语句中的select语句,也叫嵌套查询. 单行子查询 单行子查询是指只返回一行数据的子查询语句. 请思考:如果显示与smit ...
- SQA计划与系统测试
(一)目的 本计划的目的是定义我们该小组所做的“爱上长大”项目的SQA任务和职责,在项目过程中应遵循的流程.规范和约定等,以确保软件质量得到维持. (二)范围 本计划应用于“爱上长大”项目开发的整个生 ...
- [C++]几种排序
本文为大大维原创,最早于博客园发表,转载请注明出处!!! 1.冒泡: #include<cmath> #include<cstdlib> #include<ctime&g ...
- Graphics Class
System.Drawing 封装一个 GDI+ 绘图图面. 此类不能被继承. https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.g ...
- PHP下载微信头像
public function downloadPic($openid='',$headimgurl='') { $header = array( 'User-Agent: Mozilla/5.0 ( ...
- python之路之简单介绍:
python介绍: a. python 基础 - 基础 - 基本的数据类型 - 函数 - 面向对象 python 安装 python 安装在os上 执行操作: 写一个文件,文件中按照python规则写 ...
- iOS TabelViewCell 删除 编辑 插入
/** TableView 进入或退出编辑状态(TableView 方法). */ - (void)setEditing:(BOOL)editing animated:(BOOL)animate{ / ...
- 2018-2019-2 网络对抗技术 20165228 Exp3 免杀原理与实践
2018-2019-2 网络对抗技术 20165228 Exp3 免杀原理与实践 免杀 一般是对恶意软件做处理,让它不被杀毒软件所检测.也是渗透测试中需要使用到的技术. 要做好免杀,就时清楚杀毒软件( ...