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