mybatis懒加载机制

mybatis懒加载机制

需要查询关联信息时,使用Mybatis懒加载特性可有效的减少数据库压力,首次查询只查询主表信息,关联表的信息在用户获取时再加载。
Mybatis一对一关联的association和一对多的collection可以实现懒加载。懒加载时要使用resultMap,不能使用resultType。

1、在mybatis的全局配置中开启延迟加载功能

  • Mybatis默认没有打开懒加载配置,需要在mybatis全局配置文件.xml中通过settings配置lazyLoadingEnabled、aggressiveLazyLoading(按需加载)来开启懒加载。
	<settings>
		<!-- dept_id转换为驼峰命名法deptId -->
		<setting name="mapUnderscoreToCamelCase" value="true"/>
		<!-- 延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true"/>
	</settings>

2、*mapper.xml文件

注意在resultMap中配置名称可能比较绕,一般问题会出现在这。

association标签

<resultMap type="User" id="userMap">
	<id property="id" column="id"/>
	<result property="name" column="name"/>
	<result property="sex" column="sex"/>
	<result property="age" column="age"/>
	<result property="oper" column="oper"/>
  	<!-- property表示java实体中属性的名称,javaType表示属性的类型,column表示在查询结果中的列,selecr表示要执行的sql语句 fetchType表示取消延迟加载 -->
	<association property="dept" javaType="Dept" column="dept_id" select="findDeptLazy" fetchType="eager" >
		<id property="id" column="id"/>
		<result property="deptName" column="dept_name"/>
		<result property="deptDesc" column="dept_desc"/>
	</association>
</resultMap>
 	
<select id="findUserDeptLazy" parameterType="int" resultMap="userMap">
	SELECT id ,name,sex,age,oper,dept_id
		FROM t_user 
		WHERE id = #{id}
</select>
 	
<select id="findDeptLazy" resultType="Dept">
	SELECT id,dept_name,dept_desc 
		FROM t_dept
		WHERE id =  #{id}
</select>

collection标签

 <!-- 懒加载 -->
<resultMap type="Dept" id="deptMapLazy">
	<!-- 主键 -->
	<id property="id" column="id"/>
	<result property="deptName" column="dept_name"/>
	<result property="deptDesc" column="dept_desc"/>
   	<!--  property表示java实体类中属性的名称,javaType表示属性的类型,ofType表示泛型,column表示应用查询的某列 select表示需要执行的sql语句 fetchType表示是否开启延迟加载eager取消延迟加载,lazy开启延迟加载,默认开启 -->
	<collection property="userlist" javaType="ArrayList" ofType="User" column="id" select="fingUserList" fetchType="eager">
 	<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="sex" column="sex"/>
		<result property="age" column="age"/>
		<result property="oper" column="oper"/>
	</collection>
</resultMap>

<select id="findDpetByIdLazy" parameterType="int" resultMap="deptMapLazy">
	SELECT  id,dept_name,dept_desc 
			from t_dept 
			where id = #{id}
</select>

<select id="fingUserList" resultType="User">
	select id,name,sex,age,oper,dept_id 
			from t_user 
			where dept_id = #{dept_id}
</select>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis懒加载是一种延迟加载的机制,只有在需要使用关联对象时才会进行加载。在Mybatis中,懒加载主要应用于关联查询,通过设置延迟规则,推迟对关联对象的查询,减轻数据库的压力。懒加载只对关联对象有延迟设置,而不会延迟主对象的查询。\[1\] 要开启懒加载,需要在Mybatis的主配置文件中的settings标签中设置lazyLoadingEnabled为true。同时,可以设置aggressiveLazyLoading为false来控制懒加载的行为。\[3\] 需要注意的是,开启懒加载是全局的设置,即对所有关联对象都生效。如果只想对部分关联对象进行懒加载,可以使用association和collection的fetchType属性来覆盖全局的懒加载状态。fetchType属性可以设置为eager表示立即加载,lazy表示使用懒加载。\[3\] 总结来说,Mybatis懒加载是一种延迟加载机制,可以减轻数据库的压力。通过在主配置文件中设置相关属性,可以控制懒加载的行为,包括全局开启懒加载、设置懒加载的延迟规则以及对部分关联对象进行懒加载。\[1\]\[3\] #### 引用[.reference_title] - *1* [Mybatis懒加载](https://blog.csdn.net/qq_52519008/article/details/127118918)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [八、(了解即可)MyBatis懒加载(或者叫延迟加载)](https://blog.csdn.net/a924382407/article/details/130505098)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [MyBatis懒加载](https://blog.csdn.net/layonly/article/details/120719900)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值