Mybatis的延迟加载又称为懒加载

mybatis在一对多的查询中,例如查询一个用户时需要查询这个用户下的所有账户信息,如果一次性的select * from user u left join account a on u.id=a.UID 会占用大量的内存 这里就可以使用延迟加载。

延迟加载配置:

UserDao.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.hdh.dao.UserDao"> <!-- 定义User的resultMap -->
<resultMap id="userAccountMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="birthday" column="birthday"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<!-- 配置user对象中accounts集合的映射 -->
<collection property="accounts" ofType="account"
select="com.hdh.dao.AccountDao.findAccountByUid" column="id">
</collection>
</resultMap>
  <select id="selectAll" resultMap="userACcountMap">
  SELECT * FROM user
  </select>
</mapper>

UserDao.java

/**
* 用户的持久层接口
*
* @author Administrator
*
*/
public interface UserDao {
// 查询所有用户
List<User> findAll(); }

AccountDao.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.hdh.dao.AccountDao"> <!-- 根据用户id查询账户列表 -->
<select id="findAccountByUid" resultType="account">
select * from account where uid = #{uid}
</select> </mapper>

AccountDao.java

public interface AccountDao {
/**
* 根据用户id查询账户信息
* @param uid
* @return
*/
List<Account> findAccountByUid(Integer uid); }

UserTest.java

public class UserTest {

    private InputStream in;
private SqlSession sqlSession;
private UserDao userDao; @Before//用于在测试方法执行之前执行
public void init()throws Exception{
//1.读取配置文件,生成字节输入流
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.获取SqlSessionFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//3.获取SqlSession对象
sqlSession = factory.openSession(true);
//4.获取dao的代理对象
userDao = sqlSession.getMapper(UserDao.class);
} @After//用于在测试方法执行之后执行
public void destroy()throws Exception{
//提交事务
// sqlSession.commit();
//6.释放资源
sqlSession.close();
in.close();
} /**
* 测试查询所有
*/
@Test
public void testFindAll(){
List<User> users = userDao.findAll();
/*for(User user : users){
System.out.println("-----每个用户的信息------");
System.out.println(user);
System.out.println(user.getAccounts());
}*/
}
}

TZ_02MyBatis_lazy SqlMapConfig.xml的更多相关文章

  1. mybatis入门基础(三)----SqlMapConfig.xml全局配置文件解析

    一:SqlMapConfig.xml配置文件的内容和配置顺序如下 properties(属性) settings(全局配置参数) typeAiases(类型别名) typeHandlers(类型处理器 ...

  2. MyBatis学习--SqlMapConfig.xml配置文件

    简介 SqlMapConfig.xml是MyBatis的全局配置文件,在前面的文章中我们可以看出,在SqlMapConfig.xml主要是配置了数据源.事务和映射文件,其实在SqlMapConfig. ...

  3. Mybatis学习记录(三)----理解SqlMapConfig.xml文件

    SqlMapConfig.xml mybatis的全局配置文件SqlMapConfig.xml,配置内容如下: properties(属性) settings(全局配置参数) typeAliases( ...

  4. sqlMapConfig.xml配置文件详解

    sqlMapConfig.xml配置文件详解: Xml代码 Xml代码  <? xml version="1.0" encoding="UTF-8" ?& ...

  5. 五 mybatis的SqlMapConfig.xml详解

    SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE conf ...

  6. mybatis11 sqlMapConfig.xml文件说明

    1sqlMapConfig.xml SqlMapConfig.xml中配置的内容和顺序如下: properties(属性) settings(全局配置参数) typeAliases(类型别名) typ ...

  7. SqlMapConfig.xml全局配置文件解析

    一:SqlMapConfig.xml配置文件的内容和配置顺序如下 properties(属性) settings(全局配置参数) typeAiases(类型别名) typeHandlers(类型处理器 ...

  8. mybatis系列笔记(3)---SqlMapConfig.xml解析

    SqlMapConfig.xml SqlMapConfig.xml是Mybatis的全局配置参数,关于他的具体用的有专门的MyBatis - API文档,这里面讲的非常清楚,所以我这里就挑几个讲下: ...

  9. 02_MyBatis项目结构,所需jar包,ehcache.xml配置,log4j.properties,sqlMapConfig.xml配置,SqlMapGenerator.xml配置

     项目结构(所需jar包,配置文件) sqlMapConfig.xml的配置内容如下: <?xmlversion="1.0"encoding="UTF-8&qu ...

随机推荐

  1. JS规则 编程练习 考考大家的数学,计算以下计算公式的结果。然后在浏览器中运行一下,看看结果是否跟你的结果一致。

    编程练习 考考大家的数学,计算以下计算公式的结果.然后在浏览器中运行一下,看看结果是否跟你的结果一致. 任务 第一步: 在  ? 处填写你的答案. 第二步: 填写完成后,运行一下,看看是不是跟你填写的 ...

  2. 74CMS漏洞打包(从老博客转)

    引子 这套CMS是上个月中做的审计,总共找到几个后台漏洞,可后台getshell,一个逻辑漏洞可任意发短信,还有一个前台注入漏洞.不过发到了某平台上之后,审核又要求我提交利用的poc,所以懒得发去了, ...

  3. LeetCode第一题—— Two Sum(寻找两数,要求和为target)

    题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...

  4. 虚拟机上CentOS7 配置NAT模式

    1. 虚拟机网络适配器选择NAT模式 2. 使用vi编辑/etc/sysconfig/network-scripts/ifcfg-ens32 vi /etc/sysconfig/network-scr ...

  5. leetcode-213-打家劫舍二

    题目描述: 方法一: class Solution(object): def rob(self, nums): """ :type nums: List[int] :rt ...

  6. javascript 数组的方法(一)

    栈方法(后进先出) ArrayObj.push():就是向数组末尾添加新的元素,返回的是数组新的长度. ArrayObj.pop():就是向数组中删除数组最后一个元素并且返回该元素.如果数组为空就返回 ...

  7. Java-Druid:Druid

    ylbtech-Java-Druid:Druid Apache Druid(孵化)是一个高性能的实时分析数据库. 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 ...

  8. CentOS源码安装Wireshark

    (2019年2月19日注:这篇文章原先发在自己github那边的博客,时间是2016年8月25日) Wireshark为网络管理员常用的一个网络管理工具,通过使用这个软件,我们可以对本机网卡上的经过的 ...

  9. IDEA中Git的使用(多人合作)

    首先我们要简单知道github跟Git的区别.git是版本控制工具, github是一个面向开源及私有软件项目的托管平台,也是程序员交流的地方. 接下来就开始讲怎么多人一起开发. 首先我们先拥有git ...

  10. 简单DP (Preparing for Xtreme 12.0) | STL map使用

    当水题遇上了map大坑 晚上写一个dp,弄了半天样例一直不过,对着队友的代码一行行看,发现跟自己逻辑完全一样啊... 然后就逐行输出比对,发现预处理出了问题,把map插入新值的地方改了下,果然就好了. ...