Mybatis中的延迟加载和懒加载

Mybatis中的延迟加载和懒加载

 

1.一对一实现延迟加载

配置SqlMapConfig.xml

Mybatis中的延迟加载和懒加载

<!--配置参数-->

<settings>

<!--开启Mybatis支持延迟加载-->

<setting name="lazyLoadingEnabled" value="true"/>

<!--默认就是false-->

<setting name="aggressiveLazyLoading" value="false"/>

</settings>

 

配置IAccountDao.xml

 

Mybatis中的延迟加载和懒加载

Mybatis中的延迟加载和懒加载

<!--懒加载-->

<!--定义封装account和user的resultMap-->

<resultMap id="accountUserMap2" type="account">

<id property="id" column="id"></id>

<result property="uid" column="uid"></result>

<result property="money" column="money"></result>

<!--一对一的关系映射:配置封装user的内容

select属性指定的内容:查询用户的唯一标志

column属性指定的内容:用户根据id查询时,所需要的参数的值

-->

<association property="user" column="uid" javaType="user" select="com.itcast.dao.IUserDao.findById"></association>

</resultMap>

 

<!--懒加载-->

<!--配置查询所有-->

<select id="findAll2" resultMap="accountUserMap2">

select * from account

</select>

 

定义接口

Mybatis中的延迟加载和懒加载定义测试类

Mybatis中的延迟加载和懒加载

开启懒加载之前

Mybatis中的延迟加载和懒加载

开启懒加载之后

Mybatis中的延迟加载和懒加载

2.一对多实现延迟加载

Mybatis中的延迟加载和懒加载

Mybatis中的延迟加载和懒加载

Mybatis中的延迟加载和懒加载

Mybatis中的延迟加载和懒加载

Mybatis中的延迟加载和懒加载