美文网首页
多表连接

多表连接

作者: 林深雾雨 | 来源:发表于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>
    

    相关文章

      网友评论

          本文标题:多表连接

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