Mybatis中collection和association的使用區別 發布於 3 年前 (2017年10月18日)

最近一直把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

public class Card implements Serializable{  	private Integer id;  	private String code;  //省略set和get方法.  }
  public class Person implements Serializable{  	private Integer id;  	private String name;  	private String sex;  	private Integer age;  	//人和身份證是一對一的關係  	private Card card;  //省略set/get方法.  }

下面是mapper和實現的介面

package com.glj.mapper;    import com.glj.poji.Card;    public interface CardMapper {  	Card selectCardById(Integer id);  }

CardMapper.xml

<?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>

User介面

package com.glj.mapper;    import com.glj.poji.Person;    public interface PersonMapper {  	Person selectPersonById(Integer id);  }

UserMapper

<?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類

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方法  }
  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方法  }

ClazzMapper.xml

<?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>  

ClazzMapper介面

package com.glj.mapper;    import com.glj.pojo.Clazz;    public interface ClazzMapper {  	Clazz selectClazzById(Integer id);  }

ClazzMapper使用到了集合-collection 即為一對多,一個班級面對多個學生

StudentMapper.xml

<?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>

StudentMapper介面

  package com.glj.mapper;    import com.glj.pojo.Student;    public interface StudentMapper {  	Student selectStudentById(Integer id);  }

StudentMapper則是與班級為多對一關係,所以使用了關聯-association


嗯,希望我以後又不記得二者的關係時,能感謝現在總結的自己

附上一張mybatis的類型別名圖