这里讲的是人员和部门进行级联分布查询

1.首先在mybatis-config.xml里进行配置

<settings>
<!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

2 在department接口添加相关方法

public Department getDeptById(Integer id);

3在departmen映射文件中进行配置

<!--public Department getDeptById(Integer id); -->
<select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department">
select id,dname departmentName from department where id=#{id}
</select>

4在employee接口添加相关方法

public Employee getEmpByIdStep(Integer id);

5在departmen映射文件中进行配置

<!-- id last_name email gender d_id -->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpByStep">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!-- association定义关联对象的封装规则
select:表明当前属性是调用select指定的方法查出的结果
column:指定将哪一列的值传给这个方法

流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
-->
<association property="dept"
select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
column="did">
</association>
</resultMap>
<!-- public Employee getEmpByIdStep(Integer id);-->
<select id="getEmpByIdStep" resultMap="MyEmpByStep">
select * from tbl_employee where id=#{id}
</select>

6在junit进行测试

Employee employee = mapper.getEmpByIdStep(1);
System.out.println(employee.getEmail());

运行结果如下:只有一条sql语句

Employee employee = mapper.getEmpByIdStep(1);
System.out.println(employee.getDept());

运行结果如下:有二条sql语句

最新文章

  1. [light oj 1328] A Gift from the Setter
  2. 如何调试msbuild?
  3. iOS之获取当前时间日期并按固定格式显示
  4. Javac编译和JIT编译
  5. c语言指针与结构体之内存动态分配
  6. 51nod_1120:机器人走方格 V3
  7. C++基本知识点总结(网摘)
  8. 我的第一次Pascal程序
  9. OpenStack_I版 6.Neutron部署
  10. 如何使用《DB 查询分析器》高效地生成旬报货运量数据
  11. Identity Server 4 预备知识 -- OpenID Connect 简介
  12. STL源码剖析-vector
  13. redis 系列13 集合对象
  14. Perl文件句柄相关常量变量
  15. DevC++出现[Error] ld returned 1 exit status,如何解决才好呢?
  16. 微软BI 之SSIS 系列 - XML Task 中XSLT 样式表转换错误记录
  17. vijos 清点人数
  18. sql server 字符串分割函数
  19. PHP判断ajax请求:HTTP_X_REQUESTED_WITH
  20. ioncube 加密软件 linux 使用方法

热门文章

  1. IDEA的快捷方式
  2. 使用T4模板为EF框架添加数据库实体注释(转)
  3. vue中如何开发插件
  4. 集群中Session共享解决方案分析
  5. SpringBootMVC04——Mybatis
  6. Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard(思维,构造)
  7. MyBatis:Parameter Maps collection does not contain value for 的问题解决
  8. JAVA笔记24-IO流(2)-节点流举例
  9. 对vue源码之缓存的研究--------------引用
  10. shiro框架学习-8-shiro缓存