1.多张表关联

三张表,用户表,主播表,关注表。

查询用户已经关注的主播的信息,那就要三张表关联起来啊。分别left join联在一起,通过id相同的连接在一起。最后where查找出最终条件。

<resultMap id="ActorAndUserResultMap" type="com.youyuTech.acSpace.modal.ActorAndUser">
<id column="a_aid" jdbcType="BIGINT" property="aid" />
<result column="a_dynamicBgImgURL" jdbcType="VARCHAR" property="dynamicbgimgurl" />
<result column="a_xiaowoBgImgURL" jdbcType="VARCHAR" property="xiaowobgimgurl" />
<result column="a_follower_count" jdbcType="INTEGER" property="followerCount" />
<result column="a_weight_ratio" jdbcType="DOUBLE" property="weightRatio" />
<result column="a_Recommend_weight" jdbcType="SMALLINT"
property="recommendWeight" />
<result column="a_first_char" jdbcType="VARCHAR" property="firstChar" />
<association property="user" javaType="com.youyuTech.acSpace.modal.User">
<id column="u_uid" jdbcType="BIGINT" property="uid" />
<result column="u_appid" jdbcType="INTEGER" property="appid" />
<result column="u_phone" jdbcType="VARCHAR" property="phone" />
<result column="u_password" jdbcType="VARCHAR" property="password" />
<result column="u_unionid" jdbcType="VARCHAR" property="unionid" />
<result column="u_openid" jdbcType="VARCHAR" property="openid" />
<result column="u_age" jdbcType="INTEGER" property="age" />
<result column="u_birthday" jdbcType="VARCHAR" property="birthday" />
<result column="u_nickname" jdbcType="VARCHAR" property="nickname" />
<result column="u_sex" jdbcType="INTEGER" property="sex" />
<result column="u_email" jdbcType="VARCHAR" property="email" />
<result column="u_qq" jdbcType="VARCHAR" property="qq" />
<result column="u_wechat" jdbcType="VARCHAR" property="wechat" />
<result column="u_province" jdbcType="VARCHAR" property="province" />
<result column="u_city" jdbcType="VARCHAR" property="city" />
<result column="u_country" jdbcType="VARCHAR" property="country" />
<result column="u_headimgurl" jdbcType="VARCHAR" property="headimgurl" />
<result column="u_backgroundimg" jdbcType="VARCHAR" property="backgroundimg" />
<result column="u_description" jdbcType="VARCHAR" property="description" />
<result column="u_channel" jdbcType="VARCHAR" property="channel" />
<result column="u_create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="u_user_type" jdbcType="BIT" property="userType" />
<result column="u_salt" jdbcType="VARCHAR" property="salt" />
</association>
</resultMap>

<sql id="ActorAndUserRow">
a.aid a_aid,
a.dynamicBgImgURL
a_dynamicBgImgURL,
a.xiaowoBgImgURL
a_xiaowoBgImgURL,
a.follower_count
a_follower_count,
a.weight_ratio
a_weight_ratio,
a.Recommend_weight
a_Recommend_weight,
a.first_char
a_first_char,
u.`uid` u_uid,
u.appid
u_appid,
u.phone u_phone,
u.`password`
u_password,
u.unionid u_unionid,
u.openid u_openid,
u.age
u_age,
u.birthday
u_birthday,
u.nickname
u_nickname,
u.sex u_sex,
u.email
u_email,
u.qq
u_qq,
u.wechat u_wechat,
u.province u_province,
u.city
u_city,
u.country
u_country,
u.headimgurl
u_headimgurl,
u.backgroundimg
u_backgroundimg,
u.description
u_description,
u.channel u_channel,
u.create_time
u_create_time,
u.user_type u_user_type,
u.salt u_salt
</sql>

<!-- 用户查看已关注的主播列表 -->
<select id="UserLoveActorList" resultMap="ActorAndUserResultMap">
SELECT
<include refid="ActorAndUserRow" />
FROM
tbl_actor a
LEFT JOIN tbl_user u ON u.uid = a.aid
LEFT JOIN
tbl_follow f
ON f.aid = u.uid
WHERE
f.uid = #{uid} AND f.is_following = limit #{startRow},#{pageSize}
</select>

2.一对一关联

查看主播的详细资料。因为主播的基本信息也在user表中,就需要user表actor表关联起来,通过id相同的关联LEFT JOIN tbl_user u ON u.uid = a.aid

