resultMap可以实现高级映射(使用association、collection实现一对一及一对多映射),association、collection具备延迟加载功能。

需求:

如果查询订单并且关联查询用户信息。如果先查询订单信息即可满足要求,当我们需要查询用户信息时再查询用户信息。把对用户信息的按需去查询就是延迟加载。

延迟加载:先从单表查询、需要时再从关联表去关联查询,大大提高 数据库性能,因为查询单表要比关联查询多张表速度要快。

接下来我们用assocation来实现延迟加载:

先给出需求:

查询订单并且关联查询用户信息

老规矩:1.sql语句,2.pojo类,3.mapper.xml和mapper.java接口

第一步:

1。我们要实现延时加载,相当于先查订单(orders)的信息,等需要用到user的信息时再去查询User表。

也就是把

  <select id="findOrdersUserLazyLoading" resultMap="OrdersUserLazyLoadingResultMap">
Select * from orders;
</select>
  <select id="finduserByid" parameterType="int" resultType="cn.itcast.mybatis.po.User">
SELECT * FROM USER WHERE ID=#{VALUE}
</select>

上面两条select语句用延迟加载的方式实现。(用resultMap来连接这两条select语句)

第二步:

2.pojo类:

package cn.itcast.mybatis.po;

import java.util.Date;
import java.util.List; public class Orders {
private Integer id;
private Integer user_id;
private String number;
private Date createtime;
private String note;
//用户信息,新增了一个User属性,为了保存查询得到的关联的User表的信息(一对一)
private User user; public List<Orderdetail> getOrderdetail() {
return orderdetails;
}
public void setOrderdetail(List<Orderdetail> orderdetail) {
this.orderdetails = orderdetail;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
} }

第三步:

3.编写OrderMapperCustom.xml代码:使用association中的select指定延迟加载去执行的statement的id。

 <resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserLazyLoadingResultMap">这句代码的作用就是用来连接上面那两条select语句来实现延迟加载。
<?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">
<!-- nanmespace:命名空间。 作用就是对sql进行分类话管理,理解Sal分离 注意:使用mapper代理方式,namespace有特殊重要的作用
--> <mapper namespace="cn.itcast.mybatis.mapper.OrdersMapperCustom"> <!-- 延迟加载的resultMap -->
<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserLazyLoadingResultMap">
<!--对订单信息进行映射配置 --> <resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserLazyLoadingResultMap">
<id column="id" property="id"/>
<result column="user_id" property="user_id"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/> <!-- 实现对用户信息进行延迟加载
select:指定延迟加载需要执行的statement的id(是根据user_id查询用户信息的statement)
要使用userMapper.xml中findUserById完成根据用户id(user_id)用户信息的查询,如果findUserById不在本mapper中需要前边加namespace
column:订单信息中关联用户信息查询的列,是user_id
关联查询的sql理解为:
SELECT orders.*,
(SELECT username FROM USER WHERE orders.user_id = user.id)username,
(SELECT sex FROM USER WHERE orders.user_id = user.id)sex
FROM orders
--> <association property="user" javaType="cn.itcast.mybatis.po.User"
select="finduserByid" column="user_id"
>
</association>
</resultMap> <select id="finduserByid" parameterType="int" resultType="cn.itcast.mybatis.po.User">
SELECT * FROM USER WHERE ID=#{VALUE}
</select> <select id="findOrdersUserLazyLoading" resultMap="OrdersUserLazyLoadingResultMap">
Select * from orders;
</select> </mapper>

编写OrdersMapperCustom.java接口:

package cn.itcast.mybatis.mapper;

import java.util.List;

import cn.itcast.mybatis.po.Orders;
import cn.itcast.mybatis.po.OrdersCustom;
import cn.itcast.mybatis.po.User; public interface OrdersMapperCustom {
//函数的名字OrdersMapperCustom.xml中select中的id名一样 public List<Orders> findOrdersUserLazyLoading();
}

第四步:

不要忘记修改总的配置文件,setting 加上延迟加载的功能

延迟加载配置

mybatis默认没有开启延迟加载,需要在SqlMapConfig.xml中setting配置。

在mybatis核心配置文件中配置:

lazyLoadingEnabled、aggressiveLazyLoading

设置项

描述

允许值

默认值

lazyLoadingEnabled

全局性设置懒加载。如果设为‘false’,则所有相关联的都会被初始化加载。

true | false

false

aggressiveLazyLoading

当设置为‘true’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。

true | false

