| <?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.pt5.pthouduan.mapper.PostMapper"> |
| |
| <!-- 插入帖子 --> |
| <insert id="save" parameterType="com.pt5.pthouduan.entity.Post" |
| useGeneratedKeys="true" keyProperty="postid" keyColumn="postid"> |
| INSERT INTO post ( |
| userid, |
| photo, |
| rannge, |
| is_pinned, |
| post_title, |
| post_content, |
| postCreatedTime, |
| updated_time, |
| tags, |
| likes |
| ) VALUES ( |
| #{userid}, |
| #{photo}, |
| #{rannge}, |
| #{is_pinned}, |
| #{post_title}, |
| #{post_content}, |
| #{postCreatedTime}, |
| #{updated_time}, |
| #{tags}, |
| COALESCE(#{likes}, 0) |
| ) |
| </insert> |
| |
| <!-- 删除帖子 --> |
| <delete id="deleteByPostid" parameterType="int"> |
| DELETE FROM post WHERE postid = #{postid} |
| </delete> |
| |
| <!-- 更新帖子 --> |
| <update id="updatePost" parameterType="com.pt5.pthouduan.entity.Post"> |
| UPDATE post |
| SET |
| userid = #{userid}, |
| photo = #{photo}, |
| rannge = #{rannge}, |
| updated_time = #{updated_time}, |
| is_pinned = #{is_pinned}, |
| post_title = #{post_title}, |
| post_content = #{post_content}, |
| postCreatedTime = #{postCreatedTime}, |
| tags = #{tags}, |
| likes = COALESCE(#{likes}, 0) |
| WHERE postid = #{postid} |
| </update> |
| |
| <!-- 模糊搜索帖子 --> |
| <select id="searchByKeyword" resultType="com.pt5.pthouduan.entity.Post"> |
| SELECT * FROM post |
| WHERE post_title LIKE CONCAT('%', #{keyword}, '%') |
| OR tags LIKE CONCAT('%', #{keyword}, '%') |
| </select> |
| |
| <!-- 点赞 +1 --> |
| <update id="incrementLikes" parameterType="int"> |
| UPDATE post |
| SET likes = likes + 1 |
| WHERE postid = #{postid} |
| </update> |
| |
| <!-- 取消点赞 --> |
| <update id="decrementLikes" parameterType="int"> |
| UPDATE post |
| SET likes = CASE WHEN likes > 0 THEN likes - 1 ELSE 0 END |
| WHERE postid = #{postid} |
| </update> |
| |
| <!-- 更新置顶状态 --> |
| <update id="updatePinnedStatus" parameterType="map"> |
| UPDATE post |
| SET is_pinned = #{pinned} |
| WHERE postid = #{postid} |
| </update> |
| |
| <!-- 根据用户 ID 查询其所有帖子 --> |
| <select id="findByUserid" resultType="com.pt5.pthouduan.entity.Post"> |
| SELECT * FROM post |
| WHERE userid = #{userid} |
| </select> |
| |
| <!-- 查询所有置顶帖子 --> |
| <select id="findPinnedPosts" resultType="com.pt5.pthouduan.entity.Post"> |
| SELECT * FROM post |
| WHERE is_pinned = TRUE |
| </select> |
| |
| <!-- 查询所有帖子,按置顶优先、创建时间倒序 --> |
| <select id="selectAllSorted" resultType="com.pt5.pthouduan.entity.Post"> |
| SELECT * FROM post |
| ORDER BY is_pinned DESC, postCreatedTime DESC |
| </select> |
| |
| </mapper> |