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

淺談collection標簽的oftype屬性能否為java.util.Map

 更新時間:2022年02月07日 15:53:24   作者:gaoshan12345678910  
這篇文章主要介紹了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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

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

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

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

    Java如何設置過期時間的map的幾種方法

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

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

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

    Spring中@Primary注解的作用詳解

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

    Maven的安裝+配置本地倉庫路徑方式

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

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

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

    java明文密碼三重加密方法

    這篇文章主要介紹了java明文密碼加密,對一個明文密碼進行了三重加密:第一層柵欄一次,第二層在柵欄一次,第三層在一次摩斯加密,感興趣的小伙伴們可以參考一下
    2016-07-07
  • SpringBoot攔截器實現(xiàn)項目防止接口重復提交

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

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

    淺析SpringBoot自動裝配的實現(xiàn)

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

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

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

最新評論

河南省| 清水河县| 白城市| 大渡口区| 达孜县| 乐安县| 赣州市| 黄陵县| 法库县| 新田县| 嘉义县| 射洪县| 莱阳市| 龙山县| 慈溪市| 芷江| 丰宁| 郴州市| 曲麻莱县| 怀安县| 松阳县| 乌恰县| 阜康市| 襄垣县| 奉贤区| 建瓯市| 望城县| 栾川县| 靖安县| 张家界市| 福海县| 洛浦县| 祁连县| 石阡县| 汶川县| 婺源县| 凤城市| 大方县| 玛沁县| 增城市| 西和县|