true

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhMAAACFCAIAAABuYe0EAAAZi0lEQVR4nO2dPW/cxtbHeT/Are93UB2o2Ga/Qaq0wULAQrVbF8YSARZQ90jIXcgpXBnYBKAEF45VJMU+FAQ4MKyoElOosNQIgrYTXHtuwRU5b+fMzPJ1pf8Pg0DLGQ7PDIfnP2eGdCIBAAAAhBB1bQAAAIANow7lOL8cjhbT8xpqAgAA0H/qUI7b63G7yjEcLajjVAqt37Men5rlMqGWhNqzHkU9zIXky2ktshrTtM0AgA4JVI7b6/Ho8sxysD3lyF2P1QExilL8YXVklGfUrmU6R94A+XTG7CCohjs9/hr6x1hL9RVVgCpWidvr8ejT/La2+gAAnoQox/nlcLQYv/+qH29ROfj5u+c8l3LiPlPv0Gs5bXbiGQcEtY63MChWYJSjhZjj7PViiJVSAFrHVzlu3n8iH9G2lMNzjs/XwIuBz0U9C4RO/Pn65RrMn1TT5AuZQQBfjPnb2nymRfWqhUY+Mi0TGgBAY3gpx9nrxZBZFmh+0YCf8/p4T83tav8tTndW4l/Gap72x3pdwR83FcV5dR+1MDvZrGqN/qmH2+vxaDF8fV9ztQAAAqdyfJ2/WgzNvQ2F+6lFOe6nsrN4dX3jlcXh9EHOaS/zX8qjeU6lKU8qbOIR6jqt4meVKKaLGMcdVI95io/9riLVR8v9NGQsAQCqUEfMYVeOBuHn3VSUIFQnLv+0OlNP12nWaf3D/K9pRi3wncP/4fm3YLtaLtN4tJGDmAOAdqljn6N1nMpBHaSm2/w03DxC6RBV0mkhH9nwyb8M0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MCTx6kcy/3D2LH1bd0b13yxVOF8pu5ja266duWQZEn+qWapVpHG61fcjePo8PTKWRAAAJ4Q1WMO+mXcZTo2V5ZENrX9z1/bVw4u5iCNlwsg5gAAPFMq73P47I0v03Hpfy3+XaFV5bDscLDGF5eiXxYAAICnTtV3q06OfebdylqQus9hlk3HlB9nsoQIVI7lfOazHqUbL/BuFQDg2VPxew7b3rgQwpjRa3N2/aWmJKNymROLLOnVKb1CJuZQ3u9i3/JSbMD3HACAZ0+lb8ivTg83dn/YCFAcAQ0AAIAVVZSj63+oqgqGTtyks+FkNt/M1gAAQJs8438rV1+t6v7rdAAA2AiesXIAAABYCygHAACAMKAcAAAAwvgfet2VsBcjSJMAAAAASUVORK5CYII=" alt="" />

在SqlMapConfig.xml中代码为:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <!-- properties的配置必须写在typeAliases的上面 -->
<properties resource="db.properties"></properties> <!-- 配置懒加载设置 -->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings> <!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理-->
<transactionManager type="JDBC" />
<!-- 数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource> </environment> </environments> <!-- 把映射文件(sqlmap/user.xml)加载进sqlMapConfig.xml-->
<mappers> <!-- 把mapper.xml加载进sqlMapConfig.xml-->
<!--
遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致
且在一个目录中。
当着这个规范的前提是:使用mapper代理方法。 -->
<mapper class="cn.itcast.mybatis.mapper.OrdersMapperCustom"/> </mappers> </configuration>

第五步:

编写测试代码:

测试思路:

1、执行上边mapper方法(findOrdersUserLazyLoading),内部去调用cn.itcast.mybatis.mapper.OrdersMapperCustom中的findOrdersUserLazyLoading只查询orders信息(单表)。

2、在程序中去遍历上一步骤查询出的List<Orders>,当我们调用Orders中的getUser方法时,开始进行延迟加载。

3、延迟加载,去调用UserMapper.xml中findUserbyId这个方法获取用户信息。

测试代码Mybatis_mappertest.java如下:

package cn.itcast.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test; import cn.itcast.mybatis.mapper.OrdersMapperCustom;
import cn.itcast.mybatis.mapper.userMapper;
import cn.itcast.mybatis.po.Orders;
import cn.itcast.mybatis.po.User;
import cn.itcast.mybatis.po.UserCustom;
import cn.itcast.mybatis.po.UserQueryVo; public class Mybatis_mappertest { private SqlSessionFactory sqlSessionFactory;
@Before
public void setup() throws IOException
{ String resource="SqlMapConfig.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
//主要是生成SqlsessionFactory。
this.sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testMaper()
{
SqlSession sqlSession=null; sqlSession=sqlSessionFactory.openSession();
//生成代理类
OrdersMapperCustom orderMapper=sqlSession.getMapper(OrdersMapperCustom.class);
//创建包装对象,设置查询条件
//orderMapper.findOrdersUser(); @SuppressWarnings("unused") List<Orders> list=orderMapper.findOrdersUserLazyLoading();
for(Orders orders:list) {//只有执行了这一步才会执行延迟加载。
User user=orders.getUser();
System.out.println(user); } } }

运行结果如下:

DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@30647e13]
DEBUG [main] - ==>  Preparing: Select * from orders;
DEBUG [main] - ==> Parameters:
DEBUG [main] - <==      Total: 3
DEBUG [main] - ==>  Preparing: SELECT * FROM USER WHERE ID=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 1
cn.itcast.mybatis.po.User@1ff90029

