Mybatis Series Textbook - related concepts - delayed loading

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 log
Step 5: only query the classification without obtaining products
Step 6: add the configuration of delayed loading
Step 7: only query the classification without obtaining products
Step 8: you can only see the SQL statement when querying the product

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

Old rule, download first Download area (click to enter) After the runnable project is configured and run, and it is confirmed that it is available, we can learn what steps have been taken to achieve this effect.

Step 2: imitation and troubleshooting

After ensuring that the runnable project can run correctly, follow the steps of the tutorial strictly and imitate the code again.
It is inevitable that there are code differences in the imitation process, resulting in the failure to get the expected running results. At this moment, locate the problem by comparing the correct answer (runnable project) with your own code.
In this way, learning is effective and error elimination is efficient, which can significantly improve the learning speed and cross all barriers on the learning road.

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

Step 3: one to many based on annotation

be based on One to many annotation To learn the configuration method of delayed loading

Step 4: add log

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

Step 5: only query the classification without obtaining products

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

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 of delayed loading in 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 switch for delayed loading -->
        <setting name="lazyLoadingEnabled" value="true" />
        <!-- Change active loading to message loading, that is, on-demand loading -->
        <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 without obtaining products

By performing the same operation, you can observe that only category is queried_ The data of the table is not queried_ Table data

Step 8: you can only see the SQL statement when querying the product

Modify the code and obtain the product set corresponding to the classification. Only then can you correct the product_ Table.

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

Tags: Java Mybatis Back-end

Posted by agriz on Mon, 23 May 2022 16:49:16 +0300