TZ_02MyBatis_lazy SqlMapConfig.xml
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的更多相关文章
- mybatis入门基础(三)----SqlMapConfig.xml全局配置文件解析
一:SqlMapConfig.xml配置文件的内容和配置顺序如下 properties(属性) settings(全局配置参数) typeAiases(类型别名) typeHandlers(类型处理器 ...
- MyBatis学习--SqlMapConfig.xml配置文件
简介 SqlMapConfig.xml是MyBatis的全局配置文件,在前面的文章中我们可以看出,在SqlMapConfig.xml主要是配置了数据源.事务和映射文件,其实在SqlMapConfig. ...
- Mybatis学习记录(三)----理解SqlMapConfig.xml文件
SqlMapConfig.xml mybatis的全局配置文件SqlMapConfig.xml,配置内容如下: properties(属性) settings(全局配置参数) typeAliases( ...
- sqlMapConfig.xml配置文件详解
sqlMapConfig.xml配置文件详解: Xml代码 Xml代码 <? xml version="1.0" encoding="UTF-8" ?& ...
- 五 mybatis的SqlMapConfig.xml详解
SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE conf ...
- mybatis11 sqlMapConfig.xml文件说明
1sqlMapConfig.xml SqlMapConfig.xml中配置的内容和顺序如下: properties(属性) settings(全局配置参数) typeAliases(类型别名) typ ...
- SqlMapConfig.xml全局配置文件解析
一:SqlMapConfig.xml配置文件的内容和配置顺序如下 properties(属性) settings(全局配置参数) typeAiases(类型别名) typeHandlers(类型处理器 ...
- mybatis系列笔记(3)---SqlMapConfig.xml解析
SqlMapConfig.xml SqlMapConfig.xml是Mybatis的全局配置参数,关于他的具体用的有专门的MyBatis - API文档,这里面讲的非常清楚,所以我这里就挑几个讲下: ...
- 02_MyBatis项目结构,所需jar包,ehcache.xml配置,log4j.properties,sqlMapConfig.xml配置,SqlMapGenerator.xml配置
项目结构(所需jar包,配置文件) sqlMapConfig.xml的配置内容如下: <?xmlversion="1.0"encoding="UTF-8&qu ...
随机推荐
- 16进制与utf-8
很多人将数据的存储.传输方式和展现形式混为一谈. 类似的16进制 2进制是讲内容在电脑里面的存储或者传输的一种格式, 而utf-8 gb2312 等是输出的展现的一种格式 不是一回事,另外 gbk包含 ...
- ThinkPHP 删除数据
ThinkPHP删除数据使用delete方法,例如: 直线电机价格 $Form = M('Form'); $Form->delete(5); 表示删除主键为5的数据,delete方法可以删除单个 ...
- input判断输入值是否合法
1.判断input输入的值是否合法有很多办法,我这里使用的是在onchange时进行判断,代码如下:[所有主要浏览器都支持] <input type="text" name= ...
- NFS+mou
前言 有两台电脑,Linux操作系统,服务器和客户端,IP不同,但是可以相互访问. 客户端想访问服务器的文件系统 准备工作 假设 服务器的ip为 192.168.0.100,要分享为公共文件夹的目录为 ...
- linux系统添加定时任务
http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html
- eclipse安装m2e
Installation You can install last M2Eclipse release by using the following update site from within E ...
- Redis系列一之《Redis设计与实践》整体观感
笔者别的Redis方面的书没有读过,读完这一本,力荐,作者黄建宏,对Redis不太熟悉的,但是对编程稍微有些基础的,全部 读下来应该无压力.作者的编写和讲解非常详细,覆盖的面基本上都讲到,之前一直都是 ...
- 04_jQuery对象初识(三)
<div id="d1"> <p><span>span</span></p> <div>div</di ...
- <scrapy爬虫>爬取腾讯社招信息
1.创建scrapy项目 dos窗口输入: scrapy startproject tencent cd tencent 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) # - ...
- Liunx常用命令行(Ubuntu)
关闭防火墙的命令行: 1. 永久性生效 开启:chkconfig iptables on 关闭:chkconfig iptables off 2. 即时生效,重启后失效 开启:service ipta ...