以下是一个典型的主子表应用,例如:
一个入库单,有入库单头(主表:'单号','日期'等信息)及入库单物料明细(子表:'物料编号','物料名称'等信息)两张表构成,在用mybatis来处理时,子表的数据就要用到Collection层现(Collection集合顾名思议就是可以放很多条记录的一个容器)
<resultMap type="Bill" id="ResultBill">
	       <id property="id" column="id"/>
	       <result property="dh" column="dh"  />	      
	       <result property="rq" column="rq"/>
	       <association property="author"  javaType="Author"  >
	          <id  property="id"  column="idAutor" />
	          <result property="name" column="authorName" />
	          <result property="email" column="authorEmail"/>
	       </association>	       
	       <collection property="det1"  javaType="ArrayList" ofType="BillDet1" >
	          <id property="id" column="idDet1"/>
	          <result property="dh" column="dhDet1"/>
	          <result property="wlbm" column="wlbm"/>
	          <result property="wlmc" column="wlmc"/>
	          <result property="unit" column="unit"/>
	          <result property="qty" column="qty"/>
	       </collection>
</resultMap>
   <select id="getAllBill" resultMap="ResultBill">
        select t1.id,t1.dh,t1.idAuthor,t1.rq
       ,t2.name as authorName ,t2.email as authorEmail
       ,t3.dh as dhDet1,t3.id as idDet1,t3.wlbm as wlbm,t3.wlmc as wlmc,t3.unit as unit,t3.qty as qty  
   from  Bill_Det1   t3
   left outer join  bill  t1 on  t1.dh=t3.dh    
   left outer join Author   t2  on t1.idAuthor=t2.idAuthor
 </select>

在collection中发现有个column属性,在以上Demo中无需使用,经测试发现,此column属性,一般与select属性结合使用,但网上很多资料中并未作出说明,故在此说明一下,让后来者少走弯路.

如:

 <collection property="det1"  javaType="ArrayList" ofType="BillDet1" column="dh" select="getAllBillDet1">
	          <id property="id" column="idDet1"/>
	          <result property="dh" column="dhDet1"/>
	          <result property="wlbm" column="wlbm"/>
	          <result property="wlmc" column="wlmc"/>
	          <result property="unit" column="unit"/>
	          <result property="qty" column="qty"/>
</collection>
  <select id="getAllBillDet1" resultMap="ResultBillDet1">
       select 
          t3.dh as dhDet1,t3.id as idDet1,t3.wlbm as wlbm,t3.wlmc as wlmc,t3.unit as unit,t3.qty as qty  
    	 from    Bill_Det1 t3          
</select>
以上使用时应注意的一些小细节,特此记录,备查.
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