wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 1 | package com.example.g8backend.mapper; |
| 2 | |
| 3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| 4 | import com.example.g8backend.entity.Post; |
| 5 | import org.apache.ibatis.annotations.Mapper; |
| 6 | import org.apache.ibatis.annotations.Param; |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame^] | 7 | import org.apache.ibatis.annotations.Select; |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 8 | |
| 9 | import java.util.List; |
| 10 | |
| 11 | @Mapper |
| 12 | public interface PostMapper extends BaseMapper<Post> { |
| 13 | List<Post> getPostsByUserId(@Param("userId") Long userId); |
夜雨声烦 | 4527a72 | 2025-04-23 17:04:25 +0800 | [diff] [blame^] | 14 | |
| 15 | @Select("<script>" + |
| 16 | "SELECT p.* " + |
| 17 | "FROM posts p " + |
| 18 | "LEFT JOIN post_tag pt ON p.post_id = pt.post_id " + |
| 19 | "LEFT JOIN tags t ON pt.tag_id = t.tag_id " + |
| 20 | "LEFT JOIN users u ON p.user_id = u.user_id " + |
| 21 | "WHERE (p.post_title LIKE CONCAT('%', #{keyword}, '%') OR p.post_content LIKE CONCAT('%', #{keyword}, '%')) " + |
| 22 | "<if test='tagIds != null and tagIds.size() > 0'> " + |
| 23 | "AND pt.tag_id IN " + |
| 24 | "<foreach item='tagId' collection='tagIds' open='(' separator=',' close=')'> " + |
| 25 | "#{tagId} " + |
| 26 | "</foreach> " + |
| 27 | "</if>" + |
| 28 | "<if test='author != null'> " + |
| 29 | "AND u.user_name = #{author} " + |
| 30 | "</if>" + |
| 31 | "</script>") |
| 32 | List<Post> searchPosts(@Param("keyword") String keyword, |
| 33 | @Param("tagIds") List<Long> tagIds, // 改成接受一个tagIds列表 |
| 34 | @Param("author") String author); |
wuchimedes | e5722e3 | 2025-04-13 17:38:50 +0800 | [diff] [blame] | 35 | } |