Mybatis框架--测试延迟加载

在学习mybatis的延迟加载时,对 lazyLoadingEnabled 和 aggressiveLazyLoading 的区别并不理解,特别是对查询的条件不同时,执行的查询语句也不一样,所以还是测试总结一下

Blog: 

    private Integer id;
    private String title;
    /*private Integer authorId;*/
    private Author author;
    private String state;
    private Boolean featured;
    private String style;

Author:

   private Integer id;
    private String username;
    private String password;
    private String email;
    private String bio;
    private String favouriteSection;
    private String nickname;
    private String realname;

测试用例如下:

1.1 使用默认配置,不查询任何属性

test:

@Test
    public void testSelectBlogByIdLazyLoading() {

        SqlSession session = MyBatisUtil.getSqlsession();
        BlogMapper blogMapper = session.getMapper(BlogMapper.class);

        System.out.println("查询blog");
        Blog blog = blogMapper.selectBlogById(1);
        session.close();

        System.out.println("查询结束");
    }

console:

    查询blog    Opening JDBC Connection    Created connection 1263877414.    Setting autocommit to false on JDBC Connection [[email protected]]    ==>  Preparing: select * from blog where id = ?     ==> Parameters: 1(Integer)    ====>  Preparing: select * from author where id = ?     ====> Parameters: 1(Integer)    <====      Total: 1    <==      Total: 1    Resetting autocommit to true on JDBC Connection [[email protected]]    Closing JDBC Connection [[email protected]]    Returned connection 1263877414 to pool.    查询结束

result:    使用默认的配置,并且不对blog进行任何属性的查询,但是仍然执行了对author表的查询

1.2 使用默认配置,查询blog的非author属性

test:
    @Test
    public void testSelectBlogByIdLazyLoading() {

        SqlSession session = MyBatisUtil.getSqlsession();
        BlogMapper blogMapper = session.getMapper(BlogMapper.class);

        System.out.println("查询blog");
        Blog blog = blogMapper.selectBlogById(1);
        session.close();

        System.out.println("查询blog的title属性");
        System.out.println(blog.getTitle());
        System.out.println("查询结束");
    }

console:      查询blog    Opening JDBC Connection    Created connection 1263877414.    Setting autocommit to false on JDBC Connection [[email protected]]    ==>  Preparing: select * from blog where id = ?     ==> Parameters: 1(Integer)    ====>  Preparing: select * from author where id = ?     ====> Parameters: 1(Integer)    <====      Total: 1    <==      Total: 1    Resetting autocommit to true on JDBC Connection [[email protected]]    Closing JDBC Connection [[email protected]]    Returned connection 1263877414 to pool.    查询blog的title属性    My Colourful Garden    查询结束

result:    使用默认配置,只对blog的非author属性进行查询,但是结果和test1.1一样,都执行了对author表的查询

1.3 使用默认配置,查询blog的属性(包括author属性)

test:

    @Test
    public void testSelectBlogByIdLazyLoading() {

        SqlSession session = MyBatisUtil.getSqlsession();
        BlogMapper blogMapper = session.getMapper(BlogMapper.class);

        System.out.println("查询blog");
        Blog blog = blogMapper.selectBlogById(1);
        session.close();

        System.out.println("查询blog的title属性");
        System.out.println(blog.getTitle());        System.out.println("查询blog的author属性");        System.out.println(blog.getAuthor().getUsername());
        System.out.println("查询结束");
    }

console:
  
    查询blog
    Opening JDBC Connection
    Created connection 1263877414.
    Setting autocommit to false on JDBC Connection [[email protected]]
    ==>  Preparing: select * from blog where id = ?
    ==> Parameters: 1(Integer)
    ====>  Preparing: select * from author where id = ?
    ====> Parameters: 1(Integer)
    <====      Total: 1
    <==      Total: 1
    Resetting autocommit to true on JDBC Connection [[email protected]]
    Closing JDBC Connection [[email protected]]
    Returned connection 1263877414 to pool.
    查询blog的title属性    My Colourful Garden    查询blog的author属性    helen
    查询结束

result:
  
  使用默认配置,对blog属性(包括author属性)进行查询,但是结果和test1.1以及test1.2一样,都执行了对author表的查询

2.1 配置 lazyLoadingEnabled:true; 不查询任何属性

 config:

<!-- 延迟加载 默认不开启 -->
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>

