最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談collection標(biāo)簽的oftype屬性能否為java.util.Map

 更新時(shí)間:2022年02月07日 15:53:24   作者:gaoshan12345678910  
這篇文章主要介紹了collection標(biāo)簽的oftype屬性能否為java.util.Map,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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ù)庫(kù)表的列名稱; 

不同情況需要告訴MyBatis 如何加載一個(gè)聚集。MyBatis 可以用兩種方式加載: 

1. select: 執(zhí)行一個(gè)其它映射的SQL 語(yǔ)句返回一個(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的語(yǔ)句名稱(需要注意的是,這里的查詢student的select語(yǔ)句條件必須是外鍵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ù)庫(kù)表字段之間的映射定義 --> ?
<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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Springboot設(shè)置統(tǒng)一的返回格式的方法步驟

    Springboot設(shè)置統(tǒng)一的返回格式的方法步驟

    在我們應(yīng)用中我們通常與前端交互使用json格式,設(shè)置統(tǒng)一的返回json 格式是非常必要的,本文主要介紹了Springboot設(shè)置統(tǒng)一的返回格式的方法步驟,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Java如何設(shè)置過期時(shí)間的map的幾種方法

    Java如何設(shè)置過期時(shí)間的map的幾種方法

    本文主要介紹了Java如何設(shè)置過期時(shí)間的map的幾種方法,常見的解決方法有:ExpiringMap、LoadingCache及基于HashMap的封裝三種,下面就詳細(xì)的介紹一下,感興趣的可以了解下
    2022-03-03
  • spring boot系列之集成測(cè)試(推薦)

    spring boot系列之集成測(cè)試(推薦)

    這篇文章主要介紹了spring boot系列集成測(cè)試,需要的朋友可以參考下
    2018-03-03
  • Spring中@Primary注解的作用詳解

    Spring中@Primary注解的作用詳解

    這篇文章主要介紹了Spring中@Primary注解的作用詳解,@Primary 注解是Spring框架中的一個(gè)注解,用于標(biāo)識(shí)一個(gè)Bean作為默認(rèn)的實(shí)現(xiàn)類,當(dāng)存在多個(gè)實(shí)現(xiàn)類時(shí),通過使用@Primary注解,可以指定其中一個(gè)作為默認(rèn)的實(shí)現(xiàn)類,以便在注入時(shí)自動(dòng)選擇該實(shí)現(xiàn)類,需要的朋友可以參考下
    2023-10-10
  • Maven的安裝+配置本地倉(cāng)庫(kù)路徑方式

    Maven的安裝+配置本地倉(cāng)庫(kù)路徑方式

    這篇文章主要介紹了Maven的安裝+配置本地倉(cāng)庫(kù)路徑方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Java、Javascript、Javaweb三者的區(qū)別及說明

    Java、Javascript、Javaweb三者的區(qū)別及說明

    這篇文章主要介紹了Java、Javascript、Javaweb三者的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • java明文密碼三重加密方法

    java明文密碼三重加密方法

    這篇文章主要介紹了java明文密碼加密,對(duì)一個(gè)明文密碼進(jìn)行了三重加密:第一層?xùn)艡谝淮危诙釉跂艡谝淮?,第三層在一次摩斯加密,感興趣的小伙伴們可以參考一下
    2016-07-07
  • SpringBoot攔截器實(shí)現(xiàn)項(xiàng)目防止接口重復(fù)提交

    SpringBoot攔截器實(shí)現(xiàn)項(xiàng)目防止接口重復(fù)提交

    基于SpringBoot框架來開發(fā)業(yè)務(wù)后臺(tái)項(xiàng)目時(shí),接口重復(fù)提交是一個(gè)常見的問題,本文主要介紹了SpringBoot攔截器實(shí)現(xiàn)項(xiàng)目防止接口重復(fù)提交,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • 淺析SpringBoot自動(dòng)裝配的實(shí)現(xiàn)

    淺析SpringBoot自動(dòng)裝配的實(shí)現(xiàn)

    springboot開箱即用,其實(shí)實(shí)現(xiàn)了自動(dòng)裝配,本文重點(diǎn)給大家介紹SpringBoot是如何做到自動(dòng)裝配的,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • Spingboot?JPA?CriteriaBuilder?如何獲取指定字段

    Spingboot?JPA?CriteriaBuilder?如何獲取指定字段

    這篇文章?主要介紹了Spingboot?JPA?CriteriaBuilder?如何獲取指定字段,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論

读书| 吴江市| 蕲春县| 满洲里市| 瑞金市| 青铜峡市| 天峨县| 专栏| 靖边县| 肇源县| 武陟县| 乐至县| 兰坪| 瓦房店市| 平和县| 文登市| 建瓯市| 乌拉特中旗| 田阳县| 华池县| 南木林县| 高尔夫| 新竹县| 和田县| 庆云县| 通州区| 南阳市| 铅山县| 定州市| 平凉市| 蓝山县| 台州市| 中超| 湖州市| 紫云| 湖南省| 鞍山市| 平潭县| 离岛区| 尉氏县| 鹤壁市|