zoukankan      html  css  js  c++  java
  • mybatis表关联彻底理解

    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 = 1 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>
  • 相关阅读:
    python测试开发django186.使用 jquery 的 .val() 无法获取input框的输入值(已解决) 上海
    2022年第 10 期《python接口web自动化+测试开发》课程,2月13号开学! 上海
    python测试开发django185.bootstraptable 后端搜索功能实现(queryParams) 上海
    python测试开发django184.bootstraptable 前端分页搜索相关配置 上海
    python测试开发django181.自定义过滤器(除法取余) 上海
    python测试开发django180.dockercompose部署django+mysql环境 上海
    python测试开发django183.bootstrapformvalidation重置校验的方法 上海
    pytest文档79 内置 fixtures 之 cache 写入和读取缓存数据 上海
    python测试开发django182.jQuery重置form表单 上海
    golang interface用法
  • 原文地址:https://www.cnblogs.com/panxuejun/p/6168806.html
Copyright © 2011-2022 走看看