Mybatis整合log4j,延迟加载

a.添加log4j和commens-logging的jar包,

b.然后在mybatis的配置文件conf.xml中开启日志

Mybatis整合log4j,延迟加载

如果不指定,Mybatis就会根据以下顺序 寻找日志
SLF4J →Apache Commons Logging →Log4j 2 → Log4j →JDK logging

c.编写配置日志输出文件log4j.properties,具体怎么编写日志文件,这里就不再解释了。

log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

配置好之后,再测试一下,mybatis执行时的各种信息都能打印在控制台中了

Mybatis整合log4j,延迟加载

 

延迟加载(懒加载):
一对一、一对多、多对一、多对多
一对多:班级-学生 ,
如果不采用延迟加载  (立即加载),查询时会将 一 和多 都查询,班级、班级中的所有学生。
如果想要  暂时只查询1的一方,  而多的一方 先不查询 而是在需要的时候再去查询 -->延迟加载

一对一:学生、学生证

mybatis中使用延迟加载,需要先配置:

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

studentMapper.xml中:

<!-- 利用mybaits提供的resultMap实现一对一,使用延迟加载 -->
<select id="queryStudentWith002LazyLoad" parameterType="int" resultMap="student_card_lazyLoad_map">
	<!-- 先查学生 -->
	select * from student2
</select>
<resultMap type="student" id="student_card_lazyLoad_map">
	<!-- 学生信息 -->
	<id property="stuNo" column="stuno"/>
	<result property="stuName" column="stuname"/>
	<result property="stuAge" column="stuage"/>
	<!-- 一对一,对象成员使用association
		此次采用延迟加载,在查询时,并不立即加载学生证(StudentCard)信息 -->
		<!-- 学生证,通过select,需要的时候再查询 -->
	<association property="studentCard" javaType="StudentCard" select="org.lanqiao.mapper.StudentCardMapper.queryCardById" column="cardid">
	</association>
</resultMap>

上面中,我要先查学生信息,再查学生证信息。即studentCard信息需要延迟加载,首先sql语句要有变化,即先查学生的信息,再查学生证的信息,后面关键在于association标签,我们需要在里面写一个select,内容是另一个mapper.xml文件的namespace+id值,所以要新建一个StudentCardMapper.xml文件

<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.lanqiao.mapper.StudentCardMapper">
	<!-- 查询学生证信息 -->
	<select id="queryCardById" parameterType="int" resultType="StudentCard">
		<!-- 查询学生对应的学生证 -->
		select * from student2card where cardid=#{cardId}
	</select>
</mapper>

由于这两张表具有一对一的关联关系,里面的sql语句要添加where语句指定外键cardid。上面一个mapper.xml中column就指定为cardid,这样两者就建立起联系了。

测试可以发现:

Mybatis整合log4j,延迟加载

延迟加载能够实现了。

 

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

再新建一个studentClassMapper文件

<mapper namespace="org.lanqiao.mapper.StudentClassMapper">
	<!-- 一对多  延迟加载 -->
	<select id="queryClassAndStudents"  resultMap="class_student_lazyLoad_map">
		<!-- 先查班级-->	
		select * from studentclass
	</select>
	<resultMap type="studentClass" id="class_student_lazyLoad_map">
		<id property="classId" column="classid"/>
		<result property="className" column="classname"/>
		<!-- 再查班级对应的学生-->
		<collection property="students" ofType="Student" 
		    select="org.lanqiao.mapper.StudentMapper.queryStudentsByClassId" column="classid" >
		</collection>
	</resultMap>
	
	
</mapper>

其实写法和一对一延迟加载写法类似,就是把association改为collection,然后关联字段为classid

studentMapper文件中:

<!-- 一对多,延迟加载需要的:查询班级中的所有学生 -->
<select id="queryStudentsByClassId" parameterType="int" resultType="student">
	select * from student2 where classid=#{classId}
</select>

测试:

Mybatis整合log4j,延迟加载

一对多延迟加载能实现