Mybatis中collection和association的使用区别
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的使用区别的更多相关文章
- mybatis中collection和association的作用以及用法
deptDaoMapper.xml 部门对应员工(1对多的关系) <resultMap type="com.hw.entity.Dept" id="deptinfo ...
- 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 联合查询 中column 传入多个参数值
下面是一个树形结构表自连接 联合查询 Demo <resultMap id="BaseResultMap" type="com.maidan.daas.entit ...
- Mybatis之collection与association标签
collection与association标签的功能就是为了解决查询条件映射到一个类或一个集合上,适用于对于多对一,一对多的映射结果,现在我们就探究其具体使用吧. 环境搭建: 数据库搭建 CREAT ...
- mybatis中collection association优化使用及多参数传递
mybatis都会用,但要优雅的用就不是那么容易了 今天就简单举例,抛砖引玉,供大家探讨 1.主表 CREATE TABLE `test_one` ( `id` int(11) NOT NULL AU ...
- MyBatis中collection (一对一,一对多)
MyBatis学习:http://www.mybatis.org/mybatis-3/zh/index.html 大对象InsuranceDetailsVO: com.quicksure.mobile ...
- mybatis中collection子查询注入参数为null
具体实现参照网上,但是可能遇到注入参数为null的情况,经过查阅及自己测试记录一下: 子查询的参数中,有<if test="">之类,需要指定别名,通过 http:// ...
随机推荐
- iOS:界面适配(二)--iPhone/iPad适配(关于xib)
本文纯属个人看法,强迫症后遗症 版本:xcode 6.0 + iOS SDK 8.0 讨论范围:控制器的view(创建VC时自带的xib) ------------------------------ ...
- 连接mysql时提示java.sql.SQLException: Access denied for user 'root'@'DESKTOP-N2B2D9A' (using password: YES)
用root连接mysql时提示:访问被拒绝 检查一下mysql server是否开启,发现后台在运行着.. 然后查了一下mysql的用户表,发现root只能运行使用本地ip(localhost或者1 ...
- bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会【tarjan】
几乎是板子,求有几个size>1的scc 直接tarjan即可 #include<iostream> #include<cstdio> #include<cstri ...
- bzoj 2442: [Usaco2011 Open]修剪草坪【单调栈】
设f[i]为i不选的最小损失,转移是f[i]=f[j]+e[i[(i-j-1<=k) 因为f是单调不降的,所以f[j]显然越靠右越好因为i-j-1<=k的限制,所以单调栈需要弹栈 #inc ...
- ngCordova插件说明
转载自 http://my.oschina.net/u/1416844/blog/495026 参 考http://blog.csdn.net/superjunjin/article/details/ ...
- MySQL索引使用以及优化
优化后台业主评价服务人员运行缓慢. 案发现场:后台业主评价服务人员列表页以及搜索页运行缓慢.运行时间为24074ms. 排查过程: 1.代码开头加时间,结束加时间.看运行了多少秒. 2.给评价 ...
- javascript检测基本类型值或引用类型值的类型方法
首先javascript的数据类型分为两种数据类型:基本数据数据类型和引用数据类型 基本数据类型:Number,String,Boolean,Undefined,Null.原始值,是简单的数据段,可按 ...
- 三分 POJ 2420 A Star not a Tree?
题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...
- Python安装第三方包(setup.py)
在github上下载了records文件到本地. 解压文件 cmd切换到文件setup.py的目录下 先执行 python setup.py build 再执行python setup.py inst ...
- MongoDB学习笔记~复杂条件拼接和正则的使用
在大叔lind框架里有日志组件logger,而在日志实现方式上有file,mongodb,sql,json等方式,对分布式日志处理上大叔推荐使用mongodb进行存储,除了它的高效写入,灵活的结构外, ...