多表连接

多表查询不允许有相同的列 也就是不写*

select emp.*,dept.loc,dept.dname from emp , dept  where emp.deptno = dept.deptno;

多对一 可以都是用自动映射


public List getEmp2();


    
    
        
        

        
            
        
    

@Test
    public void testFirst2() throws Exception {
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper=sqlSession.getMapper(EmpMapper.class);
        List list=empMapper.getEmp2();
        for(Emp e : list) {
            System.out.println(e.getEmpno()+"||"+e.getEname()+"||"+e.getJob()+"||"+e.getDept().getDeptno());
        }
        sqlSession.close();
    }

一对多 id都要手写


public List getEmp3();


    
    
    
        
        
    

@Test
    public void testFirst3() throws Exception {
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper=sqlSession.getMapper(EmpMapper.class);
        List list=empMapper.getEmp3();
        for(Dept d : list) {
            System.out.println(d.getDeptno()+" "+d.getDname()+" ");
            System.out.println(".................................");
            for (Emp e : d.getEmps()) {
                System.out.println(e.getEname()+"||"+e.getJob());
            }
        }
        sqlSession.close();
    }

多对多


    
         
        
##按照sid将结果捏合进集合中
        
        
             
        
        
    

你可能感兴趣的:(多表连接)