test:    @Test   public void testSelectBlogByIdLazyLoading() { 

     SqlSession session = MyBatisUtil.getSqlsession();     BlogMapper blogMapper = session.getMapper(BlogMapper.class);           System.out.println("查询blog");     Blog blog = blogMapper.selectBlogById(1);      session.close(); 
        System.out.println("查询结束");
    }
console:
  
    查询blog
    Opening JDBC Connection
    Created connection 1263877414.
    Setting autocommit to false on JDBC Connection [[email protected]]
    ==>  Preparing: select * from blog where id = ?
    ==> Parameters: 1(Integer)
    <==      Total: 1
    Resetting autocommit to true on JDBC Connection [[email protected]]
    Closing JDBC Connection [[email protected]]
    Returned connection 1263877414 to pool.
    查询结束

result:    配置 lazyLoadingEnabled:true,并且不对blog进行任何属性查询,则session只执行对本表的查询

2.2 配置 lazyLoadingEnabled:true; 查询blog的非author属性

 config:

   <!-- 延迟加载 默认不开启 -->
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>

test:
  
  @Test
  public void testSelectBlogByIdLazyLoading() { 

     SqlSession session = MyBatisUtil.getSqlsession();
     BlogMapper blogMapper = session.getMapper(BlogMapper.class);
    
     System.out.println("查询blog");
     Blog blog = blogMapper.selectBlogById(1);
     session.close();  

     System.out.println("查询blog的title属性");      System.out.println(blog.getTitle());
        System.out.println("查询结束");
    }

console:
  
    查询blog
    Opening JDBC Connection
    Created connection 1263877414.
    Setting autocommit to false on JDBC Connection [[email protected]]
    ==>  Preparing: select * from blog where id = ?
    ==> Parameters: 1(Integer)
    <==      Total: 1
    Resetting autocommit to true on JDBC Connection [[email protected]]
    Closing JDBC Connection [[email protected]]
    Returned connection 1263877414 to pool.
    查询blog的title属性    Opening JDBC Connection    Checked out connection 1263877414 from pool.    Setting autocommit to false on JDBC Connection [[email protected]]    ==>  Preparing: select * from author where id = ?     ==> Parameters: 1(Integer)    <==      Total: 1    Resetting autocommit to true on JDBC Connection [[email protected]]    Closing JDBC Connection [[email protected]]    Returned connection 1263877414 to pool.    My Colourful Garden    查询结束

result:
  
  配置 lazyLoadingEnabled:true,并且对blog进行非author查询,则session先执行对本表的查询,然后执行对author表的查询

2.3 配置 lazyLoadingEnabled:true;查询blog的属性(包括author属性)

config:

   <!-- 延迟加载 默认不开启 -->
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>

test:
  
  @Test
  public void testSelectBlogByIdLazyLoading() { 

     SqlSession session = MyBatisUtil.getSqlsession();
     BlogMapper blogMapper = session.getMapper(BlogMapper.class);
    
     System.out.println("查询blog");
     Blog blog = blogMapper.selectBlogById(1);
     session.close();

     System.out.println("查询blog的title属性");      System.out.println(blog.getTitle());        System.out.println("查询blog的author属性");        System.out.println(blog.getAuthor().getUsername());
        System.out.println("查询结束");
    }

console:
  
    查询blog
    Opening JDBC Connection
    Created connection 1263877414.
    Setting autocommit to false on JDBC Connection [[email protected]]
    ==>  Preparing: select * from blog where id = ?
    ==> Parameters: 1(Integer)
    <==      Total: 1
    Resetting autocommit to true on JDBC Connection [[email protected]]
    Closing JDBC Connection [[email protected]]
    Returned connection 1263877414 to pool.
    查询blog的title属性
    Opening JDBC Connection
    Checked out connection 1263877414 from pool.
    Setting autocommit to false on JDBC Connection [[email protected]]
    ==>  Preparing: select * from author where id = ?
    ==> Parameters: 1(Integer)
    <==      Total: 1
    Resetting autocommit to true on JDBC Connection [[email protected]]
    Closing JDBC Connection [[email protected]]
    Returned connection 1263877414 to pool.
    My Colourful Garden    查询blog的author属性    helen
    查询结束

result:
  
  配置 lazyLoadingEnabled:true,并且对blog进行author查询,则结果和test2.2一样,session先执行对本表的查询,然后执行对author表的查询

