Mybatis series (19) – related concepts – delayed loading

Time:2020-10-26

Step 1: run first, see the effect, and then learn
Step 2: imitation and troubleshooting
Step 3: one to many based on annotation
Step 4: add the log
Step 5: only query the classification, not the product
Step 6: add the configuration of delayed loading
Step 7: only query the classification, not the product
Step 8: you will see the SQL statement only when you query the product

Step 1: run first, see the effect, and then learn

As the old rule, first download the executable items in the download area (click to enter), configure and run them, and then learn what steps to do to achieve this effect.

Step 2: imitation and troubleshooting

After making sure that the runnable project runs correctly, follow the steps of the tutorial and imitate the code again.
It is inevitable that there is code discrepancy in the simulation process, which leads to the failure to get the expected running resultsright key(can run the project) and your own code to locate the problem.
In this way,Learning is effective and troubleshooting is efficientIt can obviously improve the learning speed and cross the threshold of learning.

It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my runnable project folder.
This software is very powerful, you can know which two files in the folder are wrong, and mark them out clearly
Here is a green installation and use tutorial: diffmerge download and use tutorial

Step 3: one to many based on annotation

One to many configuration method for learning delayed loading based on annotation

Step 4: add logs

In order to observe the changes of SQL statements before and after the delayed loading, add the log

Step 5: only query the classification, not get the product

Comment out 27, 28, 29, and only get the classification, not the product. After execution, it is found that the SQL statement for obtaining the product is also executed

Mybatis series (19) - related concepts - delayed loading

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
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 com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
public class TestMybatis {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        CategoryMapper mapper = session.getMapper(CategoryMapper.class);
        List<Category> cs = mapper.list();
        for (Category c : cs) {
            System.out.println(c.getName());
//          List<Product> ps = c.getProducts();
//          for (Product p : ps) {
//              System.out.println("\t"+p.getName());
//          }
        }
        session.commit();
        session.close();
    }
}

Step 6: add the configuration of delayed loading

Modify mybatis- config.xml , add the configuration for delayed loading on lines 6-11

<?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>
    <settings>
        <! -- turn on the delay load switch -- >
        <setting name="lazyLoadingEnabled" value="true" />
        <! -- change active load to message load that is, load on demand -- >
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    <typeAliases>
        <package name="com.how2java.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="admin"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/how2java/pojo/Category.xml"/>
        <mapper class="com.how2java.mapper.CategoryMapper"/>
        <mapper class="com.how2java.mapper.ProductMapper"/>
    </mappers>
</configuration>

Step 7: only query the classification, not the product

By doing the same thing, you can see that only category is queried_ Table data, no query product_ Table data

Mybatis series (19) - related concepts - delayed loading

Step 8: you will only see the SQL statement when you query the product

Modify the code to get the product collection corresponding to the classification. Only then can we go to the product category_ Table.

Mybatis series (19) - related concepts - delayed loading

package com.how2java;

import java.io.IOException;
import java.io.InputStream;
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 com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.pojo.Product;
public class TestMybatis {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        CategoryMapper mapper = session.getMapper(CategoryMapper.class);
        List<Category> cs = mapper.list();
        for (Category c : cs) {
            System.out.println(c.getName());
            List<Product> ps = c.getProducts();
            for (Product p : ps) {
                System.out.println("\t"+p.getName());
            }
        }
        session.commit();
        session.close();
    }
}

For more information, click to learn: https://how2j.cn/k/mybatis/mybatis-annotation-lazyload/1099.html