Springboot 2.3.5使用 Mybatis 实现关联查询

代码

  1. 一对一
<!--    -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--这里对应Maper接口类的命名空间-->
<mapper namespace="com.xxxxxx.suddenlynlinelearningplatform.mapper.VideoCommentMaper">
<!--  查询评论  -->
     <resultMap type="com.xxxxxx.suddenlynlinelearningplatform.entity.VideoComment" id="videoCommentInfo">
         <id property="id" column="id"/>
         <association property="student" column="student_id" select="findStudentById">
         </association>
         <association property="repliedStudent" column="replied_student_id" select="findStudentById">
         </association>
         <association property="videoCommentList" column="id" select="findByWhileClassVideoComments">
         </association>
         <association property="myLike" column="{id=id,param2=student_id}"  select="findMyIsLike">
         </association>
     </resultMap>
<!--  一级评论  -->
    <select id="findByFirstLevelVideoComments" resultMap="videoCommentInfo">
        select * from `video_comments` where video_comment_id=0 and status = 1 and video_id=#{param1}
    </select>
<!--    二级评论-->
    <select id="findByWhileClassVideoComments" resultMap="videoCommentInfo">
        select * from `video_comments` where status = 1 and video_comment_id=#{video_comment_id}
    </select>
<!--    查询学生-->
    <select id="findStudentById" parameterType="int" resultType="com.xxxxxx.suddenlynlinelearningplatform.entity.VideoCommentStudent">
        select id,name,avatar_url,bewrite from `students` where id = #{id}
    </select>
<!--    查询自己是否点赞了这个博客-->
    <select id="findMyIsLike" resultType="int">
        select count(1) from `video_comment_likes` where video_comment_id=#{id} and student_id=#{param2} limit 1
    </select>
</mapper>
如果觉得文章对您有帮助,希望您能 关注+推荐 哦
原文地址:https://www.cnblogs.com/xiaqiuchu/p/15182732.html