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. Javascript对象基础讲解

    1.Object对象详解 javascript 里最基本的数据类型是对象. avaScript里的对象其实是一个无序的属性集合,属性又是一个个的名-值对. 除了字符串,数字,true,false,nu ...

  2. node-rsa加密,java解密调试

    用NODE RSA JS 加密解密正常,用JAVA RSAUtils工具类加密解密正常.但是用node加密玩的java解密不了.原因:node默认的是 DEFAULT_ENCRYPTION_SCHEM ...

  3. 树莓派连接arduino(USB串口通讯)

    2018-06-0115:12:19 https://blog.csdn.net/song527730241/article/details/50884890 重要步骤  查看端口:(ttyUSB0或 ...

  4. magento 购物车 首页 显示

    如何将购物车显示在你的首页 1.复制代码:<!--new block -->                <block type="checkout/cart_sideb ...

  5. 使用Unittest做单元测试,addTest()单个case的时候却执行全部的case

    参考: http://tieba.baidu.com/p/6008699660 首先造成这个结果的原因是pycharm配置问题 问题验证: 测试代码: import unittest class Te ...

  6. Docker私有仓库的构建

    [root@localhost ~]# vim /etc/sysconfig/docker #INSECURE_REGISTRY='--insecure-registry' INSECURE_REGI ...

  7. Leetcode747至少是其他数字两倍的最大数

    Leetcode747至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 .查找数组中的最大元素是否至少是数组中每个其他数字的两倍.如果是,则返回最大元素的索引,否则返回-1 ...

  8. 顶点的度 (20 分) Java解法

    顶点的度 顶点的图.给定一个有向图,输出各顶点的出度和入度. 输入格式: 输入文件中包含多个测试数据,每个测试数据描述了一个无权有向图.每个测试数据的第一行为两个正整数n 和m,1 ≤ n ≤ 100 ...

  9. Codeforces 280C - Game on Tree

    传送门:280C - Game on Tree 不知道期望是啥的请自行Baidu或Google,(溜了 题目大意,有一棵有根树,每次随机选择一个节点,将这个节点和它的子树删除,问将整棵树删除的期望次数 ...

  10. 【eclipse】外部 jar 包导入教程

    JavaWeb 项目中,可以直接将要导入的 jar 程序包复制到你项目下的 WEB-INF/lib 文件夹下,将来程序移动到别的机子上测试时也能正常运行.如果是普通 Java 工程的话,我们可以在项目 ...