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

      

  • 相关阅读:
    MySql 有用的函数
    mysql 触发器
    java之switch语句
    MaxAlertView 强大的弹框试图
    AVMoviePlayer 视频播放器
    Mac下不能安装第三方下载软件
    HTTPS链式编程——AFNetworking 3.0
    iOS推送证书生成pem文件(详细步骤)
    iOS成长之路-使用系统默认声音、震动
    iOS 怎么自定制推送声音呢?(APP运行时和APP进入后台时)
  • 原文地址:https://www.cnblogs.com/Amywangqing/p/12945537.html
Copyright © 2020-2023  润新知