3.1 配置lazyLoadingEnabled:true;aggressiveLazyLoading:false;  不查询任何属性

  config:

   <!-- 延迟加载 默认不开启 -->    <!-- 默认是积极的懒加载 -->    <settings>        <setting name="lazyLoadingEnabled" value="true"/>        <setting name="aggressiveLazyLoading" value="false"/>    </settings>

  test:
    @Test     public void testSelectBlogByIdLazyLoading() { 

       SqlSession session = MyBatisUtil.getSqlsession();       BlogMapper blogMapper = session.getMapper(BlogMapper.class);             System.out.println("查询blog");       Blog blog = blogMapper.selectBlogById(1);        session.close(); 
          System.out.println("查询结束");
      }

  console:
     查询blog
    Opening JDBC Connection
    Created connection 1263877414.
    Setting autocommit to false on JDBC Connection [[email protected]]
    ==>  Preparing: select * from blog where id = ?
    ==> Parameters: 1(Integer)
    <==      Total: 1
    Resetting autocommit to true on JDBC Connection [[email protected]]
    Closing JDBC Connection [[email protected]]
    Returned connection 1263877414 to pool.
    查询结束

  result:

     配置 lazyLoadingEnabled:true,aggressiveLazyLoading:false; 并且不对blog进行任何查询,则session只执行对本表的查询

3.2 配置lazyLoadingEnabled:true;aggressiveLazyLoading:false;  查询非author属性

 config:

   <!-- 延迟加载 默认不开启 -->  <!-- 默认是积极的懒加载 -->
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>      <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

 test:
  
  @Test
  public void testSelectBlogByIdLazyLoading() { 

     SqlSession session = MyBatisUtil.getSqlsession();
     BlogMapper blogMapper = session.getMapper(BlogMapper.class);
    
     System.out.println("查询blog");
     Blog blog = blogMapper.selectBlogById(1);
     session.close(); 

     System.out.println("查询blog的title属性");     System.out.println(blog.getTitle());

        System.out.println("查询结束");
    }

console:
  
    查询blog
    Opening JDBC Connection
    Created connection 1263877414.
    Setting autocommit to false on JDBC Connection [[email protected]]
    ==>  Preparing: select * from blog where id = ?
    ==> Parameters: 1(Integer)
    <==      Total: 1
    Resetting autocommit to true on JDBC Connection [[email protected]]
    Closing JDBC Connection [[email protected]]
    Returned connection 1263877414 to pool.
    查询blog的title属性    My Colourful Garden
    查询结束

result:
  
  配置 lazyLoadingEnabled:true,aggressiveLazyLoading:false ;并且对blog进行非author查询,则session只执行对本表的查询

3.3 配置lazyLoadingEnabled:true;aggressiveLazyLoading:false;  查询blog的属性(包括author属性)

config:

   <!-- 延迟加载 默认不开启 -->  <!-- 默认是积极的懒加载 -->
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>      <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

test:
  
  @Test
  public void testSelectBlogByIdLazyLoading() { 

     SqlSession session = MyBatisUtil.getSqlsession();
     BlogMapper blogMapper = session.getMapper(BlogMapper.class);
    
     System.out.println("查询blog");
     Blog blog = blogMapper.selectBlogById(1);
     session.close();
          System.out.println("查询blog的title属性");        System.out.println(blog.getTitle());        System.out.println("查询blog的author属性");        System.out.println(blog.getAuthor().getUsername());
        System.out.println("查询结束");
    }

console:
  
    查询blog
    Opening JDBC Connection
    Created connection 1263877414.
    Setting autocommit to false on JDBC Connection [[email protected]]
    ==>  Preparing: select * from blog where id = ?
    ==> Parameters: 1(Integer)
    <==      Total: 1
    Resetting autocommit to true on JDBC Connection [[email protected]]
    Closing JDBC Connection [[email protected]]
    Returned connection 1263877414 to pool.
    查询blog的title属性
    Opening JDBC Connection
    Checked out connection 1263877414 from pool.
    Setting autocommit to false on JDBC Connection [[email protected]]
    ==>  Preparing: select * from author where id = ?
    ==> Parameters: 1(Integer)
    <==      Total: 1
    Resetting autocommit to true on JDBC Connection [[email protected]]
    Closing JDBC Connection [[email protected]]
    Returned connection 1263877414 to pool.
    My Colourful Garden
    查询blog的author属性
    helen
    查询结束

result:
  
  配置 lazyLoadingEnabled:true;aggressiveLazyLoading:false; 并且对blog进行author查询,则session先执行对本表的查询,然后执行对author表的查询
在默认配置下,无论是否对blog的属性进行查询,session都会执行对blog表和对author表两条查询语句,然后存入缓存中,供查询结果调用,称为不延迟加载;

在配置lazyLoadingEnabled:true 后,如果不对blog的任何属性进行查询,session只会执行查询blog的语句;但是只要对blog的任意属性进行查询,就会查询blog表和author表,然后放入缓存,共查询结果调用,称为积极的延迟加载;

在配置 lazyLoadingEnabled:true;aggressiveLazyLoading:false;后,如果不对blog的author属性进行查询,session只会执行查询blog的语句;如果查询了blog的author属性,就会继续查询author,成为不积极的延迟加载
时间: 2024-11-05 22:20:55

Mybatis框架--测试延迟加载的相关文章

用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试

这一部分的主要目的是 配置spring-service.xml  也就是配置spring  并测试service层 是否配置成功 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)在这个基础上面 继续进行spring的配置. 回顾上面  我们已经成功测试通过了Mybatis的配置. 这时候的目录结构是: 一:下面我们继续补充目录结构,在com.peakfortake的文件目录项目 

MyBatis框架中Mapper映射配置的使用及原理解析(三) 配置篇 Configuration

从上文<MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder> 我们知道XMLConfigBuilder调用parse()方法解析Mybatis配置文件,生成Configuration对象. Configuration类主要是用来存储对Mybatis的配置文件及mapper文件解析后的数据,Configuration对象会贯穿整个Mybatis的执行流程,为Mybatis的执行过程提供必要的配

Mybatis 框架文档 超详细笔记

1      Mybatis入门 1.1    单独使用jdbc编程问题总结 1.1.1  jdbc程序 Public static void main(String[] args) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { //加载数据库驱动 Class.forName("com.mysql.jdbc.Driver&q

mybatis框架入门

初识mybatis 对原生jdbc程序(单独使用jdbc开发)问题总结 mybatis框架原理 mybatis入门程序 用户的增删查改 mybatis开发dao两种方法 原始dao开发方法(程序需要编写dao接口和dao实现类) mybatis的mapper接口(相当于dao接口)代理开发方法 mybatis配置文件SqlMapConfig.xml mybatis核心 mybatis输入映射 mybatis输出映射 mybatis的动态sql 第二天 订单商品的数据分析 改机映射(一对一.一对多

MyBatis框架(一)

MyBatis介绍: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .2013年11月迁移到Github. MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动.创建connection.创建statement.手动设置参数.结果集检索等jdbc繁杂的过程代

mybatis框架搭建学习初步

mybatis框架搭建步骤:1. 拷贝jar到lib目录下,而且添加到工程中2. 创建mybatis-config.xml文件,配置数据库连接信息 <environments default="development"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSou

MyBatis框架知识整理

MyBatis框架 一.介绍: MyBatis实际上是Ibatis3.0版本以后的持久化层框架[也就是和数据库打交道的框架]! 和数据库打交道的技术有: 原生的JDBC技术---> Spring的JdbcTemplate技术 这些工具都是提供简单的SQL语句的执行,但是和我们这里学的MyBatis框架还有些不同,框架是一整套的东西,例如事务控制,查询缓存,字段映射等等. 我们用原生JDBC操作数据库的时候都会经过: 编写sql---->预编译---->设置参数----->执行sql

Hibernate框架与Mybatis框架的对比

学习了Hibernate和Mybatis,但是一直不太清楚他们两者的区别的联系,今天在网上翻了翻,就做了一下总结,希望对大家有帮助! 原文:http://blog.csdn.net/firejuly/article/details/8190229 第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀的O/R mapping框架.目前属于apache的

Mybatis框架中实现一对多关系映射

学习过Hibernate框架的伙伴们很容易就能简单的配置各种映射关系(Hibernate框架的映射关系在我的blogs中也有详细的讲解),但是在Mybatis框架中我们又如何去实现 一对多的关系映射呢? 其实很简单 首先我们照常先准备前期的环境(具体解释请  参考初识Mybatis进行增.删.改.查 blogs )这里我就直接上代码了 主配置文件:Configuration.xml <?xml version="1.0" encoding="UTF-8" ?&