log4j和延迟加载

在之前下载的mybatis包中找到log4j并导入到项目

开启日志

如果不指定,Mybatis就会根据以下顺序寻找日志
SLF4J →Apache Commons Logging →Log4j 2→Log4j → JDK logging
日志级别:
DEBUG< INFO< WARN< ERROR
如果设置为info, 则只显示info及以上级别的信息;
建议:
在开发时设置debug,在运行时设置为info或以上。

1
2
3
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>

每次运行就会出现诸如此类的debug

1
2
3
4
5
6
7
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 961160488.
DEBUG [main] - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@394a2528]

配置延迟加载

嵌套查询多用于延迟加载的设计

具体如下

一对一:查学生和学生卡信息

在xml中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- 延迟加载 -->
<select id="qSByNWithMapO2OLazy" resultMap="student_card_Lazyload_map" parameterType="int">
<!-- 先查学生 -->
select * from student
</select>
<resultMap type="student" id="student_card_Lazyload_map">
<id property="stuNo" column="stuNo"/>
<result property="stuName" column="stuName"/>
<result property="stuAge" column="stuAge"/>
<result property="graName" column="graName"/>
<result property="stuSex" column="stuSex"/>
<!-- 此次采用延迟加载:在查询学生时,并不立即加载学生证信息
通过select在需要的时候再查学生证
-->
<association property="card" javaType="StudentCard" select="top.eshyee.mapper.StudentCardMapper.querycardbyid" column="cardid">
<!-- <id property="cardId" column="cardId"/>
<result property="cardInfo" column="cardInfo"/> -->
</association>
</resultMap>

新建Cardmapper

1
2
3
4
5
<!-- 查询学生信息 -->

<select id="querycardbyid" parameterType="int" resultType="top.eshyee.entity.StudentCard">
select * from studentCard where cardid=#{cardId}
</select>

一对多:查班级和学生信息

新建班级mapper

1
2
3
4
5
6
7
8
9
10
11
12
13
<select id="qCASLazy" resultMap="class_student_classid_lazy" parameterType="int">
select c.* from studentclass c
</select>

<resultMap type="top.eshyee.entity.StudentClass" id="class_student_classid_lazy">
<id property="classId" column="classId" />
<result property="className" column="className"/>
<!-- 属性类型用javatype 属性的元素类型用oftype -->
<!-- 延迟加载 -->
<collection property="student" ofType="top.eshyee.entity.Student" select="top.eshyee.mapper.StudentMapper.queryStuByClassId" column="classId">

</collection>
</resultMap>

在studentmapper中

1
2
3
4
<!-- 延迟加载一对多 查询班级中的所有学生 -->
<select id="queryStuByClassId" parameterType="int" resultType="top.eshyee.entity.Student">
select * from student where classId=#{classId}
</select>

一对多和一对一的延迟加载配置方法相同