淺談collection標(biāo)簽的oftype屬性能否為java.util.Map
collection標(biāo)簽的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都是可以的,當(dāng)然寫成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聚集
聚集元素用來處理“一對(duì)多”的關(guān)系。需要指定映射的Java實(shí)體類的屬性,屬性的javaType(一般為ArrayList);列表中對(duì)象的類型ofType(Java實(shí)體類);對(duì)應(yīng)的數(shù)據(jù)庫表的列名稱;
不同情況需要告訴MyBatis 如何加載一個(gè)聚集。MyBatis 可以用兩種方式加載:
1. select: 執(zhí)行一個(gè)其它映射的SQL 語句返回一個(gè)Java實(shí)體類型。較靈活;
2. resultsMap: 使用一個(gè)嵌套的結(jié)果映射來處理通過join查詢結(jié)果集,映射成Java實(shí)體類型。
例如,一個(gè)班級(jí)有多個(gè)學(xué)生。
首先定義班級(jí)中的學(xué)生列表屬性:private List<StudentEntity> studentList;
使用select實(shí)現(xiàn)聚集
用法和聯(lián)合很類似,區(qū)別在于,這是一對(duì)多,所以一般映射過來的都是列表。所以這里需要定義javaType為ArrayList,還需要定義列表中對(duì)象的類型ofType,以及必須設(shè)置的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> ?
?
<!-- 查詢學(xué)生list,根據(jù)班級(jí)id --> ?
<select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap"> ?
? ? <include refid="selectStudentAll" /> ?
? ? WHERE ST.CLASS_ID = #{classID} ?
</select>?使用resultMap實(shí)現(xiàn)聚集
使用resultMap,就需要重寫一個(gè)sql,left join學(xué)生表。
<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請(qǐng)見上面TeacherMapper.xml文件部分內(nèi)容中。studentResultMap請(qǐng)見上面StudentMapper.xml文件部分內(nèi)容中。
collection中的ofType="String"時(shí)
DTO:
package com.example.mybatis.entity;
import java.util.List;
/**
* 統(tǒng)計(jì)部門下的員工名稱(只查詢出員工名稱)
*/
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)計(jì)部門下的員工名稱(只查詢出員工名稱)
? ? ?*/
? ? ListString listStringTest(Integer deptId);
}表中數(shù)據(jù):

測(cè)試:
/**
* 統(tǒng)計(jì)部門下的員工名稱(只查詢出員工名稱)
*/
@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]}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Java?HashTable與Collections.synchronizedMap源碼深入解析
- JAVA容器集合全面解析(Collection和Map)
- Java中Collection、List、Set、Map之間的關(guān)系總結(jié)
- 如何解決Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for X
- 淺談Java中常用數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)類 Collection和Map
- java 集合----Map、Collection
- Java Map集合與Collection類的使用詳解
相關(guān)文章
Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之隊(duì)列
這篇文章主要介紹了Java隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn),隊(duì)列是一種特殊的線性表,只允許在表的隊(duì)頭進(jìn)行刪除操作,在表的后端進(jìn)行插入操作,隊(duì)列是一個(gè)有序表先進(jìn)先出,想了解更多相關(guān)資料的小伙伴可以參考下面文章的詳細(xì)內(nèi)容2022-02-02
剖析Spring WebFlux反應(yīng)式編程設(shè)計(jì)及工作原理
這篇文章主要為大家介紹了Spring WebFlux反應(yīng)式編程模型工作原理的剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02
解決SpringBoot項(xiàng)目使用多線程處理任務(wù)時(shí)無法通過@Autowired注入bean問題
這篇文章主要介紹了SpringBoot項(xiàng)目使用多線程處理任務(wù)時(shí)無法通過@Autowired注入bean問題的解決方法,需要的朋友可以參考下2018-09-09
Java中的反射,枚舉及l(fā)ambda表達(dá)式的使用詳解
這篇文章主要為大家詳細(xì)介紹了Java的反射,枚舉及l(fā)ambda表達(dá)式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

