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

resultMap2_关联查询collection分步查询&延迟加载

时间:2018-01-09 21:16:13      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:加载   获取   bsp   gpo   bean   pts   int   调用   eem   

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>

 

resultMap2_关联查询collection分步查询&延迟加载

标签:加载   获取   bsp   gpo   bean   pts   int   调用   eem   

原文地址:https://www.cnblogs.com/2016024291-/p/8253458.html

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