Mybatis中的lazyloading

test代码
package cn.test.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.test.entity.Classes;
import cn.test.entity.Student;
import cn.test.utils.MyBatisUtils;

public class TestLoading {

/**
 * 测试延迟加载
 */
@Test
public void testLazy(){
    SqlSession session = MyBatisUtils.openSession();

    List<Classes> cs = session.selectList("selectClasses");

    MyBatisUtils.close();

    // 不访问任何的查询结果.
    // System.out.println("不访问查询结果");

    // 访问查询结果中的班级数据
    for(Classes c : cs){
        System.out.println(c.getName());
        for(Student s : c.getStudents()){
            System.out.println(s.getName());
        }
    }
}

}
结果的输出

:
08:04:23,854 DEBUG classes.selectClasses:139 - ==> Preparing: select * from tb_class
08:04:23,992 DEBUG classes.selectClasses:139 - ==> Parameters:
08:04:24,060 TRACE classes.selectClasses:145 - <== Columns: ID, NAME, BEGIN_TIME, TEACHER_NAME
08:04:24,068 TRACE classes.selectClasses:145 - <== Row: 1, 京南, 2016-12-23 00:00:00, 老马
08:04:24,186 TRACE classes.selectClasses:145 - <== Row: 2, 京北, 2017-02-10 00:00:00, 老高
08:04:24,187 DEBUG classes.selectClasses:139 - <== Total: 2
京南
08:04:24,189 DEBUG student.selectStudentsByClass:139 - ==> Preparing: select id, name, age from tb_student where class_id = ?
08:04:24,189 DEBUG student.selectStudentsByClass:139 - ==> Parameters: 1(BigDecimal)
08:04:24,191 TRACE student.selectStudentsByClass:145 - <== Columns: ID, NAME, AGE
08:04:24,191 TRACE student.selectStudentsByClass:145 - <== Row: 1, 小张, 25
08:04:24,192 TRACE student.selectStudentsByClass:145 - <== Row: 2, 小王, 25
08:04:24,193 DEBUG student.selectStudentsByClass:139 - <== Total: 2
小张
小王
京北
08:04:24,193 DEBUG student.selectStudentsByClass:139 - ==> Preparing: select id, name, age from tb_student where class_id = ?
08:04:24,194 DEBUG student.selectStudentsByClass:139 - ==> Parameters: 2(BigDecimal)
08:04:24,195 TRACE student.selectStudentsByClass:145 - <== Columns: ID, NAME, AGE
08:04:24,195 TRACE student.selectStudentsByClass:145 - <== Row: 3, 小丽, 25
08:04:24,197 TRACE student.selectStudentsByClass:145 - <== Row: 4, 小李, 25
08:04:24,197 DEBUG student.selectStudentsByClass:139 - <== Total: 2
小丽
小李

结果的输出:

08:07:46,646 DEBUG classes.selectClasses:139 - ==> Preparing: select * from tb_class
08:07:46,759 DEBUG classes.selectClasses:139 - ==> Parameters:
08:07:46,852 TRACE classes.selectClasses:145 - <== Columns: ID, NAME, BEGIN_TIME, TEACHER_NAME
08:07:46,864 TRACE classes.selectClasses:145 - <== Row: 1, 京南, 2016-12-23 00:00:00, 老马
08:07:46,960 TRACE classes.selectClasses:145 - <== Row: 2, 京北, 2017-02-10 00:00:00, 老高
08:07:46,961 DEBUG classes.selectClasses:139 - <== Total: 2
08:07:46,962 DEBUG student.selectStudentsByClass:139 - ==> Preparing: select id, name, age from tb_student where class_id = ?
08:07:46,963 DEBUG student.selectStudentsByClass:139 - ==> Parameters: 1(BigDecimal)
08:07:46,964 TRACE student.selectStudentsByClass:145 - <== Columns: ID, NAME, AGE
08:07:46,965 TRACE student.selectStudentsByClass:145 - <== Row: 1, 小张, 25
08:07:46,965 TRACE student.selectStudentsByClass:145 - <== Row: 2, 小王, 25
08:07:46,966 DEBUG student.selectStudentsByClass:139 - <== Total: 2
京南
小张
小王
08:07:46,967 DEBUG student.selectStudentsByClass:139 - ==> Preparing: select id, name, age from tb_student where class_id = ?
08:07:46,968 DEBUG student.selectStudentsByClass:139 - ==> Parameters: 2(BigDecimal)
08:07:46,970 TRACE student.selectStudentsByClass:145 - <== Columns: ID, NAME, AGE
08:07:46,970 TRACE student.selectStudentsByClass:145 - <== Row: 3, 小丽, 25
08:07:46,973 TRACE student.selectStudentsByClass:145 - <== Row: 4, 小李, 25
08:07:46,973 DEBUG student.selectStudentsByClass:139 - <== Total: 2
京北
小丽
小李

核心配置文件:mybatis.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 配置环境参数     name里面的值是确定的不能改为其他,否则报错 -->
    <settings>
        <!-- 开启延迟加载 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 关闭侵入性延迟加载 -->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

    <!-- 定义类型的别名, 别名定义成功后,可以在映射文件中使用.
        映射文件查找类型的时候,先检索别名,再检索全命名,如果都没有对应类型,抛出类型找不到异常
     -->
    <typeAliases>
        <!-- 为某类型定义别名 -->
        <!-- <typeAlias type="cn.test.entity.User" alias="user"/>
        <typeAlias type="java.lang.String" alias="string"/> -->
        <!-- 为某包中所有的类定义别名. 别名是类型的类名.
            如: cn.sxt.entity.User -> User
         -->
        <package name="cn.test.entity"/>
        <package name="java.lang" />
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@192.168.1.96:1521:Orcl"/>
                <property name="username" value="hr"/>
                <property name="password" value="zrb"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- 自动扫描接口+mapper映射文件 -->
        <!-- <package name="cn.test.mapper"/> -->
        <mapper resource="cn/test/entity/classes.xml"/>
        <mapper resource="cn/test/entity/student.xml"/>
    </mappers>
</configuration>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值