1. 修改SqlMapConfig.xml
~~~
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
~~~
2. 例如以下案例,根据订单id查询订单,以及订单对应的用户信息
非延迟加载如下:
~~~
<select id="getOrderById" parameterType="int" resultMap="_orderuser">
select u.*, o.*
from userinfo u, sorder o
where u.id = o.userid
and oid = #{oid}
</select>
<resultMap type="Sorder" id="_orderuser">
<id column="oid" property="oid"/>
<result column="odate" property="odate"/>
<result column="ostatus" property="ostatus"/>
<result column="oaddress" property="oaddress"/>
<result column="totalprice" property="totalprice"/>
<association property="u" javaType="Userinfo">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<result column="sal" property="sal"/>
</association>
</resultMap>
~~~
延迟加载如下:
~~~
<select id="getOrderById" parameterType="int" resultMap="_orderuser">
select o.*
from sorder o
where oid = #{oid}
</select>
<select id="getUserById" parameterType="int" resultType="Userinfo">
select *
from userinfo
where id = #{userid}
</select>
<resultMap type="Sorder" id="_orderuser">
<id column="oid" property="oid"/>
<result column="odate" property="odate"/>
<result column="ostatus" property="ostatus"/>
<result column="oaddress" property="oaddress"/>
<result column="totalprice" property="totalprice"/>
<association property="u" javaType="Userinfo" select="getUserById" column="userid">
</association>
</resultMap>
~~~