多表连接

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

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

多对一 可以都是用自动映射
public List getEmp2(); select e.*,d.loc,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno; @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(); select e.*,d.loc,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno; @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(); }

【多表连接】多对多
select s.*,l.*,sl.id,sl.sc from lession l , students s,student_lession sl where l.lid=sl.lid and sl.sid = s.sid; ##按照sid将结果捏合进集合中

    推荐阅读