Github代码地址

mybatis10--自连接多对一查询

查询老师对应的所有导师的信息

在09的基础上修改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">

    <resultMap type="Teacher" id="teacherMap">
      <id property="id" column="id"/>
      <result property="name" column="name"/>
      <!-- 设置关联集合的属性   递归查询 把查询到的tid当作id再次查询上级的导师信息 -->
      <association property="teacher" javaType="Teacher"
       select="selectTeahcerById" column="tid"/>
    </resultMap>
 
     <select id="selectTeahcerById" resultMap="teacherMap">
      select  id,name,tid from  teacher where id=#{xxx}
    </select>
    
</mapper>

 

测试类代码

    /**
     * 根据老师的编号查询所有的导师信息
     */
    @Test
    public void test1() {
        Teacher teacher = dao.selectTeahcerById(8);
        System.out.println(teacher);
    }

 

posted @ 2017-02-20 23:18  @小葱拌豆腐  阅读(922)  评论(0编辑  收藏  举报

霸气