1. 程式人生 > >mybatis10--自連接多對一查詢

mybatis10--自連接多對一查詢

所有 bsp from 測試 java out void pan tid

查詢老師對應的所有導師的信息

在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); }

mybatis10--自連接多對一查詢