mybatis association与collection的区别:
一、一对一
association通常用来映射一对一的关系
1 <resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap"> 2 3 <!-- 配置映射的订单信息 --> 4 <id column="id" property="id" /> 5 <result column="user_id" property="userId" /> 6 <result column="number" property="number" /> 7 <result column="createtime" property="createTime" /> 8 <result column="note" property="note" /> 9 <!-- 配置关联的用户信息 --> 10 11 <association property="user" javaType="cn.itcast.mybatis.po.User"> 12 <id column="user_id" property="id" /> 13 <result column="username" property="username" /> 14 <result column="sex" property="sex" /> 15 <result column="address" property="address" /> 16 </association> 17 18 </resultMap>
二、一对多,
collection
1 <resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersAndOrdersDetail" extends="OrdersUserResultMap"> 2 <!-- 配置订单详细信息 一个订单关联查出了多条明细,要使用collection进行映射 collection:对关联查到的多条记录映射到集合对象中 3 property:将关联查询到多条记录映射到对象的某个属性中 ofType 指定映射集合属性的的pojo对象的类型 --> 4 <collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail"> 5 <!-- id:订单明细唯 一标识 6 property:要将订单明细的唯 一标识 映射到cn.itcast.mybatis.po.Orderdetail的哪个属性 7 --> 8 <id column="orderdetail_id" property="id"/> 9 <result column="items_id" property="itemsId"/> 10 <result column="items_num" property="itemsNum"/> 11 <result column="orders_id" property="ordersId"/> 12 13 </collection> 14 15 </resultMap>标签:10,13,12,15,collection,Mybatis,association From: https://www.cnblogs.com/joe-tang/p/7284699.html