Mybatis Association Collection

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值