为什么要使用结果映射?? --> 关联对象查询->多表查询要使用
关联映射处理方式
嵌套结果:使用嵌套查询把所有结果一次查出
嵌套查询 : 多次查询,合并结果
先放代码分析流程:
多对一嵌套查询方式:
多对一嵌套结果方式:
一对多 嵌套查询方式:
一对多 嵌套结果方式:
注意:这里bean类就统一不放了... 【注意要在MyBatis的核心配置文件中引入beanMapper映射文件哦~~】
多对一: 嵌套查询
beanMapper接口:
public interface EmployeeMapper {
List findAll();
}
beanMapper映射文件:
测试代码:
public class EmployeeMapperTest {
@Test
public void findAll() {
SqlSession sqlSession=null;
try {
sqlSession= MybatisUtil.getSqlSession();
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
System.out.println(employeeMapper.findAll());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
运行结果:
多对一: 嵌套结果
beanMapper接口:
public interface EmployeeMapper {
List findAll();
}
beanMapper映射文件:
测试代码:
public class EmployeeMapperTest {
@Test
public void findAll() {
SqlSession sqlSession=null;
try {
sqlSession= MybatisUtil.getSqlSession();
EmployeeMapper employeeMapeper = sqlSession.getMapper(EmployeeMapper.class);
System.out.println(employeeMapeper.findAll());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
运行结果:
一对多: 嵌套查询
beanMapper接口:
public interface DepartmentMapper {
List findAll();
}
beanMapper映射文件:
测试代码:
public class DepartmentMapperTest {
@Test
public void findAll() {
SqlSession sqlSession=null;
try {
sqlSession= MybatisUtil.getSqlSession();
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
System.out.println(departmentMapper.findAll());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
运行结果:
一对多: 嵌套结果
beanMapper接口:
public interface DepartmentMapper {
List findAll();
}
beanMapper映射文件:
测试代码:
public class DepartmentMapperTest {
@Test
public void findAll() {
SqlSession sqlSession=null;
try {
sqlSession= MybatisUtil.getSqlSession();
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
System.out.println(departmentMapper.findAll());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
运行结果:
最后温馨小提示:掌握了这些代码流程,能写之后,就可以去了解MyBatis的代码生成器了,这个入门,主要是想让我们能在之后的代码生成器中看懂代码,怎么只能使用,才能让我们的学习更高效,其实有时候我们学习java,整理之后,可能就是一张图,需要我们站在巨人的肩膀上去拼接一张属于自己的图~~