浅谈collection标签的oftype属性能否为java.util.Map
作者:gaoshan12345678910 发布时间:2023-03-19 23:16:15
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]}
来源:https://blog.csdn.net/gaoshan12345678910/article/details/82463509


猜你喜欢
- Swagger以及knife4j基本使用Swagger 介绍:官网:https://swagger.io/Swagger是一个规范和完整的框
- -----------------------------------------------------操作---------------
- bean 的生命周期对象创建实例化Bean对象,默认选择无参构造方法,如果只有一个有参构造那么调用有参构造,如果只有多个有参构造那么报错,除
- 前言一直很好奇Android Root的原理,恰好最近碰到了一个跟Android默认带Root权限的问题,这里顺便记录一下Android系统
- 默认日志 Logback :默认情况下,Spring Boot会用Logback来记录日志,并用INFO级别输出到控制台。在运行应用程序和其
- 1.情景展示将要访问的接口地址等常用的配置添加到properties文件中,比直接写到java类中的好处在于:当我们需要修改相应配置时,直接
- pom配置<?xml version="1.0" encoding="UTF-8"?>&
- 昨天弄了一天的Android Studio svn,感觉没有eclipse的svn好装,中间遇到很多的麻烦问题。这里来记录下吧下载下来的时候
- 必须明确告诉DispatcherServlet如何处理MultipartRequest。SpringMVC中提供了文件上传使用方式如下配置x
- 以一个web项目为例,代码是可以移植的首先要导入mail.jar包,然后创建自己的类1:HTMLSender类package com.txq
- 因为这段时间在学习Socket,所以就试着写了一个简单的聊天室。主要分为服务器端和多个客户端。利用服务器端作数据中转站,实现消息群发。1、服
- 本文实例讲述了Android编程获取网络连接方式及判断手机卡所属运营商的方法。分享给大家供大家参考,具体如下:问题:项目中写的网络模块,感觉
- 本文实例讲述了Java自定义标签用法。分享给大家供大家参考,具体如下:简单例子实现一个标签分为两步:(1)继承SimpleTagSuppor
- 序言小编在项目中有遇到使用 flutter 实现扫码枪接入的需求。为方便使用,小编把能力封装成 package 并发布。好记性不如烂笔头,下
- forward_list 概述forward_list 是 C++ 11 新增的容器,它的实现为单链表。forward_list 是支持从容
- 😜shape属性详解<?xml version="1.0" encoding="utf-8"?
- 本文实例为大家分享了Android Studio实现进度条效果的具体代码,供大家参考,具体内容如下实验作业 要求一个进度条,进度随机效果图x
- Android中实现定时器的四种方式第一种方式利用Timer和TimerTask1、继承关系java.util.Timer基本方法sched
- RecyclerView显示Item布局不一致在自定义RecyclerAdapter的时候,在重写onCreateViewHolder方法是
- 使用的是idea+restful风格第一:引入依赖为:<!--poi--> <dependenc