我们看一下上面的结果:第一条sql语句:Select * from orders;

然后才会有:SELECT * FROM USER WHERE ID=?

实现了延迟加载、

24Mybatis_延迟加载——用association来实现的更多相关文章

  1. mybatis0206 延迟加载

    延迟加载 1.1使用延迟加载意义 在进行数据查询时,为了提高数据库查询性能,尽量使用单表查询,因为单表查询比多表关联查询速度要快. 如果查询单表就可以满足需求,一开始先查询单表,当需要关联信息时,再关 ...

  2. MyBatis从入门到放弃六:延迟加载、一级缓存、二级缓存

    前言 使用ORM框架我们更多的是使用其查询功能,那么查询海量数据则又离不开性能,那么这篇中我们就看下mybatis高级应用之延迟加载.一级缓存.二级缓存.使用时需要注意延迟加载必须使用resultMa ...

  3. 【转】使用Mybatis时遇到的延迟加载造成返回异常的问题——HttpMessageConversionException: Type definition error

    在使用Mybatis的过程中,使用了resultMap延迟加载. 延迟加载:association联表查询的过程中,查询另外两个表的对象.而延迟加载是指只有在使用这两个对象的时候才会进行查询. 问题的 ...

  4. Mybatis中使用association进行关联的几种方式

    这里以一对一单向关联为例.对使用或不使用association的配置进行举例.  实体类: @Data @ToString @NoArgsConstructor public class IdCard ...

  5. 在mybatis框架中,延迟加载与连表查询的差异

    1.引子 mybatis的延迟加载,主要应用于一个实体类中有复杂数据类型的属性,包括一对一和一对多的关系(在xml中用collection.association标签标识).这个种属性往往还对应着另一 ...

  6. Mybatis 是否支持延迟加载?如果支持,它的实现原理是什么?

    Mybatis 仅支持 association 关联对象和 collection 关联集合对象的延迟加载,association 指的就是一对一,collection 指的就是一对多查询.在 Myba ...

  7. MyBatis简单实例

    ---直接贴代码 (1)User.Java package me.gacl.domain; /** * @author gacl * users表所对应的实体类 */ public class Use ...

  8. Mybatis知识点总结

    ---恢复内容开始--- Mybatis知识点总结 1.#{}和${}的区别是什么? 答:#{}的使用场景:在表的sql映射文件中如下使用: <mapper namespace="co ...

  9. Mybatis面试整理

    #{}和${}的区别 #{}是预编译处理,${}是字符串替换. Mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值: Mybatis ...

随机推荐

  1. jQuery.makeArray() 函数详解

    jQuery.makeArray()函数用于将一个类数组对象转换为真正的数组对象. 所谓"类数组对象"就是一个常规的Object对象,但它和数组对象非常相似:具备length属性, ...

  2. PowerDesign设置列名与表名的大小写规则

    因为设计的Model是要用到Oracle 11g的,Oracle 11g的表名和列名默认是大写,在设计模型当中小写转大写一直很苦闹,发现PowerDedign可以直接设置,见下文: 打开Tools-- ...

  3. 更轻量的 View Controllers

    iew controllers 通常是 iOS 项目中最大的文件,并且它们包含了许多不必要的代码.所以 View controllers 中的代码几乎总是复用率最低的.我们将会看到给 view con ...

  4. 《极客学院 --NSAttributedString 使用详解-4-UITextKit 简介》学习笔记(待处理)

    如果要在富文本中添加图片的话,用UITextKit才能实现. 什么是UITextKit:它就是处理富文本的框架. 什么时候使用UITextKit:比如要实现图文混搭的节目. 在gitHub中 http ...

  5. java网络---基本web概念

    首先需要搞清楚web中的3个关键概念. 一.HTTP,HTML,URL. HTML 全称是Hypertext Markup Language,是用来描述一个网页的语言.或者说,该语言是用来描述网页的“ ...

  6. SQL 注入防御方法总结

    SQL 注入是一类危害极大的攻击形式.虽然危害很大,但是防御却远远没有XSS那么困难. SQL 注入可以参见:https://en.wikipedia.org/wiki/SQL_injection S ...

  7. Jquery Easy UI--datagrid的使用(转)

    第一篇学的是做一个管理的外框,接着就是数据datagrid绑定了,这里我用asp.net mvc3来做的,主要就是熟悉属性.方法. 打开easyui的demo 就可以看到如下一段代码: 和上篇一样cl ...

  8. SQL Server 2008 R2——学习/练习/错误/总结/搜集

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  9. Python开发之【简单计算器】

    开发一个简单的python计算器 1.实现加减乘除及括号优先级解析 2.用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * ...

  10. jenkins maven svn 部署web项目到本地Tomcat

    查了N多网页,折腾了几个小时,终于部署成功,部署的过程比较坎坷,遇到各种问题,记录一下,不管大家是否会遇到的同样的问题,希望有所帮助: 常规操作: 1.下载jenkins,必须要做的一步,http:/ ...