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 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
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
settings>
<typeAliases>
<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 resource="cn/test/entity/classes.xml"/>
<mapper resource="cn/test/entity/student.xml"/>
mappers>
configuration>