接上一篇,这一次,只讲嵌套结果,同样是上一次的三张表 
上次忘了介绍 resultMap,补上: 
resultMap概念视图
这次的查询要求是:
--通过教师 id 找班级和教师的信息:
select * from teacher t join classes c on t.teacherId =5001 and t.teacherId =c.teacherId join student s on s.classId=c.clsId 
1. 我们来看三个 Mapper.xml的配置
<!-- StudentMapper.xml -->
<mapper namespace="com.yc.mybatis.mapper.StudentMapper"> 
    <resultMap type="Student" id="StudentMap">
        <id column="stuId" property="stuId"/>
        <result column="stuName" property="stuName"/>
        <result column="stuSex" property="stuSex"/>
        <result column="stuBirthday" property="stuBirthday" />
        <result column="classId" property="classId"/>
    </resultMap>
</mapper>
<!-- ClassesMapper.xml -->
<mapper namespace="com.yc.mybatis.mapper.ClassesMapper"> 
    <resultMap type="Classes" id="ClassesMap">
        <id column="clsId" property="clsId"/>
        <result column="clsName" property="clsName"/>
        <collection property="stus" ofType="Student" resultMap="com.yc.mybatis.mapper.StudentMapper.StudentMap"></collection>
    </resultMap>
</mapper>
<!---  TeacherMapper.xml -->
<mapper namespace="com.yc.mybatis.mapper.TeacherMapper"> 
    <resultMap type="Teacher" id="TeacherMap">
        <id column="teacherId" property="teacherId"/>
        <result column="teacherName" property="teacherName"/>
        <result column="workYear" property="workYear"/>
        <result column="professional" property="professional"/>
        <!-- 调用 classesmapperxml 中的resultmap 需要有 namespace + id  -->
        <association property="classes" resultMap="com.yc.mybatis.mapper.ClassesMapper.ClassesMap"></association>
    </resultMap>
    <select id="getTeacherById" parameterType="int" resultMap="TeacherMap">
        select * from teacher t join classes c on t.teacherId =#{teacherId} and t.teacherId =c.teacherId
         join student s on s.classId=c.clsId 
    </select>
</mapper>
2. mybatis.xml 
  
 
<configuration>
    <!-- 配置文件和数据库连接的属性文件关联 然后可以通过 ognl 表达式 调用属性文件的内容  -->
    <properties resource="jdbc.properties"></properties>
    <!-- 定义类型的别名 -->
    <typeAliases>
        <!--  只能为一种类型指定别名 -->
        <!--<typeAlias type="com.yc.mybitis.entity.User" alias="User"/>-->
        <!-- 给指定包的所有类定义别名, 别名就是与不带包的类名相同 -->
        <package name="com/yc/mybitis/entity"/>
    </typeAliases>
    <!-- 环境可配置多个,default属性指明当前默认采用的环境  需要注意的是default 指定的环境必须在下文有配置-->
    <environments default="development">
        <environment id="development">
        <!-- transactionManager:事务管理, type:jdbc 自己管理,或者 type:Manager 交给第三方管理 如 spring -->
        <transactionManager type="JDBC"/>
        <!-- 数据源,POOLED 使用数据库连接池   ;    UNPOOLED 不使用  ;   JNDI  使用其他容器的连接池 -->
        <dataSource type="POOLED">
 <!-- properties已经在上面配置好了资源文件,与数据库连接 关联,通过表达式直接调用 -->
 <property name="driver" value="${jdbc.driver}"/>
 <property name="url" value="${jdbc.url}"/>
 <property name="username" value="${jdbc.user}"/>
 <property name="password" value="${jdbc.password}"/>
 </dataSource>
 </environment>
 </environments>
<mappers>
 <mapper resource="com/yc/mybitis/entity/ClassesMapper.xml"/>
 <mapper resource="com/yc/mybitis/entity/StudentMapper.xml"/>
 <mapper resource="com/yc/mybitis/entity/TeacherMapper.xml"/>
 </mappers> 
</configuration>
3. 接口 
 
4. 测试类 
package test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.yc.mybatis.mapper.TeacherMapper;
import com.yc.mybitis.entity.Teacher;
import com.yc.mybitis.util.MybatisUtil;
public class TeacherMapperTest {
    private TeacherMapper tMapper;
    @Before
    public void setUp() throws Exception {
        tMapper=MybatisUtil.getSession().getMapper(TeacherMapper.class);
    }
    @After
    public void tearDown() throws Exception {
        tMapper=null;
    }
    @Test
    public void testGetTeacherById() {
        Teacher t=tMapper.getTeacherById(5001);
        System.out.println( t );
    }
}
5. 测试结果 
 
好了,嵌套结果告一段落。 
mybatis 讲解怎么配置和使用,并没有了解底层实现,有兴趣的大家可以找找资料。
版权声明:本文为博主原创文章,谢谢参考!有问题的地方,欢迎纠正,一起进步。
原文地址:http://blog.csdn.net/emilyrr/article/details/47028407