多表连接
多表查询不允许有相同的列 也就是不写*
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将结果捏合进集合中
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- py连接mysql
- Android|Android BLE蓝牙连接异常处理
- springboot整合数据库连接池-->druid
- Python3|Python3 MySQL 数据库连接
- Xshell5|Xshell5 远程连接本地虚拟机Ubuntu16
- mac|mac 链接linux服务器 如何在Mac上连接服务器
- TCP长连接与段链接
- 运维|如何限制IP 通过 SSH连接服务器
- 网络|网络编程释疑(TCP连接拔掉网线后会发生什么)