接上一篇,這一次,只講嵌套結果,同樣是上一次的三張表
上次忘了介紹 resultMap,補上:
resultMap概念視圖
- constructor – 類在實例化時,用來注入結果到構造方法中
- idArg – ID參數;標記結果作爲ID可以幫助提高整體效能
- arg – 注入到構造方法的一個普通結果
- id – 一個ID結果;標記結果作爲ID可以幫助提高整體效能
- result – 注入到字段或JavaBean屬性的普通結果
- association – 一個複雜的類型關聯;許多結果將包成這種類型
- collection – 複雜類型的集
- discriminator – 使用結果值來決定使用哪個結果映射
- case – 基於某些值的結果映射
這次的查詢要求是:
--通過教師 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 講解怎麼配置和使用,並沒有瞭解底層實現,有興趣的大家可以找找資料。