您的位置:首页 > 其它

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

2014-07-03 11:40 267 查看
先说一对一,是通过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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: