mybatis lazyload

首先pojo类
dept.java

package com.husky.pojo;

import java.util.List;

public class Dept {
    private int id;
    private String name;
    private List emps;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List getEmps() {
        return emps;
    }

    public void setEmp(List emps) {
        this.emps = emps;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", emps=" + emps +
                '}';
    }
}

emp.java

package com.husky.pojo;

public class Emp {
    private int id;
    private String firstName;
    private double salary;
    private Emp manager;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Emp getManager() {
        return manager;
    }

    public void setManager(Emp manager) {
        this.manager = manager;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", salary=" + salary +
                ", manager=" + manager +
                '}';
    }
}

下面是EmpMapper.xml


<select id="findByDept" parameterType="int" resultType="com.husky.pojo.Emp">
        select id,first_name name,salary from s_emp where dept_id = #{id}
    select>

DeptMapper.xml


    
    <resultMap id="lazyDept" type="com.husky.pojo.Dept">
        <id column="id" property="id"/>
        
        <collection property="emps" ofType="com.husky.pojo.Emp"
                    select="emp.findByDept" column="id">collection>
    resultMap>

    <select id="findDeptById" parameterType="int" resultMap="lazyDept">
        select id,name from s_dept where id = #{id}
    select>

当然要开启懒加载要在mybatis 配置文件中加上lazyload的配置
sqlMapConfig.xml

<settings>
        
        <setting name="cacheEnabled" value="true"/>
        
        <setting name="defaultStatementTimeout" value="3000"/>
        
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        
        <setting name="useGeneratedKeys" value="true"/>

        
        <setting name="lazyLoadingEnabled" value="true"/>
        
        <setting name="aggressiveLazyLoading" value="false"/>
        
        <setting name="lazyLoadTriggerMethods" value=""/>
    settings>

测试

package com.husky.test;

import com.husky.pojo.Dept;
import com.husky.pojo.Emp;
import com.husky.pojo.Region;
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 org.junit.Before;


import java.io.IOException;
import java.io.InputStream;

public class Test {
    SqlSessionFactory factory;
    @Before
    public void before() throws IOException {
        String res = "com/husky/sqlMapConfig.xml";
        InputStream is = Resources.getResourceAsStream(res);
        factory = new SqlSessionFactoryBuilder().build(is);
    }

    @org.junit.Test
    public void lazyTest(){
        SqlSession session = factory.openSession();
        Dept d = session.selectOne("findDeptById",42);
        System.out.println(d);
        System.out.println(d.getEmps());
    }

}

输出结果

Dept{id=42, name='Operations', emps=null}
[Emp{id=7, firstName='null', salary=1250.0, manager=null}, Emp{id=18, firstName='null', salary=1200.0, manager=null}, Emp{id=19, firstName='null', salary=795.0, manager=null}]

你可能感兴趣的:(mybatis)