collection和association的使用区别

一对一使用association

一对多使用ollection

班级实体类

public class Clazz implements Serializable {

	private Integer id;
	private String name;

}

  

学生实体类

public class Student implements Serializable {

	private Integer id;
	private String name;
	private Integer age;
	private Integer classId;

}

  

学生对班级,一对一关系

public class ClazzAndStudentVO implements Serializable {

	private Integer id;
	private String name;
	private Integer age;
	private Integer classId;
	private Clazz clazz;

}

  

学生对班级,一对一映射关系

<mapper namespace="xx.xx.xx.xx.StudentAndClazzMapper">
		<resultMap id="StudentAndClazzVOMap" 
		type="cn.tedu.mybatis.vo.StudentAndClazzVO">
		<!-- id节点:主键的配置 -->
		<!-- column:查询结果的列名 -->
		<!-- property:数据类型的属性名 -->
		<id column="id" property="id"/>
			<result column="name" property="name"/>
			<result column="age" property="age"/>
			<result column="class_id" property="class_id"/>
		<association property="clazz" column="department_sn" javaType="Clazz" >
            <id property="id" column="cid" />
            <result property="name" column="cname" javaType="String"/>
        </association>
	</resultMap>	

  

班级对学生一对多关系

public class StudentAndClazzVO implements Serializable {

	private Integer clazzId;
	private String clazzName;
	private List<Student> students;
}

  

班级对学生一对多,映射关系

<mapper namespace="xx.xx.xx.xx.StudentAndClazzMapper">
	<resultMap id="StudentAndClazzVOMap" 
		type="cn.tedu.mybatis.vo.StudentAndClazzVO">
		<!-- id节点:主键的配置 -->
		<!-- column:查询结果的列名 -->
		<!-- property:数据类型的属性名 -->
		<id column="clazz_id" property="clazzId"/>
		<!-- 其它结果的配置 -->
		<result column="clazz_name" property="clazzName"/>
		<!-- 配置List集合 -->
		<!-- ofType:集合中放的哪种类型的数据 -->
		<collection property="students"
			ofType="xx.xx.xx.Student">

			<id column="id" property="id"/>
			<result column="name" property="name"/>
			<result column="age" property="age"/>
			<result column="class_id" property="class_id"/>
		</collection>
	</resultMap>	

  

原文地址:https://www.cnblogs.com/Amywangqing/p/12945537.html