<!-- 查看主播的详细资料 -->
<select id="ActorDetails" resultMap="ActorAndUserResultMap">
SELECT
<include refid="ActorAndUserRow" />
FROM
tbl_actor a
LEFT JOIN tbl_user u ON u.uid = a.aid
WHERE
a.aid =
#{aid}
</select>

mybatis表关联彻底理解的更多相关文章

  1. Mybatis表关联一对多、多对一、多对多

    项目工程结构如下: 1. 搭建MyBatis框架环境 首先需要引入两个包:mybatis.jar 和 sqljdbc42.jar包 若分页需要导入两个包:pagehelper-5.1.0.jar 和 ...

  2. Mybatis表关联多对一

    在上章的 一对多 中,我们已经学习如何在 Mybatis 中关联多表,但在实际项目中也是经常使用 多对一 的情况,这些查询是如何处理的呢,在这一节中我们来学习它.多表映射的多对一关系要用到 mybit ...

  3. Mybatis表关联一对多

    有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如:最常见到的多对一,一对多等.这些查询是如何处理的呢,这一讲就讲这个问题.前面几篇教程中介绍的都是单表映射的一 ...

  4. Mybatis表关联多对多

    创建表 创建表对应的 JavaBean 对象 package com.tanlei.newer.model; import java.util.List; /** * @author:Mr.Tan * ...

  5. Spring+MyBatis框架中sql语句的书写,数据集的传递以及多表关联查询

    在很多Java EE项目中,Spring+MyBatis框架经常被用到,项目搭建在这里不再赘述,现在要将的是如何在项目中书写,增删改查的语句,如何操作数据库,以及后台如何获取数据,如何进行关联查询,以 ...

  6. MyBatis实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  7. MyBatis之多表关联查询

    1使用resultType.ResultMap处理返回结果 处理返回结果 resultType:指定返回值结果的完全限定名,处理多表查询的结果. 多表查询需要定义vo封装查询的结果. 需求:查询部门和 ...

  8. MyBatis——实现关联表查询

    原文:http://www.cnblogs.com/xdp-gacl/p/4264440.html 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创 ...

  9. JAVA入门[9]-mybatis多表关联查询

    概要 本节要实现的是多表关联查询的简单demo.场景是根据id查询某商品分类信息,并展示该分类下的商品列表. 一.Mysql测试数据 新建表Category(商品分类)和Product(商品),并插入 ...

随机推荐

  1. HTML基础2——综合案例1——如何用iis配置网站

      1.打开iis 如果机子上面没有iis,可以先装一个,不同的系统可能安装步骤不一样,至于iis的安装方法,大家可以去百度找找.   2.准备网站源程序 既然要配置网站,肯定要先准备好网站源程序,网 ...

  2. ASP.NET MVC 生成验证码

    using System.Web.Mvc; using System.Drawing; using System; using System.Drawing.Imaging; using Models ...

  3. UML中类之间的关系

    UML中类之间的关系分为以下几种:依赖.关联.泛化.聚合.组合. 依赖是指一个类使用了另一个类,它是一种使用关系,描述了一个事物的规格说明的变化可能会影响到使用它的另一个事物(反之不一定).最常见的依 ...

  4. LN : leetcode 242 Valid Anagram

    lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...

  5. 为什么字符串类型可以调用构造函数String的方法,却又不是它的实例

    从所周知,在js中定义一个字符串我们有两种办法: var a = new String("a"); var a = "a"; 第一种方法使用构造函数创建,作为S ...

  6. WEB-CSS实现单行(多行)文本溢出显示省略号

    //单行文本溢出部分隐藏显示省略号...overflow: hidden; text-overflow:ellipsis; white-space: nowrap; /** n 行文本溢出部分隐藏显示 ...

  7. ionic3带参数返回原来页面

    最近用ionic3+angular4做项目.我遇到了个问题,我返回原来页面时一般都会调用this.navCtrl.pop()方法,但这个方法不能携带参数.怎么办? 可以写个回调方法. 我在a页面定义个 ...

  8. springMVC接收get请求传递多个参数

    @RequestMapping(value = "/sendSignal/{state}/{limberId}/{account}", method = RequestMethod ...

  9. GC策略

           JVM里的GC(Garbage Collection)的算法有很多种,如标记清除收集器,压缩收集器,分代收集器等等,详见HotSpot VM GC 的种类 现在比较常用的是分代收集(ge ...

  10. windows如何统计端口的连接数

    习惯了linux的系统管理员,对linux的命令行工具总是印象极深,几乎所有的管理都可以在命令行下完成.命令行工具是linux系统管理的主流. 而使用windows是,因为图形化的界面,大家习惯了图形 ...