MyBatis,collection标签和association标签区别?为什么一对多时,用两个都一样的
比如同时有A.java和B.java两个类,A.java如下: public class A{ private B b1; private List<B> b2; } 在映射b1属性时用association标签, 映射b2时用collection标签,分别是一对一,一对多的关系
一对一:关联对象(如:User)或者一个属性(如:name) , 用 association
一对多:关联一个集合(如:userList) , 用 collection
然而 association 也可以用出 collection的效果,不过规范书写总是好的。
<resultMap type="com.camelot.core.domain.CmsConfig" id="CmsConfigResultMap">
<id property="id" column="config_id"/>
<result property="contextPath" column="context_path"/>
<result property="port" column="port"/>
<result property="dbFileUri" column="db_file_uri"/>
<result property="uploadToDb" column="is_upload_to_db"/>
<result property="defImg" column="def_img"/>
<result property="loginUrl" column="login_url"/>
<result property="processUrl" column="process_url"/>
<result property="countClearTime" column="count_clear_time" />
<result property="countCopyTime" column="count_copy_time"/>
<result property="downloadCode" column="download_code"/>
<result property="downloadTime" column="download_time"/>
<result property="emailValidate" column="email_validate"/>
<result property="viewOnlyChecked" column="view_only_checked"/>
<association property="m_markConfig" column="config_id" select="getMarkConfigByCfgId"/>
<collection property="listMapAttr" column="config_id" select="findCmsConfigAttrByCfgId"/>
</resultMap>
<resultMap type="com.camelot.core.domain.MarkConfig" id="MarkConfigResultMap">
<result property="on" column="mark_on"/>
<result property="minWidth" column="mark_width"/>
<result property="minHeight" column="mark_height"/>
<result property="imagePath" column="mark_image"/>
<result property="content" column="mark_content"/>
<result property="size" column="mark_size"/>
<result property="color" column="mark_color"/>
<result property="alpha" column="mark_alpha"/>
<result property="pos" column="mark_position"/>
<result property="offsetX" column="mark_offset_x"/>
<result property="offsetY" column="mark_offset_y"/>
</resultMap>
<select id="getMarkConfigByCfgId" resultMap="MarkConfigResultMap"></span>
SELECT
mark_on,
mark_width,
mark_height,
mark_image,
mark_content,
mark_size,
mark_color,
mark_alpha,
mark_position,
mark_offset_x,
mark_offset_y
FROM
jc_config
where
config_id = #{cfgId}
</select>
<resultMap type="java.util.HashMap" id="CmsConfigAttrResultMap">
<result property="name" column="attr_name"/>
<result property="value" column="attr_value"/>
</resultMap>
<select id="findCmsConfigAttrByCfgId" resultMap="CmsConfigAttrResultMap" >
select
attr_name,attr_value
from
jc_config_attr
where
config_id = #{cfgId}
</select>
参考:
http://www.cnblogs.com/xdp-gacl/p/4264440.html (写得最好)
http://blog.csdn.net/isea533/article/details/20868189
http://cczakai.iteye.com/blog/1276722