zoukankan      html  css  js  c++  java
  • 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>	
    

      

  • 相关阅读:
    【solr专题之三】Solr常见异常
    python清除数据库错误日志
    【LeetCode OJ 136】Single Number
    ZOJ 1649 Rescue(有敌人迷宫BFS)
    HDU 1885
    hdu 2594 Simpsons’ Hidden Talents
    _DataStructure_C_Impl:链串
    Errors occurred during the build. Errors running builder &#39;Integrated External Tool Builder&#39; on proje
    logo切图大小相应的尺寸
    再记作品展
  • 原文地址:https://www.cnblogs.com/Amywangqing/p/12945537.html
Copyright © 2011-2022 走看看