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 >#{startTime}</if>
<if test="endTime!=null">and t.update_time <#{endTime}</if>
order by t.update_time
</select>
</mapper>
经过查询,得知collection合并规则是依据id进行区分是否要合在一个collection中的;