Mybatis知识点(懒加载)
-
懒加载和联级映射的关系
在《mybatis 3源码深度解析》有下文:
MyBatis其中一个比较强大的功能是支持查询结果级联映射。使用MyBatis级联映射,我们可以很轻松地实现一对多、一对一或者多对多关联查询,甚至可以利用MyBatis提供的级联映射实现懒加载。所谓的懒加载,就是当我们在一个实体对象中关联其他实体对象时,如果不需要获取被关联的实体对象,则不需要为被关联的实体执行额外的查询操作,仅当调用当前实体的Getter方法获取被关联实体对象时,才会执行一次额外的查询操作。通过这种方式在一定程度上能够减轻数据库的压力。
总结:
- 懒加载是发生在一对一,一对多的时候才有用所以说发生在联级映射
- 懒加载是当调用了关联对象的get()方法后额外执行一次查询,否则就不执行
-
如何使用懒加载
- MyBatis主配置文件中提供了lazyLoadingEnabled和aggressiveLazyLoading参数用来控制是否开启懒加载机制。
- lazyLoadingEnabled参数值为true时表示开启懒加载,否则表示不开启懒加载。
- aggressiveLazyLoading参数用于控制ResultMap默认的加载行为,参数值为false表示ResultMap默认的加载行为为懒加载,否则为积极加载。
- 除此之外,
和 标签还提供了一个fetchType属性,用于控制级联查询的加载行为,fetchType属性值为lazy时表示该级联查询采用懒加载方式,当fetchType属性值为eager时表示该级联查询采用积极加载方式。
-
演示
这里引用的我的文章:
https://www.cnblogs.com/SweetheartAndPeaches/p/15439005.html中的一对一的例子。
我们先来看ResultMap映射
<resultMap type="entity.Vote" id="votemap"> <!-- coulmn是字段数据库中的字段 ,告诉mybatis要用property来对应字段 --> <result column="voteTitle" property="voteTitle"/> <result column="voteDescription" property="voteDescription"/> <result column="createTime" property="createTime"/> <result column="startTime" property="startTime"/> <result column="endTime" property="endTime"/> <result column="createUser" property="createUser"/> <result column="comments" property="comments"/> <!-- association javaType是函数类型自定义的对象 --> <association property="user" javaType="entity.VoteUser"> <result column="userName" property="userName" /> <result column="userPwd" property="userPwd" /> </association> </resultMap> <!-- 使用这个自定义的map --> <select id="selectVoteByVoteuser" resultMap="votemap"> select vote.voteTitle,voteuser.username,voteuser.userpwd,voteuser.applyVote from vote,voteuser where vote.createuser=voteuser.username and voteid=1 </select>如果要使用懒加载我们需要把上面的ResultMap进行小小改造分离association(因为上面的联级映射一次就会把所有数据查询出来)
<!-- type是resultmap的类型 id是标识符 --> <resultMap type="entity.Vote" id="votemap"> <!-- coulmn是字段数据库中的字段 ,告诉mybatis要用property来对应字段 --> <result column="voteTitle" property="voteTitle"/> <result column="voteDescription" property="voteDescription"/> <result column="createTime" property="createTime"/> <result column="startTime" property="startTime"/> <result column="endTime" property="endTime"/> <result column="createUser" property="createUser"/> <result column="comments" property="comments"/> <!-- association javaType是函数类型自定义的对象 --> <association property="user" javaType="entity.VoteUser" column="createUser" select="selectuser"></association> </resultMap> <!--定义一个调用selectUser --> <select id="selectuser" resultType="entity.VoteUser"> select * from Voteuser where username=#{username} </select> <!-- 使用这个自定义的map --> <select id="selectVoteByVoteuser" resultMap="votemap"> select vote.voteTitle,voteuser.username,voteuser.userpwd,voteuser.applyVote from vote,voteuser where vote.createuser=voteuser.username and voteuser.username="小红" </select>发现了原来的 ---->association发生变化
<association property="user" javaType="entity.VoteUser"> <result column="userName" property="userName" /> <result column="userPwd" property="userPwd" /> </association>变成了下面的----->association
<!--多了一个select="selectuser"代表的是引用id名叫selectuser的select语句--> <!--细心的同学还发现多了一个column字段这代表的要把column="createUser"的值传递给select--> <association property="user" javaType="entity.VoteUser" column="createUser" select="selectuser"> <select id="selectuser" resultType="entity.VoteUser"> select * from Voteuser where username=#{username} </select>如果你现在运行的话并不会出现懒加载我们需要设置
<settings> <!--开启mybatis的懒加载功能--> <setting name="lazyLoadingEnabled" value="true"/> <!--将积极加载改变成按需加载--> <setting name="aggressiveLazyLoading" value="false"/> <!--equals,clone,hashCode,toString将不会触发懒加载--> <setting name="lazyLoadTriggerMethods" value=""/> </settings>现在懒加载已经开启了!
只要调用了实体类的getuser()方法association才会被执行然后赋值给这个实体类
目的:当查询vote表的字段已经够用时,继续查询相关联的voteuser表已经没有必要了,为了节省资源有了懒加载这个功能。

浙公网安备 33010602011771号