zoukankan      html  css  js  c++  java
  • mybatis中collection和association的作用以及用法

    deptDaoMapper.xml
    部门对应员工(1对多的关系)

    <resultMap type="com.hw.entity.Dept" id="deptinfo"><!-- 如果不用resultMap则不写 -->
            <result column="did" property="did" />
            <result column="dname" property="dname" />
            <!-- mybatis中 1方配置多方 -->
            <collection property="per" ofType="com.hw.entity.Person">
                <result column="pid" property="pid" />
                <result column="pname" property="pname" />
                <result column="psex" property="psex" />
                <result column="skilled" property="skilled" />
                <result column="degree" property="degree" />
                <result column="jobtime" property="jobtime" javaType="java.sql.Date" jdbcType="DATE" />
                <result column="resume" property="resume" />
                <result column="filepath" property="filepath" />
            </collection>
        </resultMap>
    

    javabean中的属性是集合set ,这时用collection

    
    

    personDaoMapper.xml
    员工对应部门(多对一的关系)

    <resultMap type="com.hw.entity.Person" id="personinfo"><!-- 如果不用resultMap则不写 -->
            <result column="pid" property="pid" />
            <result column="pname" property="pname" />
            <result column="psex" property="psex" />
            <result column="skilled" property="skilled" />
            <result column="degree" property="degree" />
            <result column="jobtime" property="jobtime" javaType="java.sql.Date"
                jdbcType="DATE" />
            <result column="resume" property="resume" />
            <result column="filepath" property="filepath" />
            <!--多对一的关系, property: 指的是属性的值, javaType:指的是属性的类型 -->
            <association property="dept" javaType="com.hw.entity.Dept">
                <result column="did" property="did" />
                <result column="dname" property="dname" />
            </association>
        </resultMap>
    

    javabean是类与类之间的关联,这时用association
    关联(Association)关系是类与类之间的联接,它使一个类知道另一个类的属性和方法。关联可以是双向的,也可以是单向的。在Java语言中,关联关系一般使用成员变量来实现。



    作者:愤怒的_菜鸟
    链接:https://www.jianshu.com/p/92efd20637ed
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    leetcode 279. Perfect Squares
    leetcode 546. Remove Boxes
    leetcode 312. Burst Balloons
    leetcode 160. Intersection of Two Linked Lists
    leetcode 55. Jump Game
    剑指offer 滑动窗口的最大值
    剑指offer 剪绳子
    剑指offer 字符流中第一个不重复的字符
    leetcode 673. Number of Longest Increasing Subsequence
    leetcode 75. Sort Colors (荷兰三色旗问题)
  • 原文地址:https://www.cnblogs.com/yaowen/p/8882100.html
Copyright © 2011-2022 走看看