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

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 >
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" >
select * from tbl_employee where id=#{id}
</select>

6在junit进行测试

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

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

association实现懒加载分段级联查询

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

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

association实现懒加载分段级联查询

 

相关文章:

  • 2022-01-05
  • 2021-07-13
  • 2021-10-30
  • 2022-12-23
  • 2021-12-14
  • 2022-12-23
  • 2021-12-19
  • 2021-07-14
猜你喜欢
  • 2021-10-03
  • 2022-12-23
  • 2021-09-11
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2022-12-23
相关资源
相似解决方案