mybatis collection集合合并规则

1.背景

需要进行一对多的查询,但发现得到的集合里面始终仅有一个元素且集合,且集合里面的元素被拆分成多个对象了;

2.示例

在这里插入图片描述

类似于这种效果,可以发现 t.id出现了多次,没合在一起;

3.解决方案

package org.tagging.model;

import java.util.List;

public class Tag {
    private Integer id;
    private Tagging tagging;
    private List<? extends Abbreviation> taggingAbbreviationList;
    private List<? extends UnknownWord> taggingUnknownWords;


    public Tagging getTagging() {
        return tagging;
    }

    public void setTagging(Tagging tagging) {
        this.tagging = tagging;
    }

    public List<? extends Abbreviation> getTaggingAbbreviationList() {
        return taggingAbbreviationList;
    }

    public void setTaggingAbbreviationList(List<? extends Abbreviation> taggingAbbreviationList) {
        this.taggingAbbreviationList = taggingAbbreviationList;
    }

    public List<? extends UnknownWord> getTaggingUnknownWords() {
        return taggingUnknownWords;
    }

    public void setTaggingUnknownWords(List<? extends UnknownWord> taggingUnknownWords) {
        this.taggingUnknownWords = taggingUnknownWords;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Tag{" +
                "tagging=" + tagging +
                ", taggingAbbreviationList=" + taggingAbbreviationList +
                ", taggingUnknownWords=" + taggingUnknownWords +
                '}';
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.tagging.dao.TagMapper">
    <resultMap id="tagResultMap" type="org.tagging.model.Tag">
        <id column="id" property="id" jdbcType="DECIMAL"/>
        <association property="tagging" javaType="org.tagging.model.Tagging">
            <id column="id" property="id" jdbcType="DECIMAL"/>
            <result column="example" property="example"/>
            <result column="using_by" property="usingBy"/>
            <result column="tupdate_by" property="updateBy"/>
            <result column="tupdate_time" property="updateTime"/>
            <result column="example_chinese_manual" property="exampleChineseManual"/>
            <result column="example_chinese_machine" property="exampleChineseMachine"/>
            <result column="update_by_translation" property="updateByTranslation"/>
            <result column="update_by_abbreviation" property="updateByAbbreviation"/>
            <result column="update_by_unknown_word" property="updateByUnknownWord"/>
        </association>
        <collection property="taggingUnknownWords" ofType="org.tagging.model.UnknownWord">
            <id column="uid" property="id"/>
            <result column="uid" property="id"/>
            <result column="tag_id" property="tagId"/>
            <result column="unknown_word" property="unknownWord"/>
            <result column="unknown_word_correct" property="unknownWordCorrect"/>
            <result column="category" property="category"/>
            <result column="uupdate_time" property="updateTime"/>
            <result column="uupdate_by" property="updateBy"/>
            <result column="recommend" property="recommend"/>
        </collection>
        <!--        <collection property="taggingAbbreviationList" ofType="org.tagging.model.Abbreviation">-->
        <!--            <result column="id" property="id"/>-->
        <!--            <result column="tag_id" property="tag_id"/>-->
        <!--            <result column="abbreviation" property="abbreviation"/>-->
        <!--            <result column="abbreviation_complete" property="abbreviationComplete"/>-->
        <!--            <result column="abbreviation_complete_chinese" property="abbreviationCompleteChinese"/>-->
        <!--            <result column="abbreviation_complete_manual" property="abbreviationCompleteManual"/>-->
        <!--            <result column="status" property="status"/>-->
        <!--            <result column="update_by" property="updateBy"/>-->
        <!--            <result column="update_time" property="updateTime"/>-->
        <!--        </collection>-->
    </resultMap>
    <select id="listByTimePeriod" resultMap="tagResultMap">
        select t.id as id, example, example_chinese_machine, example_chinese_manual, t.update_by as tupdate_by,
               using_by, t.update_time as tupdate_time, update_by_translation, update_by_abbreviation,
               update_by_unknown_word, u.id as uid, tag_id, unknown_word, category, remark,
               u.update_time as uupdte_time, u.update_by as uupdate_by, unknown_word_correct, recommend
        from tagging2.tagging_indonesian as t,tagging2.tagging_indonesian_unknown_word as u
        where t.id = u.tag_id
        <if test="startTime!=null">and t.update_time &gt;#{startTime}</if>
        <if test="endTime!=null">and t.update_time &lt;#{endTime}</if>
        order by t.update_time
    </select>

</mapper>

经过查询,得知collection合并规则是依据id进行区分是否要合在一个collection中的;

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值