• Mybatais09 自连接查询一对多


    查询导师 下面的所有 老师的信息!

    创建实体类 和对应的数据库

    复制代码
    /**
     *导师的实体类
     */
    public class Teacher {
        private  Integer id;
        private  String name;
         //导师下所有的老师
        private  Set<Teacher> teachers;
        
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Set<Teacher> getTeachers() {
            return teachers;
        }
        public void setTeachers(Set<Teacher> teachers) {
            this.teachers = teachers;
        }
        public Teacher(Integer id, String name, Set<Teacher> teachers) {
            super();
            this.id = id;
            this.name = name;
            this.teachers = teachers;
        }
        public Teacher() {
            super();
        }
        @Override
        public String toString() {
            return "Teacher [id=" + id + ", name=" + name + ", teachers="
                    + teachers + "]";
        }
    }
    复制代码

    创建对应的mapper文件和dao

    复制代码
    public interface TeacherDao {
        /**
         * 根据导师的编号查询所有的老师信息 
         * 相当于递归查询 
         */
        List<Teacher> selectTeahcerByTid(Integer tId);
    }
    复制代码
    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
    <mapper namespace="cn.bdqn.dao.TeacherDao">
    
    
    
    
        <resultMap type="Teacher" id="teacherMap">
          <id property="id" column="id"/>
          <result property="name" column="name"/>
          <!-- 设置关联集合的属性 
           select="selectTeahcerByTid"
                       因为查询的结果都是一样的   递归查询
           column="id"
                     根据查询到的id再作为参数进行下一次的查询
           -->
          <collection property="teachers" ofType="Teacher"
           select="selectTeahcerByTid" column="id"/>
        </resultMap>
     
         <select id="selectTeahcerByTid" resultMap="teacherMap">
          select  id,name from  teacher where tid=#{xxx}
        </select>
        
    </mapper>
    复制代码

    mybatis.xml中管理mapper文件

        <!-- 加载映射文件信息 -->
        <mappers>
            <mapper resource="cn/bdqn/dao/TeacherMapper.xml" />
        </mappers>

    测试代码

    复制代码
    package cn.bdqn.test;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.ibatis.session.SqlSession;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import cn.bdqn.bean.Teacher;
    import cn.bdqn.dao.TeacherDao;
    import cn.bdqn.util.SessionUtil;
    
    public class TeacherTest {
        TeacherDao dao;
        SqlSession session;
    
        @Before
        public void before() {
            // 因为需要关闭session 需要把session提取出去
            session = SessionUtil.getSession();
            dao = session.getMapper(TeacherDao.class);
        }
    
        @After
        public void after() {
            if (session != null) {
                session.close();
            }
        }
    
        /**
         * 根据导师的id查询出对应的所有老师信息
         */
        @Test
        public void test1() {
            List<Teacher> list = dao.selectTeahcerByTid(1);
            for (Teacher teacher : list) {
                System.out.println(teacher);
            }
        }
        
    }
    复制代码

    查询导师 的信息 以及 下面的所有 老师的信息!

     修改dao 和 mapper文件

    public interface TeacherDao {
        /**
         * 根据导师的编号查询所有的老师信息以及自身的信息 
         */
        Teacher selectTeahcerById(Integer tId);
    }
    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-Mapper.dtd">
    <mapper namespace="cn.bdqn.dao.TeacherDao">
    
       <select id="selectTeahcerByTid" resultMap="teacherMap">
         select id,name from teacher where tid=#{xxx}
         <!--#{xxx} 就是再根据tid查询对应的子项 -->
       </select>
    
        <resultMap type="Teacher" id="teacherMap">
          <id property="id" column="id"/>
          <result property="name" column="name"/>
          <!-- 设置关联集合的属性 
           select="selectTeahcerByTid"
                       因为查询的结果都是一样的   递归查询
           column="id"
                     根据查询到的id再作为tid进行下一次的查询
           -->
          <collection property="teachers" ofType="Teacher"
           select="selectTeahcerByTid" column="id"/>
        </resultMap>
     
         <select id="selectTeahcerById" resultMap="teacherMap">
              <!--先查询出 自身的信息  -->
          select  id,name from  teacher where id=#{xxx}
        </select>
        
    </mapper>
    复制代码

    修改测试类

    复制代码
        /**
         * 根据导师的id查询出对应的所有老师信息
         */
        @Test
        public void test1() {
            Teacher teacher = dao.selectTeahcerById(1);
                System.out.println(teacher);
            }
  • 相关阅读:
    苹果 macOS 安装 Source Code Pro
    C# 中代码执行 ping 操作
    WPF 中 Path 使用虚线
    查看 Java Web 开发环境软件是 32 位还是 64 位
    linux 磁盘io监控
    Linux下系统如何监控服务器硬件、操作系统、应用服务和业务
    ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
    完全卸载oracle11g步骤
    用JDBC连接SQL Server2017数据库
    oracle INS-13001 环境不满足最低要求
  • 原文地址:https://www.cnblogs.com/kaisadadi/p/7612300.html
Copyright © 2020-2023  润新知