1. 程式人生 > >ibatis配置多表關聯(一對一、一對多、多對多)

ibatis配置多表關聯(一對一、一對多、多對多)

<sqlMap namespace="test"><typeAlias alias="Key" type="zzcv.dao.domain.Key"/><typeAlias alias="Lock" type="zzcv.dao.domain.Lock"/><resultMap id="KeyResult" class="Key"><result property="id" column="id"/><result property="keyName" column="keyName"/><result property="lock"
 column="lockId" select="getLockById"/></resultMap><!-- 通過key表中儲存的lock的id實現的一對一關聯,ibatis會使用getLockById(lockId)的結果填充lock屬性 --><resultMap id="LockResult" class="Lock"><result property="id" column="id"/><result property="lockName" column="lockName"/><result property="keys"
 column="id" select="getKeysByLockId"/></resultMap><!-- 通過lock的id實現的一對多關聯,ibatis會使用getKeysByLockId(id)得到的List填充keys屬性 --><!-- 多對多可以通過巢狀實現,這裡就不列出了 --><select id="selectAllkeys" resultMap="KeyResult"><![CDATA[   
  select id,lockId,keyName from key
  
]]></select><select 
id="getLockById" parameterClass="int" resultClass="Lock"><![CDATA[   
  select id,lockName from lock where id = #value#   
  
]]></select><select id="selectAllLocks" resultMap="LockResult"><![CDATA[   
  select id,lockName from lock
  
]]></select><select id="getKeyByLockId" parameterClass="int" resultClass="key"><![CDATA[   
  select id,lockId,keyName from lock where lockId = #value#   
  
]]></select></sqlMap>