多表连接

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

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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 为什么要进行多表链接?需要查询的数据分布在多张表中。 什么是连接?1)连接就是在多给奥之间通过一定的连接条件,使表...
    ZainLee_8f5b阅读 474评论 0 0
  • 表连接 Left join: 以左表为准,去右表找数据,如果没有匹配的数据,则以null补空位, Inner jo...
    船_长阅读 4,789评论 0 1
  • 多表连接初步 引出 •思考如下问题? –写一条查询语句,查询员工姓名、部门名称、工作地点? 写一个查询显示员工姓名...
    wqjcarnation阅读 2,899评论 0 7
  • 多表连接初步 引出 •思考如下问题? –写一条查询语句,查询员工姓名、部门名称、工作地点? 写一个查询显示员工姓名...
    C_cole阅读 678评论 0 0
  • 一、什么是连接 连接是在多个表之间通过一定的连接条件,使表之间发生关联,进而能从多个表之间获得数据。 语法:SEL...
    辽A丶孙悟空阅读 2,654评论 0 40