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
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文地址:https://www.cnblogs.com/yaowen/p/8882100.html