码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis文件映射之利用延迟加载解决collection分布查询(六)

时间:2020-01-20 20:38:37      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:log   throws   except   result   添加   结果   step   builder   art   

Employee.java

public class Employee {
    private Integer id;
    private String lastName;
    private String gender;
    private String email;
    Department dept;
}

Department.java

public class Department {
    private Integer id;
    private String deptName;
    private List<Employee> employee;
}

EmployeeMapperPlus.java

public interface EmployeeMapperPlus {
    public List<Employee> getEmpsByDeptId(Integer id);
}

EmployeeMapperPlus.xml

    <select id="getEmpsByDeptId" resultType="com.gong.mybatis.bean.Employee">
        select * from tbl_employee where d_id=#{deptId}
    </select>

DepartmentMapper.java

public interface DepartmentMapper {    
    public Department getDeptByIdStep(Integer id);
}

DepartmentMapper.xml

    <resultMap type="com.gong.mybatis.bean.Department" id="MyDeptStep">
        <id column="id" property="id"/>
        <result column="dept_name" property="deptName"/>
        <collection property="employee" select="com.gong.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
        column="id">    
        </collection>
    </resultMap>
    <select id="getDeptByIdStep" resultMap="MyDeptStep">
        select id,dept_name from tbl_department where id=#{id}
    </select>

同样的在mybatis配置文件中:

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

最后进行测试:

package com.gong.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

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.Test;

import com.gong.mybatis.bean.Department;
import com.gong.mybatis.bean.Employee;
import com.gong.mybatis.dao.DepartmentMapper;
import com.gong.mybatis.dao.EmployeeMapperPlus;

public class TestMybatis2 {
    
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }

    @Test
    public void test2() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
            Department department = mapper.getDeptByIdStep(1);
            System.out.println(department.getDeptName());
            openSession.commit();
        } finally {
            openSession.close();
        }
        
    }

}

首先我们只需要部门的名字:

结果:

DEBUG 01-20 19:35:58,093 ==> Preparing: select id,dept_name from tbl_department where id=? (BaseJdbcLogger.java:145)
DEBUG 01-20 19:35:58,167 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 19:35:58,299 <== Total: 1 (BaseJdbcLogger.java:145)
开发部

sql语句只发送了一次。

接下来我们需要部门里面员工信息:

添加:

System.out.println(department.getEmployee());

结果:

DEBUG 01-20 19:50:48,861 ==> Preparing: select id,dept_name from tbl_department where id=? (BaseJdbcLogger.java:145)
DEBUG 01-20 19:50:48,948 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 19:50:49,123 <== Total: 1 (BaseJdbcLogger.java:145)
开发部
DEBUG 01-20 19:50:49,125 ==> Preparing: select * from tbl_employee where d_id=? (BaseJdbcLogger.java:145)
DEBUG 01-20 19:50:49,126 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 19:50:49,129 <== Total: 2 (BaseJdbcLogger.java:145)
[Employee [id=1, lastName=xiximayou, gender=1, email=xiximayou@qq.com, dept=null], Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null]]

发送了两条sql语句。

mybatis文件映射之利用延迟加载解决collection分布查询(六)

标签:log   throws   except   result   添加   结果   step   builder   art   

原文地址:https://www.cnblogs.com/xiximayou/p/12219217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!