专栏首页数据分析与挖掘mybatis文件映射之利用延迟加载解决collection分布查询(六)

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

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语句。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • mybatis文件映射之利用association进行关联查询之分步查询(三)

    现在新增:DepartmentMapper.java和DepartmentMapper.xml

    绝命生
  • 基于maven+ssm的增删改查之使用mybatis逆向工程生成相关文件

    (5)javabean生成的位置、mapper接口的位置、mapper.xml文件的位置

    绝命生
  • mybatis扩展之使用PageHelper插件进行分页(是真好用)

    绝命生
  • mybatis文件映射之利用association进行关联查询之分步查询(三)

    现在新增:DepartmentMapper.java和DepartmentMapper.xml

    绝命生
  • [javaSE] GUI(事件监听机制)

    调用Frame对象的addWindowListener()方法,参数:WindowListener对象,WindowListener是个接口,里面有七个方法要实...

    陶士涵
  • [javaSE] GUI(练习-列出指定目录内容)

    陶士涵
  • [javaSE] GUI(对话框Dialog)

    获取Dialog对象,new出来,构造参数:Frame对象,String的标题,模式

    陶士涵
  • [javaSE] GUI(菜单)

    调用Frame对象的setMenuBar()方法,设置菜单,参数:MenuBar对象

    陶士涵
  • Jmeter Beanshell采样器调用JAVA方法(二)

    3、查看debug响应数据,jmeter1=111 jmeter2=222 jmeter3=333 jmeter4=444 jmeter5=555,确认Bean...

    橙子探索测试
  • Javac命令使用ct.sym文件约束可使用的类

    在JDK6,升级为JDK7或者JDK8的过程中会遇到一些奇怪的问题,简单的介绍一下经典的ClassNotFound:sun.nio.*、com.sun.imag...

    白凡

扫码关注云+社区

领取腾讯云代金券