Mybaits延迟加载

本文介绍了MyBatis的延迟加载,即按需加载,当一个对象包含另一属性对象时,等需要该属性对象信息才执行查询。具体实现是将1对1或1对多的sql语句按表分割,再连接两次查询和填充。还提及了开启延迟加载及不同关系的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mybaits延迟加载

​ 延迟加载,即按需加载

​ 一个对象(表)中包含另一个属性对象(另一张表),等到需要属性对象的信息时,才执行相关的查询,获取到属性对象的信息。

​ 具体实现,将1对1或1对多的sql语句按照表进行分割,自然属性的填充也就进行了分割,然后将两次查询,两次填充连接起来。

开启延迟加载

 <settings>
 	<!-- 开启延迟加载 -->
 	<setting name="lazyLoadingEnabled" value="true"/>
 	<setting name="aggressiveLazyLoading" value="false"/>
 </settings>

延迟加载1对多

sql映射

  		<resultMap type="Dept" id="baseMap">
  			<id column="dept_id" property="deptId"/>
  			<result column="dept_name" property="deptName"/>
  			<result column="dept_desc" property="deptDesc"/>
  			<!-- 1 对 多的关联关系通过 collection配置
  				property 对应的就是对象中的多的那方的变量名称
  				ofType:就是集合中的泛型的类型
  			-->
  			<collection property="emps" ofType="emp"
  				select="queryEmpByDeptId" column="dept_id">
  			</collection>
  		</resultMap>
  		
  		<select id="query" resultMap="baseMap">
  			select * from t_dept
  		</select>
  		
  		<resultMap type="emp" id="empResultMap">
  			<id column="emp_id" property="empId"/>
			<result column="emp_name" property="empName"/>
			<result column="emp_age" property="empAge"/>
  		</resultMap>
  		
  		<select id="queryEmpByDeptId" parameterType="int" resultMap="empResultMap" >
  			select * from t_emp where dept_id = #{id}
  		</select>

测试一下

	@Test
	public void test1() throws IOException {
		InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		SqlSession session = factory.openSession(true);
		DeptMapper dao = session.getMapper(DeptMapper.class);
		List<Dept> list = dao.query();
		for (Dept dept : list) {
			System.out.println(dept.getDeptId()+" "+dept.getDeptName());
			//System.out.println("----"+dept.getEmps());
		}
		session.close();
	}

需要dept对象信息

[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==>  Preparing: select * from t_dept 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <==      Total: 6
1 小卖部
2 销售部
3 研发部
4 行政部

需要emp对象信息

[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==>  Preparing: select * from t_dept 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <==      Total: 6
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==>  Preparing: select * from t_emp where dept_id = ? 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: 1(Integer)
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <==      Total: 2
----[Emp [empId=4, empName=古天乐, empAge=15], Emp [empId=8, empName=耶稣, empAge=15]]
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==>  Preparing: select * from t_emp where dept_id = ? 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: 2(Integer)
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <==      Total: 2
----[Emp [empId=2, empName=刘德华, empAge=15], Emp [empId=3, empName=刘诗诗, empAge=16]]
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==>  Preparing: select * from t_emp where dept_id = ? 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: 3(Integer)
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <==      Total: 2
----[Emp [empId=1, empName=孙悟空, empAge=13], Emp [empId=7, empName=美杜莎, empAge=17]]
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==>  Preparing: select * from t_emp where dept_id = ? 
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | ==> Parameters: 4(Integer)
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(159) | <==      Total: 2
----[Emp [empId=5, empName=水娃, empAge=13], Emp [empId=6, empName=宙斯, empAge=16]]

延迟加载1对1

sql映射

  		<resultMap type="emp" id="baseMap">
  			<id column="emp_id" property="empId"/>
			<result column="emp_name" property="empName"/>
			<result column="emp_age" property="empAge"/>
			<!-- 一对一的关联配置 -->
			<association property="dept" javaType="dept" select="queryDeptByDeptId" column="dept_id">
				
			</association>
  		</resultMap>
  		
  		
  		<select id="query" resultMap="baseMap">
  			select * from t_emp
  		</select>
  		
  		<resultMap type="dept" id="deptResultMap">
  				<id column="dept_id" property="deptId"/>
	  			<result column="dept_name" property="deptName"/>
	  			<result column="dept_desc" property="deptDesc"/>
  		</resultMap>
  		
  		<select id="queryDeptByDeptId" parameterType="int" resultMap="deptResultMap" >
  			select * from t_dept where dept_id=#{id}
  		</select>

其它与1对多类似,省略。

项目打包 提取码:ov66 环境:eclipse,maven

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值