ym923 | f1d9f45 | 2025-05-27 18:29:44 +0800 | [diff] [blame] | 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
| 3 | "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| 4 | |
| 5 | <mapper namespace="com.pt5.pthouduan.mapper.PostMapper"> |
| 6 | |
| 7 | <!-- 插入帖子 --> |
| 8 | <insert id="save" parameterType="com.pt5.pthouduan.entity.Post" |
| 9 | useGeneratedKeys="true" keyProperty="postid" keyColumn="postid"> |
| 10 | INSERT INTO post ( |
| 11 | userid, |
| 12 | photo, |
| 13 | rannge, |
| 14 | is_pinned, |
| 15 | post_title, |
| 16 | post_content, |
| 17 | postCreatedTime, |
| 18 | updated_time, |
| 19 | tags, |
| 20 | likes |
| 21 | ) VALUES ( |
| 22 | #{userid}, |
| 23 | #{photo}, |
| 24 | #{rannge}, |
| 25 | #{is_pinned}, |
| 26 | #{post_title}, |
| 27 | #{post_content}, |
| 28 | #{postCreatedTime}, |
| 29 | #{updated_time}, |
| 30 | #{tags}, |
| 31 | COALESCE(#{likes}, 0) |
| 32 | ) |
| 33 | </insert> |
| 34 | |
| 35 | <!-- 删除帖子 --> |
| 36 | <delete id="deleteByPostid" parameterType="int"> |
| 37 | DELETE FROM post WHERE postid = #{postid} |
| 38 | </delete> |
| 39 | |
| 40 | <!-- 更新帖子 --> |
| 41 | <update id="updatePost" parameterType="com.pt5.pthouduan.entity.Post"> |
| 42 | UPDATE post |
| 43 | SET |
| 44 | userid = #{userid}, |
| 45 | photo = #{photo}, |
| 46 | rannge = #{rannge}, |
| 47 | updated_time = #{updated_time}, |
| 48 | is_pinned = #{is_pinned}, |
| 49 | post_title = #{post_title}, |
| 50 | post_content = #{post_content}, |
| 51 | postCreatedTime = #{postCreatedTime}, |
| 52 | tags = #{tags}, |
| 53 | likes = COALESCE(#{likes}, 0) |
| 54 | WHERE postid = #{postid} |
| 55 | </update> |
| 56 | |
| 57 | <!-- 模糊搜索帖子 --> |
| 58 | <select id="searchByKeyword" resultType="com.pt5.pthouduan.entity.Post"> |
| 59 | SELECT * FROM post |
| 60 | WHERE post_title LIKE CONCAT('%', #{keyword}, '%') |
| 61 | OR tags LIKE CONCAT('%', #{keyword}, '%') |
| 62 | </select> |
| 63 | |
| 64 | <!-- 点赞 +1 --> |
| 65 | <update id="incrementLikes" parameterType="int"> |
| 66 | UPDATE post |
| 67 | SET likes = likes + 1 |
| 68 | WHERE postid = #{postid} |
| 69 | </update> |
| 70 | |
| 71 | <!-- 取消点赞 --> |
| 72 | <update id="decrementLikes" parameterType="int"> |
| 73 | UPDATE post |
| 74 | SET likes = CASE WHEN likes > 0 THEN likes - 1 ELSE 0 END |
| 75 | WHERE postid = #{postid} |
| 76 | </update> |
| 77 | |
| 78 | <!-- 更新置顶状态 --> |
| 79 | <update id="updatePinnedStatus" parameterType="map"> |
| 80 | UPDATE post |
ym923 | 4b86221 | 2025-06-06 17:25:46 +0800 | [diff] [blame^] | 81 | SET is_pinned = #{isPinned} |
ym923 | f1d9f45 | 2025-05-27 18:29:44 +0800 | [diff] [blame] | 82 | WHERE postid = #{postid} |
| 83 | </update> |
| 84 | |
| 85 | <!-- 根据用户 ID 查询其所有帖子 --> |
| 86 | <select id="findByUserid" resultType="com.pt5.pthouduan.entity.Post"> |
| 87 | SELECT * FROM post |
| 88 | WHERE userid = #{userid} |
| 89 | </select> |
| 90 | |
| 91 | <!-- 查询所有置顶帖子 --> |
| 92 | <select id="findPinnedPosts" resultType="com.pt5.pthouduan.entity.Post"> |
| 93 | SELECT * FROM post |
| 94 | WHERE is_pinned = TRUE |
| 95 | </select> |
| 96 | |
| 97 | <!-- 查询所有帖子,按置顶优先、创建时间倒序 --> |
| 98 | <select id="selectAllSorted" resultType="com.pt5.pthouduan.entity.Post"> |
| 99 | SELECT * FROM post |
| 100 | ORDER BY is_pinned DESC, postCreatedTime DESC |
| 101 | </select> |
| 102 | |
ym923 | 4b86221 | 2025-06-06 17:25:46 +0800 | [diff] [blame^] | 103 | <!-- ✅ 根据 postid 查询单个帖子 --> |
| 104 | <select id="selectById" parameterType="int" resultType="com.pt5.pthouduan.entity.Post"> |
| 105 | SELECT * FROM post |
| 106 | WHERE postid = #{postid} |
| 107 | </select> |
| 108 | |
ym923 | f1d9f45 | 2025-05-27 18:29:44 +0800 | [diff] [blame] | 109 | </mapper> |