association
使用association定义关联的单个对象的封装规则
1、mapper接口
Account selectByResultMap(Integer id);
2、映射文件
property指定那个对象是联合的对象, javaType指定联合对象的类型(不能省略)
<resultMap id="resultMapTest" type="com.caq.study.entity.Account"> <id column="id" property="id"></id> <result column="name" property="name"></result> <result column="money" property="money"></result> <association property="dept" javaType="com.caq.study.entity.Dept"> <id column="id" property="id"></id> <result column="dept_name" property="departName"></result> </association> </resultMap> <select id="selectByResultMap" resultMap="resultMapTest"> SELECT account.`id`, account.`name`, account.`money`, depart.`id`, depart.`dept_name` FROM `account` LEFT JOIN `depart` ON account.d_id = depart.id WHERE account.id = #{id} </select>
3、测试
@Test public void testResultMap() throws IOException { SqlSession sqlSession = getSqlSessionFactory().openSession(); AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class); Account account = accountMapper.selectByResultMap(1); System.out.println(account); sqlSession.commit(); sqlSession.close(); } Account(id=1, name=tom, money=5000.00, dept=Dept(id=1, departName=开发部))
上面这种查询方式可以理解为嵌套查询
分布查询
相当于查询account表的所有数据,查询到所有数据后,
再把和Dept有关联的字段传给DeptMapper接口
通过这种方式实现分布查询
mapper接口: Account selectAssociate(Integer id); 映射文件: <resultMap id="AsscociateTest" type="com.caq.study.entity.Account"> <id column="id" property="id"></id> <result column="name" property="name"></result> <result column="money" property="money"></result> <association property="dept" select="com.caq.study.mapper.DepartMapper.getDeptById" column="d_id"> </association> </resultMap> <select id="selectAssociate" resultMap="AsscociateTest"> SELECT * from account where id = #{id} </select>
mapper接口: Dept getDeptById(Integer id); 映射文件: <select id="getDeptById" resultType="com.caq.study.entity.Dept"> select id,dept_name departName from depart where id = #{id} </select>
测试
@Test public void testAssociate() throws IOException { SqlSession sqlSession = getSqlSessionFactory().openSession(); AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class); Account account = accountMapper.selectAssociate(1); System.out.println(account); System.out.println(account.getDept()); sqlSession.commit(); sqlSession.close(); }
Account(id=1, name=tom, money=5000.00, dId=null, dept=Dept(id=1, departName=开发部)) Dept(id=1, departName=开发部)
延迟加载
上面的我们写的查询有什么问题呢?
就是部门信息我们无论需不需要它都会查询,这样很浪费资源
怎么解决这个问题呢?
==延迟加载!==
分段查询的基础上在mybatis主配置文件加上两个配置
显示的指定每个我们需要更改的配置的值,即使他是默认的
log4j
做延迟加载前,为了看效果我们加上log4j查看控制台的日志
1、使用Log4j需要导入相关的jar包 (这里是Maven中的依赖包):
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
2、使用log4j
<settings> <!--标准日志工厂实现--> <!--<setting name="logImpl" value="STDOUT_LOGGING"/>--> <!---Log4j工厂实现--> <setting name="logImpl" value="LOG4J"/> </settings>
3、log4j.properties
log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=./log/Gosion.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=5 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
测试延迟加载
==未开启==
==开启==
<settings> <setting name="logImpl" value="LOG4J"/> <setting name="jdbcTypeForNull" value="NULL"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings>