MyBatis结果映射(resultMap)

为什么要使用结果映射??             --> 关联对象查询->多表查询要使用

 

关联映射处理方式

嵌套结果:使用嵌套查询把所有结果一次查出

嵌套查询 : 多次查询,合并结果


先放代码分析流程:

多对一嵌套查询方式:

MyBatis结果映射(resultMap)_第1张图片

多对一嵌套结果方式:

MyBatis结果映射(resultMap)_第2张图片

一对多 嵌套查询方式:

MyBatis结果映射(resultMap)_第3张图片

一对多 嵌套结果方式:

MyBatis结果映射(resultMap)_第4张图片


注意:这里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();
            }
        }
    }
}

运行结果:

MyBatis结果映射(resultMap)_第5张图片


多对一:  嵌套结果

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();
            }
        }
    }
}

运行结果:

MyBatis结果映射(resultMap)_第6张图片


一对多: 嵌套查询

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结果映射(resultMap)_第7张图片


一对多: 嵌套结果

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结果映射(resultMap)_第8张图片

最后温馨小提示:掌握了这些代码流程,能写之后,就可以去了解MyBatis的代码生成器了,这个入门,主要是想让我们能在之后的代码生成器中看懂代码,怎么只能使用,才能让我们的学习更高效,其实有时候我们学习java,整理之后,可能就是一张图,需要我们站在巨人的肩膀上去拼接一张属于自己的图~~

你可能感兴趣的:(-----,-----④,MyBatis)