MyBatis 延迟加载

2020-10-12

1、SQL

create table user (
id int primary key,
name varchar(400)
);
insert user info VALUES(1, 'ConstXiong1');


create table info (
user_id int primary key,
name varchar(400)
);
insert into info VALUES(1, '大熊');

create table article (
user_id int,
title varchar(400)
);
insert into article VALUES(1, '文章1');
insert into article VALUES(1, '文章2');

 

2、Maven 依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>constxiong</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>mybatis-lazyload</artifactId>

    <dependencies>
        <!-- MySQL 8.0.21 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>

        <!-- MyBatis -->
        <dependency>
		  <groupId>org.mybatis</groupId>
		  <artifactId>mybatis</artifactId>
		  <version>3.5.5</version>
		</dependency>

    </dependencies>

    <build>

    </build>
</project>

 

3、MyBatis 配置

<?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>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
        <setting name="lazyLoadTriggerMethods" value=""/>
    </settings>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://172.31.32.184:3306/constxiong?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="constxiong@123"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="constxiong/mapper/UserMapper.xml"/>
    </mappers>
</configuration>

 

4、Mapper 接口

package constxiong.mapper;

import constxiong.po.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * UserMapper 接口,映射对数据库的操作
 */
public interface UserMapper {

    User selectUserWithLazyInfo();

}
package constxiong.mapper;

import constxiong.po.Info;
import org.apache.ibatis.annotations.*;

/**
 * InfoMapper 接口,映射对数据库的操作
 */
public interface InfoMapper {

    @Select("select * from info where user_id = #{userId}")
    @Results(value = {@Result(column="user_id", property = "userId")})
    Info selectInfoByUserId(int userId);

}

 

5、Mapper xml

<?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">
<mapper namespace="constxiong.mapper.UserMapper">

    <select id="selectUserWithLazyInfo" resultMap="UserWithLazyInfo">
        select * from user where id = 1
    </select>

    <resultMap id="UserWithLazyInfo" type="constxiong.po.User">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="info" javaType="constxiong.po.Info" select="constxiong.mapper.InfoMapper.selectInfoByUserId" column="id"/>
    </resultMap>

</mapper>


7、测试代码

 

ConstXiong 备案号:苏ICP备16009629号-3