淺談collection標簽的oftype屬性能否為java.util.Map
collection標簽的oftype屬性能否為java.util.Map
基于mybatis-3.4.5.jar版本,結(jié)論是可以的。
<resultMap type="*.*.*.TestShowVO" id="testShowVO"> ? ? <result column="APP_ID" jdbcType="VARCHAR" property="id" /> ? ? <result column="APP_NAME" jdbcType="VARCHAR" property="name" /> ? ? <result column="PRIORITY" jdbcType="DECIMAL" property="priority" /> ? ? <collection property="multiLanguageList" ofType="map"> ? ? ? ? <result column="LANGUAGE_CODE" property="languageCode" /> ? ? ? ? <result column="TEXT" property="text" /> ? ? </collection> </resultMap> <select id="getAppWithMultiLanguage" ?resultMap="testShowVO"> ? SELECT APP_ID ,APP_NAME,PRIORITY,LANGUAGE_CODE,TEXT from TABLE_APP left join TABLE_LANGUAGE on TABLE_LANGUAGE.DATA_ID = TABLE_APP.APP_ID </select>
其中,ofType寫成map或java.util.HashMap都是可以的,當然寫成pojo的完整名也是可以的,例如ofType="a.b.c.MultiLanguageVO"
package *.*.*; import java.util.HashMap; import java.util.List; import java.util.Map; public class TestShowVO{ private String id; private String name; private Integer priority; // private List<MultiLanguageVO> multiLanguageList; // private List<HashMap> multiLanguageList; private List<Map> multiLanguageList; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getPriority() { return priority; } public void setPriority(Integer priority) { this.priority = priority; } public List<Map> getMultiLanguageList() { return multiLanguageList; } public void setMultiLanguageList(List<Map> multiLanguageList) { this.multiLanguageList = multiLanguageList; } }
collection聚集
聚集元素用來處理“一對多”的關系。需要指定映射的Java實體類的屬性,屬性的javaType(一般為ArrayList);列表中對象的類型ofType(Java實體類);對應的數(shù)據(jù)庫表的列名稱;
不同情況需要告訴MyBatis 如何加載一個聚集。MyBatis 可以用兩種方式加載:
1. select: 執(zhí)行一個其它映射的SQL 語句返回一個Java實體類型。較靈活;
2. resultsMap: 使用一個嵌套的結(jié)果映射來處理通過join查詢結(jié)果集,映射成Java實體類型。
例如,一個班級有多個學生。
首先定義班級中的學生列表屬性:private List<StudentEntity> studentList;
使用select實現(xiàn)聚集
用法和聯(lián)合很類似,區(qū)別在于,這是一對多,所以一般映射過來的都是列表。所以這里需要定義javaType為ArrayList,還需要定義列表中對象的類型ofType,以及必須設置的select的語句名稱(需要注意的是,這里的查詢student的select語句條件必須是外鍵classID)。
ClassMapper.xml文件部分內(nèi)容:
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/> <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" select="getStudentByClassID"/> </resultMap> <select id="getClassByID" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT WHERE CT.CLASS_ID = #{classID}; </select>
StudentMapper.xml文件部分內(nèi)容:
<!-- java屬性,數(shù)據(jù)庫表字段之間的映射定義 --> ? <resultMap type="StudentEntity" id="studentResultMap"> ? ? ? <id property="studentID" column="STUDENT_ID" /> ? ? ? <result property="studentName" column="STUDENT_NAME" /> ? ? ? <result property="studentSex" column="STUDENT_SEX" /> ? ? ? <result property="studentBirthday" column="STUDENT_BIRTHDAY" /> ? </resultMap> ? ? <!-- 查詢學生list,根據(jù)班級id --> ? <select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap"> ? ? ? <include refid="selectStudentAll" /> ? ? ? WHERE ST.CLASS_ID = #{classID} ? </select>?
使用resultMap實現(xiàn)聚集
使用resultMap,就需要重寫一個sql,left join學生表。
<resultMap type="ClassEntity" id="classResultMap"> <id property="classID" column="CLASS_ID" /> <result property="className" column="CLASS_NAME" /> <result property="classYear" column="CLASS_YEAR" /> <association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/> <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" resultMap="studentResultMap"/> </resultMap> <select id="getClassAndTeacherStudent" parameterType="String" resultMap="classResultMap"> SELECT * FROM CLASS_TBL CT LEFT JOIN STUDENT_TBL ST ON CT.CLASS_ID = ST.CLASS_ID LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID WHERE CT.CLASS_ID = #{classID}; </select>
其中的teacherResultMap請見上面TeacherMapper.xml文件部分內(nèi)容中。studentResultMap請見上面StudentMapper.xml文件部分內(nèi)容中。
collection中的ofType="String"時
DTO:
package com.example.mybatis.entity; import java.util.List; /** * 統(tǒng)計部門下的員工名稱(只查詢出員工名稱) */ public class ListString { // 部門id private int deptId; // 員工名稱集合 private List<String> empNames; public ListString() { } public ListString(int deptId, List<String> empNames) { this.deptId = deptId; this.empNames = empNames; } // getter .... // setter .... }
mapper:
? ? <resultMap id="deptWithEmpNameMap" type="com.example.mybatis.entity.ListString"> ? ? ? ? <result property="deptId" jdbcType="BIGINT" column="dept_id"/> ? ? ? ? <collection property="empNames" ofType="String" > ? ? ? ? ? ? <id ?column="emp_name"/> ? ? ? ? </collection> ? ? </resultMap> ? ? <select id="listStringTest" parameterType="Integer" resultMap="deptWithEmpNameMap"> ? ? ? ? SELECT ?deptId as 'dept_id',name as 'emp_name' ? ? ? ? FROM employee WHERE deptId = #{deptId}; ? ? </select>
dao:
@Mapper public interface EmployeeMapper { ? ? /** ? ? ?* 統(tǒng)計部門下的員工名稱(只查詢出員工名稱) ? ? ?*/ ? ? ListString listStringTest(Integer deptId); }
表中數(shù)據(jù):
測試:
/** * 統(tǒng)計部門下的員工名稱(只查詢出員工名稱) */ @Test public void deptWithEmpNameTest(){ ListString listString = employeeMapper.listStringTest(1); System.out.println(listString); }
輸出結(jié)果:
ListString{deptId=1, empNames=[小紅1, 小紅2, 小紅3, 小紅4, 小紅5, 小紅6, 小紅7, 小紅8, 小紅9, 小紅10]}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Java?HashTable與Collections.synchronizedMap源碼深入解析
- JAVA容器集合全面解析(Collection和Map)
- Java中Collection、List、Set、Map之間的關系總結(jié)
- 如何解決Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for X
- 淺談Java中常用數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)類 Collection和Map
- java 集合----Map、Collection
- Java Map集合與Collection類的使用詳解
相關文章
Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之隊列
這篇文章主要介紹了Java隊列數(shù)據(jù)結(jié)構(gòu)的實現(xiàn),隊列是一種特殊的線性表,只允許在表的隊頭進行刪除操作,在表的后端進行插入操作,隊列是一個有序表先進先出,想了解更多相關資料的小伙伴可以參考下面文章的詳細內(nèi)容2022-02-02解決SpringBoot項目使用多線程處理任務時無法通過@Autowired注入bean問題
這篇文章主要介紹了SpringBoot項目使用多線程處理任務時無法通過@Autowired注入bean問題的解決方法,需要的朋友可以參考下2018-09-09Java中的反射,枚舉及l(fā)ambda表達式的使用詳解
這篇文章主要為大家詳細介紹了Java的反射,枚舉及l(fā)ambda表達式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03