浅谈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版本,结论是可以的。

<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实体类);对应的数据库表的列名称; 

不同情况需要告诉MyBatis 如何加载一个聚集。MyBatis 可以用两种方式加载: 

1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活; 

2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

例如,一个班级有多个学生。 

首先定义班级中的学生列表属性:private List<StudentEntity> studentList;

使用select实现聚集 

用法和联合很类似,区别在于,这是一对多,所以一般映射过来的都是列表。所以这里需要定义javaType为ArrayList,还需要定义列表中对象的类型ofType,以及必须设置的select的语句名称(需要注意的是,这里的查询student的select语句条件必须是外键classID)。

ClassMapper.xml文件部分内容:

<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文件部分内容:

<!-- java属性,数据库表字段之间的映射定义 -->  
<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,根据班级id -->  
<select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap">  
    <include refid="selectStudentAll" />  
    WHERE ST.CLASS_ID = #{classID}  
</select> 

使用resultMap实现聚集 

使用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文件部分内容中。studentResultMap请见上面StudentMapper.xml文件部分内容中。

collection中的ofType="String"时

DTO:

package com.example.mybatis.entity;
import java.util.List;
/**
 * 统计部门下的员工名称(只查询出员工名称)
 */
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 {
    /**
     * 统计部门下的员工名称(只查询出员工名称)
     */
    ListString listStringTest(Integer deptId);
}

表中数据:

在这里插入图片描述

测试:

 /**
    * 统计部门下的员工名称(只查询出员工名称)
    */
    @Test
    public void deptWithEmpNameTest(){
        ListString listString = employeeMapper.listStringTest(1);
        System.out.println(listString);
    }

输出结果:

ListString{deptId=1, empNames=[小红1, 小红2, 小红3, 小红4, 小红5, 小红6, 小红7, 小红8, 小红9, 小红10]}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 10分钟带你理解Java中的弱引用

    10分钟带你理解Java中的弱引用

    这篇文章将带大家快速理解Java中弱引用,文章介绍的很详细,对大家学习Java很有帮助哦,有需要的可以参考借鉴。
    2016-08-08
  • Java 数据结构与算法系列精讲之队列

    Java 数据结构与算法系列精讲之队列

    这篇文章主要介绍了Java队列数据结构的实现,队列是一种特殊的线性表,只允许在表的队头进行删除操作,在表的后端进行插入操作,队列是一个有序表先进先出,想了解更多相关资料的小伙伴可以参考下面文章的详细内容
    2022-02-02
  • mybatis plus in方法使用详解

    mybatis plus in方法使用详解

    这篇文章主要介绍了mybatis plus in方法使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 剖析Spring WebFlux反应式编程设计及工作原理

    剖析Spring WebFlux反应式编程设计及工作原理

    这篇文章主要为大家介绍了Spring WebFlux反应式编程模型工作原理的剖析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-02-02
  • 解决SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题

    解决SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题

    这篇文章主要介绍了SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题的解决方法,需要的朋友可以参考下
    2018-09-09
  • Java设计模式之java策略模式详解

    Java设计模式之java策略模式详解

    这篇文章主要介绍了Java经典设计模式之策略模式,简单说明了策略模式的概念、原理并结合实例形式分析了java策略模式的具有用法与相关注意事项,需要的朋友可以参考下
    2021-09-09
  • SpringBoot2线程池定义使用方法解析

    SpringBoot2线程池定义使用方法解析

    这篇文章主要介绍了SpringBoot2线程池定义使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java中的反射,枚举及lambda表达式的使用详解

    Java中的反射,枚举及lambda表达式的使用详解

    这篇文章主要为大家详细介绍了Java的反射,枚举及lambda表达式,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • Java Collections类操作集合详解

    Java Collections类操作集合详解

    这篇文章主要介绍了Java Collections类操作集合详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • java通过Jsoup爬取网页过程详解

    java通过Jsoup爬取网页过程详解

    这篇文章主要介绍了java通过Jsoup爬取网页过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09

最新评论