mybatis中延迟加载Lazy策略
2017-08-10 22:21
357 查看
延迟加载:
lazy策略原理:只有在使用查询sql返回的数据是才真正发出sql语句到数据库,否则不发出(主要用在多表的联合查询)
1.一对一延迟加载:
假设数据库中有person表和card表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;
假设要查询某个人的姓名和身份证号码:
原理:在查询姓名时,实际本没有查询出身份证号码的信息,只有当前台使用身份证号时才发出对card的查询,需要查询出身份证号码是采取查询的一种策略;
实现实例:
实现步骤:
1-导入mybatis 的依赖jar包
2-添加log4j文件 (可查看内存中实际执行的程序)
1-原理:只有当前台使用身份证号时才发出对card的查询,否则只发出person信息的查询
2-开启lazy:在conf.xml
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
3.实现:
(1)在mapper.xml映射文件中:
<select id="findCid" parameterType="int" resultType="card">
select * from card where cid=#{value}
</select>
<resultMap type="person" id="p_c1">
<id column="pid" property="pid" />
<result column="pname" property="pname" />
<result column="page" property="page" />
<result column="psex" property="psex" />
<association property="card" javaType="card" select="findCid"
column="cid">
</association>
</resultMap>
<select id="selectpersonAndCardLazyByPid" parameterType="int"
resultMap="p_c1">
SELECT * FROM person where pid=#{value}
</select> 1-select:指定关联的查询语句
2-column:指定主语句中的哪个字段的值作为参数传递给从sql语句
(2)在mapper接口中定义方法:
public Person selectpersonAndCardLazyByPid(int pid);
(3)使用junit测试结果:
1.此处是只发出person信息的查询;
@Test
public void testselectpersonAndCardLazyByPid(){//lazy策略一对1
Person p=pm.selectpersonAndCardLazyByPid(1);
//System.out.println(p);
System.out.println(p.getPname()+",");
//System.out.println(p.getPname()+","+p.getCard().getCnum());
}结果执行的查询语句:
![](https://img-blog.csdn.net/20170810224049392?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTBfMzc5MDU0Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2.当前台使用身份证号时才发出对card的查询
@Test
public void testselectpersonAndCardLazyByPid(){//lazy策略一对1
Person p=pm.selectpersonAndCardLazyByPid(1);
//System.out.println(p);
System.out.println(p.getPname()+",");
System.out.println(p.getPname()+","+p.getCard().getCnum());//当前台使用身份证号时才发出对card的查询
}结果执行的查询语句:
![](https://img-blog.csdn.net/20170810224237106?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTBfMzc5MDU0Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2.一对多延迟加载:
实现实例:
假设数据库中有person表和card身份信息表,adder地址表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;adder表有字段aid,ashi,pid
假设要查询某个人的姓名和住址,身份证号码:
(1)mapper.xml映射文件:
<!-- lazy策略一对多 -->
<select id="fingCard_Adder" parameterType="int" resultType="adder">
select * from adder where pid=#{value}
</select>
<select id="findCid1" parameterType="int" resultType="card">
select * from card where cid=#{value}
</select>
<resultMap type="person" id="p_c1_a1">
<id column="pid" property="pid" />
<result column="pname" property="pname" />
<result column="page" property="page" />
<result column="psex" property="psex" />
<association property="card" javaType="card" select="findCid1"
column="cid">
</association>
<collection property="adder" ofType="Adder" select="fingCard_Adder"
column="pid">
</collection>
</resultMap>
<select id="selectpersonAndCardAndAdderLazyByPid" parameterType="int"
resultMap="p_c1_a1">
SELECT * FROM person where pid=#{value}
</select>(2)mapper接口定义方法:
1.此处是只发出person信息的查询;
@Test
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一对多
Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");////此处是只发出person信息的查询
}
结果执行的查询语句:
![](https://img-blog.csdn.net/20170810225528564?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTBfMzc5MDU0Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2.此处是发出person信息和身份信息的查询;
@Test
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一对多
Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");//此处是只发出person信息的查询;
System.out.println(p.getPname()+","+p.getCard().getCnum());//此处是发出person信息和身份信息的查询;
}结果执行的查询语句:
![](https://img-blog.csdn.net/20170810225828506?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTBfMzc5MDU0Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3.此处发出person信息和身份信息,地址信息的查询;
@Test
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一对多
Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");//此处是只发出person信息的查询;
System.out.println(p.getPname()+","+p.getCard().getCnum());//此处是发出person信息和身份信息的查询;
//System.out.println(p.getPname()+","+p.getCard().getCnum());
for (Adder adder : p.getAdder()) {////此处发出person信息和身份信息,地址信息的查询;
System.out.println(adder.getAshi());
}
}结果执行的查询语句:
lazy策略原理:只有在使用查询sql返回的数据是才真正发出sql语句到数据库,否则不发出(主要用在多表的联合查询)
1.一对一延迟加载:
假设数据库中有person表和card表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;
假设要查询某个人的姓名和身份证号码:
原理:在查询姓名时,实际本没有查询出身份证号码的信息,只有当前台使用身份证号时才发出对card的查询,需要查询出身份证号码是采取查询的一种策略;
实现实例:
实现步骤:
1-导入mybatis 的依赖jar包
2-添加log4j文件 (可查看内存中实际执行的程序)
1-原理:只有当前台使用身份证号时才发出对card的查询,否则只发出person信息的查询
2-开启lazy:在conf.xml
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
3.实现:
(1)在mapper.xml映射文件中:
<select id="findCid" parameterType="int" resultType="card">
select * from card where cid=#{value}
</select>
<resultMap type="person" id="p_c1">
<id column="pid" property="pid" />
<result column="pname" property="pname" />
<result column="page" property="page" />
<result column="psex" property="psex" />
<association property="card" javaType="card" select="findCid"
column="cid">
</association>
</resultMap>
<select id="selectpersonAndCardLazyByPid" parameterType="int"
resultMap="p_c1">
SELECT * FROM person where pid=#{value}
</select> 1-select:指定关联的查询语句
2-column:指定主语句中的哪个字段的值作为参数传递给从sql语句
(2)在mapper接口中定义方法:
public Person selectpersonAndCardLazyByPid(int pid);
(3)使用junit测试结果:
1.此处是只发出person信息的查询;
@Test
public void testselectpersonAndCardLazyByPid(){//lazy策略一对1
Person p=pm.selectpersonAndCardLazyByPid(1);
//System.out.println(p);
System.out.println(p.getPname()+",");
//System.out.println(p.getPname()+","+p.getCard().getCnum());
}结果执行的查询语句:
2.当前台使用身份证号时才发出对card的查询
@Test
public void testselectpersonAndCardLazyByPid(){//lazy策略一对1
Person p=pm.selectpersonAndCardLazyByPid(1);
//System.out.println(p);
System.out.println(p.getPname()+",");
System.out.println(p.getPname()+","+p.getCard().getCnum());//当前台使用身份证号时才发出对card的查询
}结果执行的查询语句:
2.一对多延迟加载:
实现实例:
假设数据库中有person表和card身份信息表,adder地址表:其中person表中有字段pid,pname,page,psex,cid,card表中有字段cid,cnum;adder表有字段aid,ashi,pid
假设要查询某个人的姓名和住址,身份证号码:
(1)mapper.xml映射文件:
<!-- lazy策略一对多 -->
<select id="fingCard_Adder" parameterType="int" resultType="adder">
select * from adder where pid=#{value}
</select>
<select id="findCid1" parameterType="int" resultType="card">
select * from card where cid=#{value}
</select>
<resultMap type="person" id="p_c1_a1">
<id column="pid" property="pid" />
<result column="pname" property="pname" />
<result column="page" property="page" />
<result column="psex" property="psex" />
<association property="card" javaType="card" select="findCid1"
column="cid">
</association>
<collection property="adder" ofType="Adder" select="fingCard_Adder"
column="pid">
</collection>
</resultMap>
<select id="selectpersonAndCardAndAdderLazyByPid" parameterType="int"
resultMap="p_c1_a1">
SELECT * FROM person where pid=#{value}
</select>(2)mapper接口定义方法:
1.此处是只发出person信息的查询;
@Test
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一对多
Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");////此处是只发出person信息的查询
}
结果执行的查询语句:
2.此处是发出person信息和身份信息的查询;
@Test
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一对多
Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");//此处是只发出person信息的查询;
System.out.println(p.getPname()+","+p.getCard().getCnum());//此处是发出person信息和身份信息的查询;
}结果执行的查询语句:
3.此处发出person信息和身份信息,地址信息的查询;
@Test
public void testselectpersonAndCardAndAdderLazyByPid(){//lazy策略一对多
Person p=pm.selectpersonAndCardAndAdderLazyByPid(1);
System.out.println(p.getPname()+",");//此处是只发出person信息的查询;
System.out.println(p.getPname()+","+p.getCard().getCnum());//此处是发出person信息和身份信息的查询;
//System.out.println(p.getPname()+","+p.getCard().getCnum());
for (Adder adder : p.getAdder()) {////此处发出person信息和身份信息,地址信息的查询;
System.out.println(adder.getAshi());
}
}结果执行的查询语句:
相关文章推荐
- mybatis中延迟加载Lazy策略的方法
- Mybatis延迟加载问题( Cannot enable lazy loading because CGLIB is not available)
- Hibernate中集合的延迟加载(lazy)和抓取策略(fetch)
- Mybatis中的延迟加载案例解析
- jQuery Lazy Load 图片延迟加载
- mybatis 延迟加载
- 移动端图片居多,加载过慢,使用延迟加载|懒加载( lazyload.js)
- 测试mybatis延迟加载错误与解决方法
- 【Hibernate】Hibernate性能优化之延迟加载与抓取策略
- C# 4.0 延迟加载 Lazy<>用法
- 关于延迟加载(lazy)和强制加载(Hibernate.initialize(Object proxy) )
- mybatis--()延迟加载
- jquery.lazyload 实现图片延迟加载jquery插件(转自http://www.jb51.net/article/21987.htm)
- 关于延迟加载(lazy)和强制加载(Hibernate.initialize(Object proxy) ) 等
- jQuery图片延迟加载插件jQuery.lazyload
- 关于图片延迟加载技术-ImageLazyLoad
- mybatis(八) mybatis查询缓存 延迟加载 一级缓存和二级缓存原理 mybatis整合ehcache
- Mybatis其他-延迟加载
- jquery.lazyload.js实现图片延迟加载——wordpress图片随滚动条渐显效果
- MyBatis 延迟加载,一级缓存,二级缓存设置