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. MyEclipse无法自动编译项目故障一例

    MyEclipse导入项目后发现无法自动编译,classes目录下没有编译的类. 尝试的解决方法: 1.刷新项目,失败: 2.project->clean-,失败: 3.关闭项目再次打开,失败: ...

  2. [译]curl_multi_info_read

    curl_multi_info_read - read multi stack informationals读取multi stack中的信息 SYNOPSIS#include <curl/cu ...

  3. 生成清除某个数据库下的所有表的SQL语句

    方法1:重建库和表 用mysqldump --no-data把建表SQL导出来,然后drop database再create database,执行一下导出的SQL文件: 方法2:生成清空所有表的SQ ...

  4. oracle性能优化培训总结

  5. [ HDOJ 3826 ] Squarefree number

    \(\\\) \(Description\) \(T\)组数据,每次给出一个正整数 \(N\) ,判断其是否能被任意一个完全平方数整除. \(T\le 20,N\le 10^{18}\) \(\\\) ...

  6. Android Camera 3D效果

    一.概念 在Android中要想实现3D效果,第一个想到的应该就是OpenGL ES,因为在很多基础教材中几乎都提到了它.但是其使用起来还是稍微麻烦一些,而且它也主要用在游戏方面,那在应用方面有没有更 ...

  7. webSql的简单小例子

    初始化websql数据库的参数信息 var config = { name: 'my_plan', version: '', desc: 'manage my plans', size: 20 * 1 ...

  8. HDU_1556_线段树区间更新

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. ltp-ddt qspi_mtd_dd_rw error can't read superblock on /dev/mtdblock0

    can't read superblock on /dev/mtdblock0 1.fsck /dev/xxx 2.e2fsck -b 8193 <device> e2fsck -b 32 ...

  10. Error LNK2019: unresolved external symbol C++模板类声明与定义链接错误问题

    编译器在编译模板时,并不会生成代码,只有遇到实例化的时候才会生成代码.因此,当我们只引用模板声明文件的时候,在实例化的对象时候,模板的定义问文件是不可见的,于是出现链接错误.例如: //A.h #pr ...