zoukankan      html  css  js  c++  java
  • mybatis09--自连接一对多查询

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

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

    /**
     *导师的实体类
     */
    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);
            }
  • 相关阅读:
    MySQL 查询树结构、循环查询、查看函数、视图、存储过程
    SpringBoot 部署:外置依赖包
    springboot集成docker实现自动化构建镜像说明
    Chrome谷歌浏览器常用快捷键、开发技巧
    Windows常用CMD命令
    Git 版本管理,与 SVN区别对比
    TensorFlow重新导入restore报错: OP_REQUIRES failed at save_restore_v2_ops.cc:184 : Not found: Key Variable not found in checkpoint
    MySQL字符集设置
    Hive 时间操作
    深度思考:如何规划你的职业生涯
  • 原文地址:https://www.cnblogs.com/xtdxs/p/7095988.html
Copyright © 2011-2022 走看看