mybatis 关联查询 Collection定义关联集合封装规则

来源:互联网 发布:mac ppt 页面无法显示 编辑:程序博客网 时间:2024/06/08 04:53

应用场景 当关联查询时关联的是一个集合而不是一个对象时 例如:查询一个部门中的所有员工信息 此时使用ResultMap中的collection标签对关联的集合定义封装规则:

  1. collection嵌套结果集:
<resultMap type="mybatis_02.Department" id="getDepartmentAndAllPersonById">    <id column="did" property="id"/>    <result column="dname" property="name"/>    <collection property="persons" ofType="mybatis_02.Person">        <id column="pid" property="id"/>        <result column="pname" property="name"/>        <result column="gender" property="gender"/>    </collection> </resultMap>

其中
property:表示要封装成集合的javabean中的属性 例如Department类中有一个属性list persons表示部门中的所有人
ofType:表示封装的集合中的元素的java类型

分布查询与延迟加载与association类似 就不多说了

原创粉丝点击