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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值