Files
guidance-backend/community-8073/src/main/resources/mapper/PostMapper.xml
苏元皓 117ab31c00 修复bug
2024-09-05 14:06:57 +08:00

118 lines
5.0 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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">
<mapper namespace="com.ivmiku.tutorial.mapper.PostMapper">
<resultMap id="BaseResultMap" type="com.ivmiku.tutorial.entity.Post">
<id property="postId" column="post_id" jdbcType="BIGINT"/>
<result property="userOpenid" column="user_openid" jdbcType="VARCHAR"/>
<result property="communityId" column="community_id" jdbcType="BIGINT"/>
<result property="title" column="title" jdbcType="VARCHAR"/>
<result property="content" column="content" jdbcType="VARCHAR"/>
<result property="videoUrl" column="video_url" jdbcType="VARCHAR"/>
<result property="imageUrls" column="image_urls" jdbcType="VARCHAR"/>
<result property="isDeleted" column="is_deleted" jdbcType="TINYINT"/>
<result property="isOfficial" column="is_official" jdbcType="TINYINT"/>
<result property="isPublic" column="is_public" jdbcType="TINYINT"/>
<result property="location" column="location" jdbcType="VARCHAR"/>
<result property="isDraft" column="is_draft" jdbcType="TINYINT"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
<result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
post_id, user_openid, community_id, title, content,
video_url, image_urls,
is_deleted, is_official, is_public, location, created_at, updated_at
</sql>
<!-- 查询指定帖子的所有评论 -->
<select id="selectCommentsByPostId" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM post
WHERE post_id = #{postId} AND is_deleted = 0
</select>
<!-- 插入新的帖子 -->
<insert id="insertPost" parameterType="com.ivmiku.tutorial.entity.Post">
INSERT INTO post (
post_id, user_openid, community_id, title, content,
video_url, image_urls,
is_deleted, is_official, is_public, location, is_draft, created_at, updated_at
) VALUES (
#{postId}, #{userOpenid}, #{communityId}, #{title}, #{content},
#{videoUrl}, #{imageUrls},
#{isDeleted}, #{isOfficial}, #{isPublic}, #{location}, #{isDraft}, #{createdAt}, #{updatedAt}
)
</insert>
<!-- 更新帖子 -->
<update id="updatePost" parameterType="com.ivmiku.tutorial.entity.Post">
UPDATE post
SET
user_openid = #{userOpenid},
community_id = #{communityId},
title = #{title},
content = #{content},
video_url = #{videoUrl},
image_urls = #{imageUrls},
is_deleted = #{isDeleted},
is_official = #{isOfficial},
is_public = #{isPublic},
location = #{location},
is_draft = #{isDraft},
created_at = #{createdAt},
updated_at = #{updatedAt}
WHERE post_id = #{postId}
</update>
<!-- 删除帖子实际上是将is_deleted字段设置为1 -->
<update id="deletePost">
UPDATE post
SET is_deleted = 1, updated_at = CURRENT_TIMESTAMP
WHERE post_id = #{postId}
</update>
<select id="getTagPostList" resultType="com.ivmiku.tutorial.entity.Post">
SELECT p.post_id, p.user_openid, p.community_id, p.title, p.content,
p.is_deleted, p.is_official, p.is_public, p.location, p.created_at, p.updated_at
FROM post p, posttag pt, tag t
WHERE p.post_id = pt.post_id AND pt.tag_id = t.tag_id AND t.tag_id = #{tagId} AND p.is_deleted = 0 AND p.is_public = 1
</select>
<select id="selectPopularSearches" resultType="java.lang.String">
SELECT title
FROM post
WHERE is_deleted = 0 AND is_public = 1
LIMIT 9
</select>
<select id="searchPosts" resultType="com.ivmiku.tutorial.entity.Post">
SELECT
<include refid="Base_Column_List"/>
FROM post
WHERE is_deleted = 0
AND is_public = 1
AND (title LIKE CONCAT('%', #{keyword}, '%')
OR content LIKE CONCAT('%', #{keyword}, '%'))
</select>
<select id="getTopFavoritedPosts" resultType="com.ivmiku.tutorial.entity.Post">
SELECT p.post_id, p.user_openid, p.community_id, p.title, p.content,
p.is_deleted, p.is_official, p.is_public, p.location, p.created_at, p.updated_at
FROM post p
JOIN favorite f ON p.post_id = f.post_id
WHERE p.is_deleted = 0 AND p.is_public = 1 AND f.is_deleted = 0
GROUP BY p.post_id
ORDER BY COUNT(f.id) DESC
LIMIT #{limit}
</select>
<select id="getPostsBasedOnHistoryAndHotness" resultType="com.ivmiku.tutorial.entity.Post">
SELECT *
FROM post
WHERE content LIKE CONCAT('%', #{searchHistory}, '%')
ORDER BY hotness DESC
</select>
</mapper>