很久沒寫MyBatis了,今天繼續。 html
處理has-one關係須要用到association元素,而處理has many關係則須要用到collection元素。例如本例中,假設一名教師可同時指導多名學生,下面就來介紹如何使用collection元素來實現這種映射,具體的任務是查詢出教師及其指導的多個學生的信息(本示例源代碼下載頁面:http://down.51cto.com/data/490947)。 java
1、爲Teacher實體增長相關屬性 spring
爲教師實體增長指導學生集合的屬性以下: mybatis
private List<Student> supStudents;//指導學生
併爲其增長setter和getter方法,這裏略過。 app
2、TeacherMapper接口 dom
爲實現教師實體映射,應先建立映射器接口以下: ide
package com.abc.mapper; import com.abc.domain.Teacher; public interface TeacherMapper { public Teacher getById(int id); }
3、映射文件 學習
爲教師實體建立的映射文件以下: ui
<?xml version="1.0" encoding="utf8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--與之前同樣,namespace的值是對應的映射器接口的完整名稱--> <mapper namespace="com.abc.mapper.TeacherMapper"> <!--TeacherMapper接口中getById方法對應的SQL語句。 查詢教師及其指導的學生的信息。因爲教師、學生都有 id、name、gender等屬性,所以給教師的字段都起了別名--> <select id="getById" parameterType="int" resultMap="supervisorResultMap"> select t.id t_id, t.name t_name, t.gender t_gender, t.research_area t_research_area, t.title t_title, s.id,s.name, s.gender,s.major,s.grade from teacher t,student s where t.id=#{id} and s.supervisor_id = t.id </select> <!--教師實體映射--> <resultMap id="supervisorResultMap" type="Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> <result property="gender" column="t_gender"/> <result property="researchArea" column="t_research_area"/> <result property="title" column="t_title"/> <!--collection元素映射教師的指導學生集合的屬性。resultMap 以命名空間名.resultMap的id的形式,引用studentResultMap。 須要注意的是,上面的select語句中學生的字段名/別名應與 studentResultMap中的column屬性一致--> <collection property="supStudents" resultMap="com.abc.mapper.StudentMapper.studentResultMap"/> </resultMap> </mapper>
相應地,學生實體的映射文件以下: spa
<?xml version="1.0" encoding="utf8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.abc.mapper.StudentMapper"> <resultMap id="studentResultMap" type="Student"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="gender" column="gender"/> <result property="major" column="major"/> <result property="grade" column="grade"/> <!--相應地,在此引用supervisorResultMap,亦採用 命名空間名.resultMap的id的形式。--> <association property="supervisor" resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap"/> </resultMap> </mapper>
在工程的src\resources目錄下新建子目錄mappers,用來統一存放映射文件。爲了能讓MyBatis找到這些映射文件,修改其核心配置文件configuration.xml中的mappers元素以下:
<!--在classpath中以相對路徑的形式引用映射文件--> <mappers> <mapper resource="resources/mappers/StudentMapper.xml"/> <mapper resource="resources/mappers/TeacherMapper.xml"/> </mappers>
注意:resources目錄在工程編譯前會被複制到classes目錄下(詳見工程生成文件build.xml中的copy-resources和compile這兩個target),而classes目錄會被ant添加到classpath中。
4、執行類
執行類爲CollectionDemo,其內容以下:
package com.demo; import org.springframework.context.ApplicationContext; import com.abc.mapper.StudentMapper; import com.abc.mapper.TeacherMapper; import com.abc.domain.Teacher; import com.abc.domain.Student; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class CollectionDemo { private static ApplicationContext ctx; static { //在類路徑下尋找resources/beans.xml文件 ctx = new ClassPathXmlApplicationContext("resources/beans.xml"); } public static void main(String[] args) { //從Spring容器中請求映射器 TeacherMapper mapper = (TeacherMapper)ctx.getBean("teacherMapper"); //查詢id爲1的教師 Teacher teacher = mapper.getById(1); if(teacher == null) { System.out.println("未找到相關教師信息。"); } else { //教師信息 System.out.println("**********************************************"); System.out.println("教師姓名:" + " " + teacher.getName()); System.out.println("教師職稱:" + " " + teacher.getTitle()); System.out.println("**********************************************"); System.out.println("指導學生信息:"); //遍歷指導的學生 for(Student s : teacher.getSupStudents()) { System.out.println("**********************************************"); System.out.println( s.getName() + " " + s.getGender() + " " + s.getGrade() + " " + s.getMajor()); //從學生端訪問教師 System.out.println("指導教師研究方向:" + s.getSupervisor().getResearchArea()); } System.out.println("**********************************************"); } } }
從中可看出,能夠從任意一端訪問另外一端的對象。
5、修改build.xml
爲了能用ant運行此程序,需修改build.xml中的run target,指定類CollectionDemo爲執行類。以下:
<target name="run" depends="compile"> <!--指定CollectionDemo爲要運行的類--> <java fork="true" classname="com.demo.CollectionDemo" classpathref="library"> <!--把classes目錄添加到工程的classpath中。 ${targetdir}是指引用上面定義的property元素targetdir--> <classpath path="${targetdir}"/> </java> </target>
運行結果以下:
MyBatis技術交流羣:188972810,或掃描二維碼:
【MyBatis學習筆記】系列之預備篇一:ant的下載與安裝
【MyBatis學習筆記】系列之二:MyBatis增刪改示例
【MyBatis學習筆記】系列之三:MyBatis的association示例
【MyBatis學習筆記】系列之四:MyBatis association的兩種形式
【MyBatis學習筆記】系列之五:MyBatis與Spring集成示例
【MyBatis學習筆記】系列之六:MyBatis與Spring集成示例續
【MyBatis學習筆記】系列之七:MyBatis一對多雙向關聯
【MyBatis學習筆記】系列之八:MyBatis MapperScannerConfigurer配置
【MyBatis學習筆記】系列之九:MyBatis collection的兩種形式
【MyBatis學習筆記】系列之十:MyBatis日誌之Log4j示例
【MyBatis學習筆記】系列之十一:MyBatis多參數傳遞之註解方式示例
【MyBatis學習筆記】系列之十二:MyBatis多參數傳遞之默認命名方式示例
【MyBatis學習筆記】系列之十三:MyBatis多參數傳遞之Map方式示例
【MyBatis學習筆記】系列之十四:MyBatis中的N+1問題
【MyBatis學習筆記】系列之十五:MyBatis多參數傳遞之混合方式
【MyBatis學習筆記】系列之十六:Spring聲明式事務管理示例
【MyBatis學習筆記】系列之十七:MyBatis多對多保存示例
【MyBatis學習筆記】系列之十八:MyBatis多對多關聯查詢示例
【MyBatis學習筆記】系列之十九:如何在MyBatis-3.2.7中使用Log4j2 rc2