Mybatis一对一,一对多,自关联

先说一对一,是通过association标签,有两种方式:

1.  

<association property="userInfo" resultMap="com.weida18.mapper.UserInfoMapper.UserInfo"/>
或者

<association property="userInfo" column="userId" javaType="com.weida18.entity.UserInfo">
<id property="userId" column="userId"/>
<result property="idNumber" column="idNumber"/>
<result property="realName" column="realName"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
</association>

这种方式需要使用关联查询,即select a.*,b.* from A a left join B b on a.userid = b.userid where ...........。

2. 

<association property="userInfo" column="id" select="com.weida18.mapper.UserInfoMapper.findUserInfoByUserId<span style="font-family: Arial, Helvetica, sans-serif;">/>

这种方式不需要表关联,在查询时先查询user,再拿着id自动去查userinfo。

一对多关系:

<!-- 表关联 n:n -->
		<collection property="tags" column="id"
			select="com.weida18.mapper.VideoTagMapper.findTagByVideoId"/>

自关联:

<span>	</span><resultMap id="VideoColumn" type="com.weida18.entity.VideoColumn">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="parentId" column="parentId"/>
		<result property="topFlag" column="topFlag"/>
		
		<association property="column" column="parentId" 
			select="com.weida18.mapper.VideoColumnMapper.findVideoColumnById"/>
	
	</resultMap>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值