Mybatis_懒加载

  • 懒加载就是在查询过程中,返回resultMap中对应的collection或者association如果不需要第一时间使用所有记录的值,可以再后续使用到时再进行进一步的查询,减轻数据库查询负担,提高效率。
<!-- 1.主查询语句,关联resultMap -->
<select id="select" resultMap="scmuserResultMap">
 	select * from scmuser
</select>
<!--2.定义resultMap -->
<resultMap type="com.test.model.Scmuser" id="scmuserResultMap">
	<id column="account" property="account"/>
	<result column="name" property="name"/>
	<result column="password" property="password"/>
	<result column="createdate" property="createdate"/>
	<result column="status" property="status"/>
	<!--3.collection 或者 association就是需要被懒加载的部分
		select 是懒加载时执行的查询语句
		column 关联条件列
	-->
	<collection property="models" ofType="com.test.model.Usermodel" select="selectModelByAccount" column="account">
	</collection>
</resultMap>
<!--4。懒加载对应的查询语句 -->
<select id="selectModelByAccount" parameterType="java.lang.String" resultType="com.test.model.Usermodel">
	select 
		account,u.modelcode,s.modelname
	from
		usermodel u, systemmodel s
	where
		u.modelcode = s.modelcode
	and 
		u.account = #{account}
</select>
  • mybatis配置文件中添加setting
<settings>
	<!-- 开启延时加载特性 -->
	<setting name="lazyLoadingEnabled" value="true"/>
	<!-- 懒加载对象不管有几层,每层属性都是用到时加载-->
	<setting name="aggressiveLazyLoading" value="false"/>
</settings>
发布了340 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Chill_Lyn/article/details/103753566