本项目中的SQL语句逻辑整理

需求

记录一下本项目中的SQL语句逻辑。

文章模块

  1. 查看文章的推荐文章
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @Repository
    public interface ArticleDao extends BaseMapper<Article> {
    /**
    * 查看文章的推荐文章
    *
    * @param articleId 文章id
    * @return 文章列表
    */
    List<ArticleRecommendDTO> listRecommendArticles(@Param("articleId") Integer articleId);
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <select id="listRecommendArticles" resultType="com.minzheng.blog.dto.ArticleRecommendDTO">
    SELECT
    id,
    article_title,
    article_cover,
    create_time
    FROM
    (
    SELECT DISTINCT
    article_id
    FROM
    ( SELECT tag_id FROM tb_article_tag WHERE article_id = #{articleId} ) t
    JOIN tb_article_tag t1 ON t.tag_id = t1.tag_id
    WHERE
    article_id != #{articleId}
    ) t2
    JOIN tb_article a ON t2.article_id = a.id
    WHERE a.is_delete = 0
    ORDER BY
    is_top DESC,id DESC
    LIMIT 6
    </select>

    SQL语句逻辑:

    1. 先查询文章标签表,得到当前文章的所有标签id
    2. 再次连接文章标签表,得到与当前文章同标签的所有文章id(排除掉当前文章)
    3. 通过文章id,内连接文章表 获取文章详细信息(排除已删除的文章)(按是否置顶和id排序,取前6条)

角色模块

  1. 查询路由角色列表(查询出所有页面能被哪些角色访问)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @Repository
    public interface RoleDao extends BaseMapper<Role> {

    /**
    * 查询路由角色列表
    *
    * @return 角色标签
    */
    List<ResourceRoleDTO> listResourceRoles();

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <select id="listResourceRoles" resultMap="RolePermissionMap">
    SELECT
    re.id,
    url,
    request_method,
    role_label
    FROM tb_resource re
    LEFT JOIN tb_role_resource rep
    on re.id = rep.resource_id
    LEFT JOIN tb_role r
    on rep.role_id = r.id
    WHERE
    parent_id is NOT NULL
    AND is_anonymous = 0
    </select>

    SQL语句逻辑:

    1. 查询资源表,连接角色资源中间表
    2. 用角色资源中间表,连接角色表,显示出角色信息
    3. 过滤掉非页面的资源(菜单组)(只查询页面),过滤掉不需要登录即可访问的资源(只查询需要登录才可以访问的资源)