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" />FROMtbl_actor aLEFT JOIN tbl_user u ON u.uid = a.aidLEFT JOINtbl_follow fON f.aid = u.uidWHEREf.uid = #{uid} AND f.is_following =1limit #{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" />FROMtbl_actor aLEFT JOIN tbl_user u ON u.uid = a.aidWHEREa.aid =#{aid}</select>?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的mybatis表关联彻底理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 双击和手势的图片缩放
- 下一篇: 【分布式】Zookeeper请求处理