一、下载log4j.jar导入项目。
在conf.xml中开启日志:
<settings> <setting name="logImpl" value="LOG4J"/> </settings>
创建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
运行测试类:
二、延迟加载
在personMapper.xml中添加:
<!-- 使用resultMap关联查询,延迟加载 --> <select id="queryPerson_cardId4" resultMap="person_card_map2_lazyload"> select * from person2 </select> <resultMap type="person" id="person_card_map2_lazyload"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <result property="persex" column="sex"/> <!-- 一对一用association,采用延迟加载修改以下 --> <association property="card" javaType="PersonCard" select="org.ruangong.mapper.personcardMapper.querycardById" column="cardId"> <!-- <id property="cardId" column="cardId"/> <result property="cardInfo" column="cardInfo"/> --> </association> </resultMap>
并在原mapper包中创建personcardMapper.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="org.ruangong.mapper.personcardMapper"> <!-- 使用resultMap关联查询,延迟加载 --> <select id="querycardById" parameterType="int" resultType="PersonCard"> select * from person_card where cardId = #{cardId} </select> </mapper>
在conf.xml的mappers添加映射:
<mapper resource="org/ruangong/mapper/personcardMapper.xml"/>
在有开启log4j日志的setting标签中加入:
<!-- 开启延迟加载 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 关闭立即加载 --> <setting name="aggressiveLazyLoading" value="false"/>
实现延迟加载。