前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mybatis文件映射之利用延迟加载解决collection分布查询(六)

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

作者头像
西西嘛呦
发布2020-08-26 11:22:48
2970
发布2020-08-26 11:22:48
举报
文章被收录于专栏:数据分析与挖掘

Employee.java

代码语言:javascript
复制
public class Employee {
    private Integer id;
    private String lastName;
    private String gender;
    private String email;
    Department dept;
}

Department.java

代码语言:javascript
复制
public class Department {
    private Integer id;
    private String deptName;
    private List<Employee> employee;
}

EmployeeMapperPlus.java

代码语言:javascript
复制
public interface EmployeeMapperPlus {
    public List<Employee> getEmpsByDeptId(Integer id);
}

EmployeeMapperPlus.xml

代码语言:javascript
复制
    <select id="getEmpsByDeptId" resultType="com.gong.mybatis.bean.Employee">
        select * from tbl_employee where d_id=#{deptId}
    </select>

DepartmentMapper.java

代码语言:javascript
复制
public interface DepartmentMapper {    
    public Department getDeptByIdStep(Integer id);
}

DepartmentMapper.xml

代码语言:javascript
复制
    <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配置文件中:

代码语言:javascript
复制
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

最后进行测试:

代码语言:javascript
复制
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语句只发送了一次。

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

添加:

代码语言:javascript
复制
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语句。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
日志服务
日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档