接下来介绍resultMap定义查询结果集,实现关联查询

1 首先在接口中定义操作的方法

public interface EmployeeMapperPlus {

public Employee getEmpAndDept(Integer id);

}

2在xml里进行配置

<!--第一种进行配置

</resultMap>


<!--
第二种配置使用association定义关联的单个对象的封装规则;
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" >departmentName FROM tbl_employee e,department d
WHERE e.did=d.id AND e.id=#{id}
</select>

3在junit里进行测试

@Test
public void test05() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
Employee empAndDept = mapper.getEmpAndDept(1);
System.out.println(empAndDept);
System.out.println(empAndDept.getDept());

}finally{
openSession.close();
}


}

相关文章: