信息发布→ 登录 注册 退出

浅谈collection标签的oftype属性能否为java.util.Map

发布时间:2026-01-11

点击量:
目录
  • collection标签的oftype属性能否为java.util.Map
    • collection聚集
    • 使用select实现聚集 
    • 使用resultMap实现聚集 
  • collection中的ofType="String"时

    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]}

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

    在线客服
    服务热线

    服务热线

    4008888355

    微信咨询
    二维码
    返回顶部
    ×二维码

    截屏,微信识别二维码

    打开微信

    微信号已复制,请打开微信添加咨询详情!