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. Android 性能优化(19)*代码优化11条技巧:Performance Tips

    Performance Tips 1.In this document Avoid Creating Unnecessary Objects 避免多余的对象 Prefer Static Over Vi ...

  2. 全面学习ORACLE Scheduler特性(10)管理Chains

    5.2  管理Chains 5.2.1  修改Chains属性 基本上碰到修改CHAIN属性的机率不会太大,因此确实没啥可修改的,对于CHAIN对象来说,能够修改的属性只有两个:evaluation_ ...

  3. c语言数据读入---sscanf、fscanf

    #include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> ...

  4. 总结MySQL中SQL语法的使用

    --where子句操作符: where子句操作符 = 等于 <> 不等于(标准语法) != 不等于(非标准语法,可移植性差) < 小于 <= 小于等于 > 大于 > ...

  5. hdu5122 K.Bro Sorting

    思路: 模拟. 实现: #include <iostream> #include <cstdio> using namespace std; ], n, t; int main ...

  6. Codewars练习Python

    计算一个数组的中间数,数的两边和相等,并返回index值 如:数组[1,2,3,4,6] 返回3(数组序号从0开始) def find_even_index(arr): ""&qu ...

  7. Oracle XE WM_CONCAT undifine

    用docker 跑了个oracle XE 报错 没有WM_CONCAT    下载三个sql文件然后按顺序执行后可以正常使用 一:下载三个文件 解压到 oracle 目录下面 (要能找到,注意权限要o ...

  8. (转)MySQL中的索引详讲

    序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...

  9. java.net.MalformedURLException: no protocol: www.baidu.com

    URL url = new URL("www.baidu.com");改为 URL url = new URL("http://www.baidu.com");

  10. 洛谷——P2007 魔方

    P2007 魔方 常神牛家的魔方都是3*3*3的三阶魔方,大家都见过. 模拟即可: #include<iostream> #include<cstdio> #include&l ...