我想知道iBatis是否可以像Hibernate支持的那样为懒惰提取提供支持?例如,我有User类
public class User {
List<Address> addresses;
public List<Address> getAddresses(){
return addresses;
}
...
}
现在我想在调用user.getAddresses()时实现延迟提取。是否有可能在iBatis中实现这一目标?如果是,那怎么样?
谢谢你的帮助。
当然。
根据文档,您可以为要加载延迟的属性设置lazyLoad
的result
元素的resultMap
属性。
例如。:
<resultMap id="resultMapUser" class="User">
<result property="addresses" select="someOtherStatementName" lazyLoad="true"/>
</resultMap>
XML映射: 3.2.7+ - > fetchType属性(lazy / eager)
<resultMap id="itemResultMap" type="Item">
<id property="id" column="id"/>
<association property="subItem" column="subItemId" javaType="SubItem"
select="selectSubItemByIdQuery" fetchType="lazy"/>
</resultMap>
@Annotation映射:
@Select({"SELECT * FROM item WHERE id = #{id}"})
@Results({
@Result(property = "id", column = "id"),
@Result(property = "subItem", column = "subItemId", javaType = SubItem.class,
one = @One(select = "SubItemMapper.getById",
fetchType = FetchType.LAZY))
})
Item getById(int id);