zoukankan      html  css  js  c++  java
  • resultMap2_关联查询collection分步查询&延迟加载

    1、DepartmentMapper.java添加代码

    public Department getDeptByIdStep(Integer id);

    2、EmployeeMapperPlus.java添加代码

    public Employee getEmpByIdDeptId(Integer deptId);

    3、DepartmentMapper.xml添加代码

    <!-- public Department getDeptByIdStep(Integer id); -->
        <resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
            <id column="id" property="id"/>
            <result column="departmentName" property="departmentName"/>
            <collection property="emps" 
                                select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpByIdDeptId"
                                    column="id">
            </collection>    
        </resultMap>
        <select id="getDeptByIdStep" resultMap="MyDeptStep">
            select id,dept_name departmentName from tbl_dept where id=#{id}
        </select>

    注:collection类似于association,先调用getDeptByIdStep方法获得tbl_dept中id的值,在用获取的id值做为参数传入到com.atguigu.mybatis.dao.EmployeeMapperPlus接口中getDeptByIdStep方法得到Employee 的所有参数值,最后封装到Department对象中 

    4、EmployeeMapperPlus.xml添加代码

    <!-- public Employee getEmpByIdDeptId(Integer deptId); -->
        <resultMap type="com.atguigu.mybatis.bean.Employee" id="emps22">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
        </resultMap>
        <select id="getEmpByIdDeptId" resultMap="emps22">
            select * from tbl_employee where d_id=#{deptId}
        </select>
  • 相关阅读:
    【linux 文件管理】7-文件重定向
    by David Bombal CCNA with kali linux
    【linux 文件管理】6-软链接和硬链接
    13.mysql索引的使用
    11.mysql SQL优化之SQL问题定位
    mysql服务常用命令
    10.mysql存储引擎
    9. Mysql的体系结构概览
    8.mysql触发器
    项目上线部署
  • 原文地址:https://www.cnblogs.com/2016024291-/p/8253458.html
Copyright © 2011-2022 走看看