Mybatis整合log4j、延迟加载

整合log4j

1、导入log4j的jar包
在这里插入图片描述
2、在xml文件中加入log4j的配置

<!-- 如果不指定访问日志顺序,则mybatis按照
SLF4J-> Apache commons Logging ->log4j 2-> log4j-> JDK logging 顺序访问-->
<settings>
	<setting name="logImpl" value="LOG4J"/>
</settings>

3、创建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=%d %p [%c] - %m%n

4、运行程序

延迟加载

1、在conf.xml文件中配置延迟加载的开启

<settings>
	<!-- 开启延迟查询 -->
	<setting name="lazyLoadingEnabled" value="true"/>
	<!-- 关闭直接查询 -->
	<setting name="aggressiveLazyLoading" value="false"/>
</settings>

2、配置对应的xml文件的SQL

	<!-- 进行一对一延迟查询 -->
<select id="queryoflazy" parameterType="int" resultMap="querylazyMap">
	select * from student where id=#{id}
</select>

<resultMap type="com.mybatis.test.Student" id="querylazyMap">
	<id property="id" column="id"></id>
	<result property="student_id" column="student_id"/>
	<result property="student_name" column="student_name"/>
		<!-- javaType:指定该属性的类型 ,select:对应延迟查询的SQL的xml文件路径-,
		 column:两个SQL之间的关联属性->
		<association property="studentcard" javaType="com.mybatis.test.studentCard" select="com.mybatis.test.querystudentlazy.querylazycard" column="cardid">
			<!-- 正常一对一的配置:
			<id property="cardid" column="cardid"/>
			<result property="cardname" column="cardname"/> 
			-->
		</association>
</resultMap>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper  
	PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
	
	<!--  namespace:该mapper.xml的唯一标识符-->
<mapper namespace="com.mybatis.test.querystudentlazy">
	
	<select id="querylazycard" parameterType="int" resultType="studentcard">
		select * from studentcard where cardid=#{cardid}
	</select>
</mapper>

3、在对应的接口中创建对应方法

package com.mybatis.mapper;

import java.util.List;

import com.mybatis.test.Student;
import com.mybatis.test.StudentName;
import com.mybatis.test.studentClass;

public interface StudentMapper {
	Student queryoflazy(int id);
}

4、进行测试

	//进行一对一的延迟查询
	public static void lazyqurey() throws IOException {
		
		Reader read = Resources.getResourceAsReader("config.xml");
		
		SqlSessionFactory sqlsession = new SqlSessionFactoryBuilder().build(read);
		SqlSession session = sqlsession.openSession();
		
		//
//		String statement = "com.mybatis.test.studentMapper.studentSelect";
//		Student stu = session.selectOne(statement, 1);
		
		StudentMapper studentmapper = session.getMapper(StudentMapper.class);
		
		Student stud = studentmapper.queryoflazy(1);
		System.out.println(stud.getStudent_id()+","+stud.getStudent_name());
		
		System.out.println(stud.getStudentcard().getCardid()+","+stud.getStudentcard().getCardname());
		
		session.close();
	}

5、结果(当进行数据的访问的时候,访问一个数据才进行一个数据的查询,从日志的输出就能看出来)

020-05-07 22:30:53,205 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Opening JDBC Connection
2020-05-07 22:30:53,980 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] - Created connection 873610597.
2020-05-07 22:30:53,980 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@34123d65]
2020-05-07 22:30:53,983 DEBUG [com.mybatis.mapper.StudentMapper.queryoflazy] - ==>  Preparing: select * from student where id=? 
2020-05-07 22:30:54,009 DEBUG [com.mybatis.mapper.StudentMapper.queryoflazy] - ==> Parameters: 1(Integer)
2020-05-07 22:30:54,071 DEBUG [com.mybatis.mapper.StudentMapper.queryoflazy] - <==      Total: 1
20205211,Fang
2020-05-07 22:30:54,073 DEBUG [com.mybatis.test.querystudentlazy.querylazycard] - ==>  Preparing: select * from studentcard where cardid=? 
2020-05-07 22:30:54,073 DEBUG [com.mybatis.test.querystudentlazy.querylazycard] - ==> Parameters: 1(Integer)
2020-05-07 22:30:54,075 DEBUG [com.mybatis.test.querystudentlazy.querylazycard] - <==      Total: 1
1,card1
2020-05-07 22:30:54,075 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@34123d65]
2020-05-07 22:30:54,075 DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@34123d65]
2020-05-07 22:30:54,075 DEBUG [org.apache.ibatis.datasource.pooled.PooledDataSource] - Returned connection 873610597 to pool.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值