Mybatis一对一,一对多,自关联
2014-07-03 11:40
267 查看
先说一对一,是通过association标签,有两种方式:
1.
这种方式需要使用关联查询,即select a.*,b.* from A a left join B b on a.userid = b.userid where ...........。
2.
这种方式不需要表关联,在查询时先查询user,再拿着id自动去查userinfo。
一对多关系:
自关联:
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>
相关文章推荐
- MyBatis高级映射之-一对一(一对多)关联映射
- mybatis 实现一对一,一对多,多对多关联查询 小结
- Mybatis 一对一、一对多、多对多关联之级联添加
- MyBatis 3(4)关联映射:一对一,一对多
- Mybatis总结(5)--实现关联表查询(一对一、一对多)
- MyBatis实现关联表查询(一对一,一对多,联合查询,嵌套查询)
- mybatis的关联:自关联、一对一、一对多、多对多
- mybatis---一对一双向关联
- Mybatis中一对一关联查询resultMap
- Mybatis 一对一,一对多,多对一,多对多的理解
- MyBatis 一对多 多对一关联 trim foreach
- MyBatis(三)---实现一对一和一对多的关联表查询
- Mybatis 一对一,一对多,多对一,多对多的理解
- 【mybatis深度历险系列】mybatis中的高级映射一对一、一对多、多对多
- MyBatis学习笔记:一对一和一对多关联表查询
- Mybatis 一对一 关联查询 与 resultMap
- mybatis映射 一对一、一对多、多对多高级映射
- MyBatis 详解(一对一,一对多,多对多)
- MyBatis学习(一)之一对一关联映射查询
- MyBatis框架的学习(五)——一对一关联映射和一对多关联映射