Mybatis框架--测试延迟加载

Posted 耍流氓的兔兔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis框架--测试延迟加载相关的知识,希望对你有一定的参考价值。

 

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

 

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,成为不积极的延迟加载

 












































































































































以上是关于Mybatis框架--测试延迟加载的主要内容,如果未能解决你的问题,请参考以下文章

测试mybatis延迟加载错误与解决方法

MyBatis测试resultMap,分步查询以及延迟加载

Mybatis从入门到精通系列 11——Mybatis 延迟加载与立即加载

MyBatis专题-延迟加载

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

使用MyBatis 3和Java进行延迟加载