Mybatis 学习笔记 — 开启日志启用懒加载

Posted Johnny*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis 学习笔记 — 开启日志启用懒加载相关的知识,希望对你有一定的参考价值。

使用log4j日志

开启日志

在 config.xml文件中:

	<settings>
		<!-- 开启日志 -->
		<setting name="logImpl" value="LOG4J"></setting>
	</settings>

注意configuration标签下的子标签有顺序限制:必须是(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)"这个顺序。

log4j.properties

src下创建log4j.properties文件:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

日志级别:
DEBUG<INFO<WARN<ERROR
如果设置为info,则只显示 info及以上级别的信息;
建议:在开发时设置debug,在运行时设置为info或以上。

延迟加载

一对 一

开启延迟加载、关闭立即加载

在这里插入图片描述

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

一对一 懒加载查询

//一对一映射查询 采用懒加载
	public static void queryPersonOrByIdWithIDcard_LazyLoad() throws IOException {
		InputStream in = Resources.getResourceAsStream("config.xml");
		SqlSessionFactory sqlFactory =  new SqlSessionFactoryBuilder().build(in);
		SqlSession session = sqlFactory.openSession();
		PersonMapper personMapper = session.getMapper(PersonMapper.class);
		List<Person> persons = personMapper.queryPersonOrByIdWithIDcard_LazyLoad(null);
		for(Person person: persons) {
			IDcard card = person.getIdCard();//需要card时才查询
			System.out.println(person+" "+card.getCardId()+" "+card.getCardCode());
		}
	}
 <!-- 一对一映射 采用懒加载 -->
	 <select id="queryPersonOrByIdWithIDcard_LazyLoad" parameterType="Integer" resultMap="personWithIDcard_LazyLoad">
	 		<!-- 先查询person  -->
	 		select * from person
	 		<where>
	 			<if test=" id !=null "> id = #{id}</if>
	 		</where>
	 </select>
	 
	 <resultMap id="personWithIDcard_LazyLoad" type="person" >
	 	<id property="id" column="id"/>
	 	<result property="name" column="name"/>
	 	<result property="age" column="age"/>
	 	<result property="sex" column="perSex"/>
	 	<!-- 懒加载 : 后查询 IDcard  通过外键card_id来连接两张表 -->
	 	<association property="idCard"  javaType="IDcard"  select="com.johnny.mapper.IDCardMapper.queryIDcardByCardId" column="card_id" >
	 	</association>
	 </resultMap>

一对多 懒加载

测试类

//一对多关联查询
	public static void queryNationOrByIdWithPerson_LazyLoad() throws IOException {
		InputStream in = Resources.getResourceAsStream("config.xml");
		SqlSessionFactory sqlFactory =  new SqlSessionFactoryBuilder().build(in);
		SqlSession session = sqlFactory.openSession();
		NationMapper nationMapper = session.getMapper(NationMapper.class);
		List<Nation> nations = nationMapper.queryNationOrByIdWithPerson_LazyLoad(1);
		
		for( Nation nation : nations ) {
			System.out.println(nation+ " :");
			for(Person person:  nation.getPersons()) {
				System.out.println(person);
			}
		}
	}

nationMapper.xml

<?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">
 
<mapper namespace="com.johnny.mapper.NationMapper">  <!-- 该mapper的命名空间要为对应接口的全类名 -->
	<!-- 一对多映射 采用懒加载-->
	<select id="queryNationOrByIdWithPerson_LazyLoad" parameterType="Integer" resultMap="personWithNation_LazyLoad">
		select * from nation
		<where>
			<if test="nationId != null "> nation_Id = #{nationId}</if>
		</where>
	</select>
	<resultMap id="personWithNation_LazyLoad" type="nation">
		<id property="nationId"  column="nation_id" />
		<result property="nationName" column="nation_name" />
		<!-- 属性类型则使用jdbcType   属性的元素类型则使用ofType -->
		<collection property="persons" ofType="person" select="com.johnny.mapper.PersonMapper.queryPersonByNationId" column="nation_id">
		</collection>
	</resultMap>

</mapper>

personMapper.xml

<?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">
 
<mapper namespace="com.johnny.mapper.PersonMapper">  <!-- 该mapper的命名空间要为对应接口的全类名 -->
	
	 <select id="queryPersonByNationId" parameterType="int"  resultType="person"> 
	 	select * from person where nation_id=#{nation_id}
	 </select>
</mapper>

以上是关于Mybatis 学习笔记 — 开启日志启用懒加载的主要内容,如果未能解决你的问题,请参考以下文章

mybatis学习笔记-04

mybatis学习笔记-04

mybatis学习笔记-04

Mybatis懒加载机制

mybatis分页插件以及懒加载

Mybatis学习第25节 -- 懒加载 积极与不积极