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的类型别名图

原文地址:https://www.cnblogs.com/pxzbky/p/10050257.html

时间: 2024-08-29 13:57:57

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

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

下面是一个树形结构表自连接 联合查询 Demo <resultMap id="BaseResultMap"  type="com.maidan.daas.entity.AccoSysmanResource" >    <id column="pid" property="pid" jdbcType="INTEGER" />    <result column="cre

Mybatis之collection与association标签

collection与association标签的功能就是为了解决查询条件映射到一个类或一个集合上,适用于对于多对一,一对多的映射结果,现在我们就探究其具体使用吧. 环境搭建: 数据库搭建 CREATE TABLE teacher ( id INT(10) NOT NULL, name VARCHAR(30) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=INNODB DEFAULT CHARSET=utf8 INSERT INTO teacher(id, na

MyBatis中关于resultType和resultMap的区别

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的(对应着我们的model对象中的实体),而resultMap则是对外部ResultMap的引用(提前定义了db和model之间的隐射key-->value关系),但是resultType跟resultMap不能同时存在. 在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值.

Mybatis中collection实现一对多的问题

今天在使用Mybatis中的Collection获取集合信息时,数据库中对应了多条数据,但在做单元测试时只能获取到一条数据. 纠结了很久,突然想到是不是主键的问题,结果一试,还真是这么回事. Mybatis中id和result的唯一不同是id表示的结果将是当比较对象实例时用到的标识属性.这帮助来改进整体表现,特别是缓存和嵌入结果映射.因此在不同的数据中要对ID进行唯一处理,不然就会出现类似我只能查看一条数据. <resultMap type="OrderBaseInfo" id=

mybatis 中collection中需要 open close separator

<result property="openFactories" column="open_factories" /> The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collect 2018年03月22日 10:12:55 chenxing1990 阅读数:1296 标签: mybatisr

MyBatis中引用变量符号$与#的区别解析

例子 假设我们要通过创建时间create_time查询数据库表product里的具体条目 如果要查询创建时间大于等于"2019-01-29"的条目,那么SQL查询语句应是: select * from product where create_time >="2019-01-29",这时候返回了正确的数据 用mybatis框架查询对应写法如下: @Select({ "SELECT * ", "FROM product WHERE

Mybatis中的#{}和${}的区别?

1,首先Mybatis中的#{}与${}到底有什么区别? #{}:表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,#{}可以有效防止sql注入. #{}可以接收简单类型值或pojo属性值. 如果parameterType传输单个简单类型值,#{}括号中可以是value或其它名称.“%”#{name}”%” ${}:表示拼接sql串,通过${}可以将parameterType 传入的内容拼接在sql中且不进行jdbc类

mybatis中&quot;#&quot;和&quot;$&quot;的区别

mybatis中"#"和"$"的区别 动态 sql 是 mybatis 的主要特性之一,在 mapper 中定义的参数传到 xml 中之后,在查询之前 mybatis 会对其进行动态解析.mybatis 为我们提供了两种支持动态 sql 的语法:#{} 以及 ${}. 在下面的语句中,如果 username 的值为 zhangsan,则两种方式无任何区别: select * from user where name = #{name}; select * from

mybatis 中#与$的区别

MyBatis/Ibatis中#和$的区别 1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by "111", 如果传入的值是id,则解析成的sql为order by "id". 2. $将传入的数据直接显示生成在sql中.如:order by $user_id$,如果传入的值是111,那么解析成sql时的值为order by user_id,