美文网首页
多表连接

多表连接

作者: 林深雾雨 | 来源:发表于2020-02-10 14:34 被阅读0次

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

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

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

<!--mapper.java-->
public List<Emp> getEmp2();
<!--mapper.xml-->
<select id="getEmp2" resultMap="_empdept">
        select e.*,d.loc,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;
    </select>
    
    <resultMap type="Emp" id="_empdept" autoMapping="true">
        <id column="empno" property="empno"/>
        <!-- <result column="ename" property="ename"/>
        <result column="job" property="job"/>
        <result column="mgr" property="mgr"/>
        <result column="hiredate" property="hiredate"/>
        <result column="sal" property="sal"/>
        <result column="comm" property="comm"/> -->

        <association property="dept" javaType="Dept" autoMapping="true" <!-- association 联系 -->
            <!-- <id column="deptno" property="deptno"/>
            <result column="dname" property="dname"/>
            <result column="loc" property="loc"/> -->
        </association>
    </resultMap>
<!--test.java-->
@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<Emp> list=empMapper.getEmp2();
        for(Emp e : list) {
            System.out.println(e.getEmpno()+"||"+e.getEname()+"||"+e.getJob()+"||"+e.getDept().getDeptno());
        }
        sqlSession.close();
    }

一对多 id都要手写

<!--mapper.java-->
public List<Dept> getEmp3();
<!--mapper.xml-->
<select id="getEmp3" resultMap="_deptemp">
        select e.*,d.loc,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;
    </select>
    
    <resultMap type="Dept" id="_deptemp" autoMapping="true">
    <id column="deptno" property="deptno"/>
        <collection property="emps" ofType="Emp" column="deptno" autoMapping="true">
        </collection>
    </resultMap>
<!--test.java-->
@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<Dept> 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 id="getStudent" resultMap="_studnet_LessionLession">
        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;
    </select>
    <resultMap type="Student" id="_studnet_LessionLession" autoMapping="true">
        <id column="sid" property="sid"/> <!-- property 类中的属性 -->
        <collection property="list" column="sid" ofType="Student_Lession" autoMapping="true">
##按照sid将结果捏合进集合中
        <id column="id" property="id"/>
        <association property="ls" javaType="Lession" autoMapping="true">
            <id column="lid" property="lid"/> 
        </association>
        </collection>
    </resultMap>

相关文章

  • SQL语句常用命令整理---多表查询

    多表查詢之关连查询 多表数据连接查询,简称连接查询。本篇我们来一同学习多表连接查询的相关用法,主要內容有: 内连接...

  • MySQL 基础 6 多表查询

    1.1 多表查询的概述 1.1.1 多表查询的分类 1.1.1.1连接查询 交叉连接:cross join交叉连接...

  • 多表连接查询

    多表连接查询 -- 1. 预备工作 -- 2. 多表连接的类型 -- 笛卡尔乘积 -- 内连接 (取交集) -- ...

  • Fourth Day

    /-----------------------------------多表连接查询---------------...

  • 2018-08-30

    今天学习了Oracle的多表连接,组函数,分组查询,子查询,常用函数。 多表连接有三种类型:内连接,外连接,自连接...

  • 多表连接

    多表查询不允许有相同的列 也就是不写* 多对一 可以都是用自动映射 一对多 id都要手写 多对多

  • 多表连接

    一、什么是连接 连接是在多个表之间通过一定的连接条件,使表之间发生关联,进而能从多个表之间获得数据。 语法:SEL...

  • Oracle学习day-28:多表查询

    一、多表连接查询 1.多表连接语法 主要解决目标数据分布在不同的表中(如果目标数据不再一张表,可以通过多表连接查询...

  • 数据库-day04

    多表连接查询环境准备 多表连接说明: 外连接 show语句 元数据获取 什么是元数据? 元数据 回去方法 info...

  • 003 多表查询, 事务和DCL.

    多表查询 多表查询: 笛卡尔积.内连接查询隐式内连接 : 使用where.eg: select * from ...

网友评论

      本文标题:多表连接

      本文链接:https://www.haomeiwen.com/subject/bkssnctx.html