你想 去掉 post 表中的 likeCount 字段,改为通过动态统计 like_post 表来获取点赞数。这种设计利于数据同步和准确性,避免字段冗余造成的计数不一致。下面是详细的方案和代码示例,包含:
数据库 post 表去掉 likeCount 字段
查询帖子详情和帖子列表时,使用子查询或联表查询点赞数
点赞与取消点赞操作只维护 like_post 表,不更新 post 表
1. 数据库层面
在数据库中,执行 ALTER 语句 删除 post 表的 like_count 字段(如果已有且不使用)
sql
ALTER TABLE post DROP COLUMN like_count;
确保点赞计数只存储在 like_post 表里。
2. 查询帖子详情时动态统计点赞
假设你用的是 MyBatis,改写selectPostById SQL,联结计算点赞数:
xml
<select id="selectPostById" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
IFNULL(like_counts.count, 0) AS likeCount
FROM post p
LEFT JOIN user u ON p.user_id = u.id
LEFT JOIN (
SELECT post_id, COUNT(*) AS count
FROM like_post
GROUP BY post_id
) like_counts ON like_counts.post_id = p.id
WHERE p.id = #{id}
</select>
你的 selectPostById 和 selectPostsByUserId 等SQL查询语句里,必须要带有点赞数统计(通过子查询或 LEFT JOIN like_post表分组计数),返回点赞数字段 likeCount。
如果没有,点赞数会是 null 或者默认值,前端显示不对。
业务层没给 post 对象设置点赞数:
如果SQL查询本来没返回这个likeCount字段,或者返回了但没有设置到post实体,也会导致点赞数未更新。
解决方案
一、确保你的 Mapper 查询返回了点赞数
以 selectPostById 为例,示范带点赞数的SQL:
xml
<select id="selectPostById" parameterType="int" resultMap="postResultMap">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
IFNULL(like_counts.count, 0) AS likeCount
FROM post p
LEFT JOIN user u ON p.user_id = u.id
LEFT JOIN (
SELECT post_id, COUNT(*) AS count FROM like_post GROUP BY post_id
) like_counts ON like_counts.post_id = p.id
WHERE p.id = #{id}
</select>
<!-- 配置resultMap对应属性映射到Post实体 -->
类似,selectPostsByUserId也要加类似联表点赞数统计。
二、Post 实体要有 likeCount 字段与 getter/setter
java
private Integer likeCount;
public Integer getLikeCount() {
return likeCount;
}
public void setLikeCount(Integer likeCount) {
this.likeCount = likeCount;
}
<select id="selectPostById" parameterType="int" resultMap="postResultMap">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
IFNULL(like_counts.count, 0) AS likeCount
FROM post p
LEFT JOIN user u ON p.user_id = u.id
LEFT JOIN (
SELECT post_id, COUNT(*) AS count FROM like_post GROUP BY post_id
) like_counts ON like_counts.post_id = p.id
WHERE p.id = #{id}
</select>
@Resource
private ForumMapper forumMapper;
// 帖子相关服务
public int createPost(Post post) {
return forumMapper.insertPost(post);
}
public int deletePost(Integer postId) {
return forumMapper.deletePostById(postId);
}
public int updatePost(Post post) {
return forumMapper.updatePost(post);
}
public Post getPostById(Integer postId) {
return forumMapper.selectPostById(postId);
}
public List<Post> searchPostsByTitle(String title) {
return forumMapper.selectPostsByTitleLike(title);
}
// 评论相关服务
public int createComment(Comment comment) {
return forumMapper.insertComment(comment);
}
public int deleteComment(Integer commentId) {
return forumMapper.deleteCommentById(commentId);
}
public int updateComment(Comment comment) {
return forumMapper.updateComment(comment);
}
public List<Comment> getCommentsByPostId(Integer postId) {
return forumMapper.selectCommentsByPostId(postId);
}
// 点赞相关
public int createLikePost(LikePost likePost) {
// 先查询是否已有点赞,防止重复
LikePost exist = forumMapper.selectLikePost(likePost.getUserId(), likePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertLikePost(likePost);
}
public int deleteLikePost(Integer userId, Integer postId) {
return forumMapper.deleteLikePost(userId, postId);
}
public LikePost getLikePost(Integer userId, Integer postId) {
return forumMapper.selectLikePost(userId, postId);
}
// 收藏相关
public int createFavoritePost(FavoritePost favoritePost) {
FavoritePost exist = forumMapper.selectFavoritePost(favoritePost.getUserId(), favoritePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertFavoritePost(favoritePost);
}
public int deleteFavoritePost(Integer userId, Integer postId) {
return forumMapper.deleteFavoritePost(userId, postId);
}
public FavoritePost getFavoritePost(Integer userId, Integer postId) {
return forumMapper.selectFavoritePost(userId, postId);
}
// 举报相关
public int createReportPost(ReportPost reportPost) {
// 可以做举报重复校验,暂略
return forumMapper.insertReportPost(reportPost);
}
public List<ReportPost> getReportPostsByPostId(Integer postId) {
return forumMapper.selectReportPostsByPostId(postId);
}
public List<Post> getPostsByUserId(Integer userId) {
return forumMapper.selectPostsByUserId(userId);
}
public int getLikeCountByPostId(Integer postId) {
return forumMapper.selectLikeCountByPostId(postId);
}
}
code
<!-- 插入帖子 -->
<insert id="insertPost" parameterType="com.example.springboot.entity.Forum.Post" useGeneratedKeys="true" keyProperty="id">
INSERT INTO post(user_id,title,content,created_at,updated_at)
VALUES(#{userId}, #{title}, #{content}, NOW(), NOW())
</insert>
<!-- 删除帖子 -->
<delete id="deletePostById" parameterType="int">
DELETE FROM post WHERE id = #{id}
</delete>
<!-- 更新帖子 -->
<update id="updatePost" parameterType="com.example.springboot.entity.Forum.Post">
UPDATE post
SET title = #{title}, content = #{content}, updated_at = NOW()
WHERE id = #{id}
</update>
<!-- 根据id查询帖子(带用户名) -->
<select id="selectPostById" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
LEFT JOIN user u ON p.user_id = u.id
WHERE p.id = #{id}
</select>
<!-- 模糊搜索帖子的标题 -->
<select id="selectPostsByTitleLike" parameterType="string" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
LEFT JOIN user u ON p.user_id = u.id
WHERE p.title LIKE CONCAT('%', #{title}, '%')
ORDER BY p.created_at DESC
</select>
<!-- 新增评论 -->
<insert id="insertComment" parameterType="com.example.springboot.entity.Forum.Comment" useGeneratedKeys="true" keyProperty="id">
INSERT INTO comment(post_id, user_id, parent_comment_id, content, created_at, updated_at)
VALUES(#{postId}, #{userId}, #{parentCommentId}, #{content}, NOW(), NOW())
</insert>
<!-- 删除评论 -->
<delete id="deleteCommentById" parameterType="int">
DELETE FROM comment WHERE id = #{id}
</delete>
<!-- 修改评论内容 -->
<update id="updateComment" parameterType="com.example.springboot.entity.Forum.Comment">
UPDATE comment
SET content = #{content}, updated_at = NOW()
WHERE id = #{id}
</update>
<!-- 查询某帖子下所有评论(带用户名) -->
<select id="selectCommentsByPostId" parameterType="int" resultType="com.example.springboot.entity.Forum.Comment">
SELECT
c.id,
c.post_id AS postId,
c.user_id AS userId,
u.username,
c.parent_comment_id AS parentCommentId,
c.content,
c.created_at AS createdAt,
c.updated_at AS updatedAt
FROM comment c
LEFT JOIN user u ON c.user_id = u.id
WHERE c.post_id = #{postId}
ORDER BY c.created_at ASC
</select>
<!-- 点赞 -->
<insert id="insertLikePost" parameterType="com.example.springboot.entity.Forum.LikePost" useGeneratedKeys="true" keyProperty="id">
INSERT INTO like_post(user_id, post_id, created_at) VALUES(#{userId}, #{postId}, NOW())
</insert>
<delete id="deleteLikePost" parameterType="map">
DELETE FROM like_post WHERE user_id = #{userId} AND post_id = #{postId}
</delete>
<select id="selectLikePost" parameterType="map" resultType="com.example.springboot.entity.Forum.LikePost">
SELECT * FROM like_post WHERE user_id = #{userId} AND post_id = #{postId} LIMIT 1
</select>
<!-- 收藏 -->
<insert id="insertFavoritePost" parameterType="com.example.springboot.entity.Forum.FavoritePost" useGeneratedKeys="true" keyProperty="id">
INSERT INTO favorite_post(user_id, post_id, created_at) VALUES(#{userId}, #{postId}, NOW())
</insert>
<delete id="deleteFavoritePost" parameterType="map">
DELETE FROM favorite_post WHERE user_id = #{userId} AND post_id = #{postId}
</delete>
<select id="selectFavoritePost" parameterType="map" resultType="com.example.springboot.entity.Forum.FavoritePost">
SELECT * FROM favorite_post WHERE user_id = #{userId} AND post_id = #{postId} LIMIT 1
</select>
<!-- 举报 -->
<insert id="insertReportPost" parameterType="com.example.springboot.entity.Forum.ReportPost" useGeneratedKeys="true" keyProperty="id">
INSERT INTO report_post(reporter_id, post_id, reason, created_at) VALUES(#{reporterId}, #{postId}, #{reason}, NOW())
</insert>
<select id="selectReportPostsByPostId" parameterType="int" resultType="com.example.springboot.entity.Forum.ReportPost">
SELECT * FROM report_post WHERE post_id = #{postId} ORDER BY created_at DESC
</select>
<!-- 查询某用户的所有帖子 -->
<select id="selectPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
LEFT JOIN user u ON p.user_id = u.id
WHERE p.user_id = #{userId}
ORDER BY p.created_at DESC
</select>
<!-- 查询某帖子的点赞数量 -->
<select id="selectLikeCountByPostId" parameterType="int" resultType="int">
SELECT COUNT(*) FROM like_post WHERE post_id = #{postId}
</select>
<!-- 查询用户点赞过的帖子 -->
<select id="selectLikedPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
INNER JOIN like_post lp ON p.id = lp.post_id
LEFT JOIN user u ON p.user_id = u.id
WHERE lp.user_id = #{userId}
ORDER BY lp.created_at DESC
</select>
<!-- 查询用户收藏过的帖子 -->
<select id="selectFavoritedPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
INNER JOIN favorite_post fp ON p.id = fp.post_id
LEFT JOIN user u ON p.user_id = u.id
WHERE fp.user_id = #{userId}
ORDER BY fp.created_at DESC
</select>
5. 说明与注意事项
接口路径
GET /forum/user/{userId}/liked-posts,返回该用户点赞过的所有帖子列表
GET /forum/user/{userId}/favorited-posts,返回该用户收藏过的所有帖子列表
CREATE TABLE like_post (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
post_id INT NOT NULL,
created_at DATETIME NOT NULL,
UNIQUE KEY uniq_user_post (user_id, post_id)
);
CREATE TABLE favorite_post (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
post_id INT NOT NULL,
created_at DATETIME NOT NULL,
UNIQUE KEY uniq_user_post (user_id, post_id)
);
@RestController
@RequestMapping("/forum")
public class ForumController {
code
@Resource
private ForumService forumService;
// 发布帖子
@PostMapping("/post")
public String createPost(@RequestBody Post post) {
int result = forumService.createPost(post);
return result > 0 ? "Post created successfully" : "Post creation failed";
}
// 删除帖子(普通用户需要在业务层验证是否本人)
@DeleteMapping("/post/{id}")
public String deletePost(@PathVariable Integer id) {
int result = forumService.deletePost(id);
return result > 0 ? "Post deleted successfully" : "Post deletion failed";
}
// 修改帖子
@PutMapping("/post")
public String updatePost(@RequestBody Post post) {
int result = forumService.updatePost(post);
return result > 0 ? "Post updated successfully" : "Post update failed";
}
// 根据帖子ID获取帖子详情
@GetMapping("/post/{id}")
public Post getPostById(@PathVariable Integer id) {
return forumService.getPostById(id);
}
// 模糊查询帖子标题
@GetMapping("/posts")
public List<Post> searchPostsByTitle(@RequestParam String title) {
return forumService.searchPostsByTitle(title);
}
// 新增评论
@PostMapping("/comment")
public String createComment(@RequestBody Comment comment) {
int result = forumService.createComment(comment);
return result > 0 ? "Comment added successfully" : "Comment adding failed";
}
// 删除评论
@DeleteMapping("/comment/{id}")
public String deleteComment(@PathVariable Integer id) {
int result = forumService.deleteComment(id);
return result > 0 ? "Comment deleted successfully" : "Comment deletion failed";
}
// 修改评论
@PutMapping("/comment")
public String updateComment(@RequestBody Comment comment) {
int result = forumService.updateComment(comment);
return result > 0 ? "Comment updated successfully" : "Comment update failed";
}
// 获取某帖子的评论列表(包含回复)
@GetMapping("/comments/{postId}")
public List<Comment> getCommentsByPostId(@PathVariable Integer postId) {
return forumService.getCommentsByPostId(postId);
}
// 点赞帖子
@PostMapping("/post/like")
public String likePost(@RequestBody LikePost likePost) {
int result = forumService.createLikePost(likePost);
return result > 0 ? "点赞成功" : "已经点赞过或点赞失败";
}
// 取消点赞
@DeleteMapping("/post/like")
public String unlikePost(@RequestParam Integer userId, @RequestParam Integer postId) {
int result = forumService.deleteLikePost(userId, postId);
return result > 0 ? "取消点赞成功" : "取消点赞失败";
}
// 收藏帖子
@PostMapping("/post/favorite")
public String favoritePost(@RequestBody FavoritePost favoritePost) {
int result = forumService.createFavoritePost(favoritePost);
return result > 0 ? "收藏成功" : "收藏失败或已收藏";
}
// 取消收藏
@DeleteMapping("/post/favorite")
public String unfavoritePost(@RequestParam Integer userId, @RequestParam Integer postId) {
int result = forumService.deleteFavoritePost(userId, postId);
return result > 0 ? "取消收藏成功" : "取消收藏失败";
}
// 举报帖子
@PostMapping("/post/report")
public String reportPost(@RequestBody ReportPost reportPost) {
int result = forumService.createReportPost(reportPost);
return result > 0 ? "举报成功" : "举报失败";
}
// 管理员查看举报
@GetMapping("/post/reports/{postId}")
public List<ReportPost> getReportsByPostId(@PathVariable Integer postId) {
return forumService.getReportPostsByPostId(postId);
}
@GetMapping("/user/{userId}/posts")
public List<Post> getPostsByUserId(@PathVariable Integer userId) {
List<Post> posts = forumService.getPostsByUserId(userId);
// 这里可以查询并设置每个帖子的点赞数,点赞和收藏状态,比如(简单示例,效率不错的话可DAO一次搞定):
posts.forEach(post -> {
int likeCount = forumService.getLikeCountByPostId(post.getId());
post.setLikeCount(likeCount);
// 查询当前用户是否点赞/收藏,改成传参可改
// 这里暂时userId是查询者,请改成当前登录用户id
LikePost likePost = forumService.getLikePost(userId, post.getId());
post.setLiked(likePost != null);
FavoritePost favPost = forumService.getFavoritePost(userId, post.getId());
post.setFavorited(favPost != null);
});
return posts;
}
/**
* 获取用户点赞过的帖子列表
*/
@GetMapping("/user/{userId}/liked-posts")
public List<Post> getLikedPostsByUserId(@PathVariable Integer userId) {
return forumService.getLikedPostsByUserId(userId);
}
/**
* 获取用户收藏过的帖子列表
*/
@GetMapping("/user/{userId}/favorited-posts")
public List<Post> getFavoritedPostsByUserId(@PathVariable Integer userId) {
return forumService.getFavoritedPostsByUserId(userId);
}
}
code
<!-- 插入帖子 -->
<insert id="insertPost" parameterType="com.example.springboot.entity.Forum.Post" useGeneratedKeys="true" keyProperty="id">
INSERT INTO post(user_id,title,content,created_at,updated_at)
VALUES(#{userId}, #{title}, #{content}, NOW(), NOW())
</insert>
<!-- 删除帖子 -->
<delete id="deletePostById" parameterType="int">
DELETE FROM post WHERE id = #{id}
</delete>
<!-- 更新帖子 -->
<update id="updatePost" parameterType="com.example.springboot.entity.Forum.Post">
UPDATE post
SET title = #{title}, content = #{content}, updated_at = NOW()
WHERE id = #{id}
</update>
<!-- 根据id查询帖子(带用户名) -->
<select id="selectPostById" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
LEFT JOIN user u ON p.user_id = u.id
WHERE p.id = #{id}
</select>
<!-- 模糊搜索帖子的标题 -->
<select id="selectPostsByTitleLike" parameterType="string" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
LEFT JOIN user u ON p.user_id = u.id
WHERE p.title LIKE CONCAT('%', #{title}, '%')
ORDER BY p.created_at DESC
</select>
<!-- 新增评论 -->
<insert id="insertComment" parameterType="com.example.springboot.entity.Forum.Comment" useGeneratedKeys="true" keyProperty="id">
INSERT INTO comment(post_id, user_id, parent_comment_id, content, created_at, updated_at)
VALUES(#{postId}, #{userId}, #{parentCommentId}, #{content}, NOW(), NOW())
</insert>
<!-- 删除评论 -->
<delete id="deleteCommentById" parameterType="int">
DELETE FROM comment WHERE id = #{id}
</delete>
<!-- 修改评论内容 -->
<update id="updateComment" parameterType="com.example.springboot.entity.Forum.Comment">
UPDATE comment
SET content = #{content}, updated_at = NOW()
WHERE id = #{id}
</update>
<!-- 查询某帖子下所有评论(带用户名) -->
<select id="selectCommentsByPostId" parameterType="int" resultType="com.example.springboot.entity.Forum.Comment">
SELECT
c.id,
c.post_id AS postId,
c.user_id AS userId,
u.username,
c.parent_comment_id AS parentCommentId,
c.content,
c.created_at AS createdAt,
c.updated_at AS updatedAt
FROM comment c
LEFT JOIN user u ON c.user_id = u.id
WHERE c.post_id = #{postId}
ORDER BY c.created_at ASC
</select>
<!-- 点赞 -->
<insert id="insertLikePost" parameterType="com.example.springboot.entity.Forum.LikePost" useGeneratedKeys="true" keyProperty="id">
INSERT INTO like_post(user_id, post_id, created_at) VALUES(#{userId}, #{postId}, NOW())
</insert>
<delete id="deleteLikePost" parameterType="map">
DELETE FROM like_post WHERE user_id = #{userId} AND post_id = #{postId}
</delete>
<select id="selectLikePost" parameterType="map" resultType="com.example.springboot.entity.Forum.LikePost">
SELECT * FROM like_post WHERE user_id = #{userId} AND post_id = #{postId} LIMIT 1
</select>
<!-- 收藏 -->
<insert id="insertFavoritePost" parameterType="com.example.springboot.entity.Forum.FavoritePost" useGeneratedKeys="true" keyProperty="id">
INSERT INTO favorite_post(user_id, post_id, created_at) VALUES(#{userId}, #{postId}, NOW())
</insert>
<delete id="deleteFavoritePost" parameterType="map">
DELETE FROM favorite_post WHERE user_id = #{userId} AND post_id = #{postId}
</delete>
<select id="selectFavoritePost" parameterType="map" resultType="com.example.springboot.entity.Forum.FavoritePost">
SELECT * FROM favorite_post WHERE user_id = #{userId} AND post_id = #{postId} LIMIT 1
</select>
<!-- 举报 -->
<insert id="insertReportPost" parameterType="com.example.springboot.entity.Forum.ReportPost" useGeneratedKeys="true" keyProperty="id">
INSERT INTO report_post(reporter_id, post_id, reason, created_at) VALUES(#{reporterId}, #{postId}, #{reason}, NOW())
</insert>
<select id="selectReportPostsByPostId" parameterType="int" resultType="com.example.springboot.entity.Forum.ReportPost">
SELECT * FROM report_post WHERE post_id = #{postId} ORDER BY created_at DESC
</select>
<!-- 查询某用户的所有帖子 -->
<select id="selectPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
LEFT JOIN user u ON p.user_id = u.id
WHERE p.user_id = #{userId}
ORDER BY p.created_at DESC
</select>
<!-- 查询某帖子的点赞数量 -->
<select id="selectLikeCountByPostId" parameterType="int" resultType="int">
SELECT COUNT(*) FROM like_post WHERE post_id = #{postId}
</select>
<!-- 查询用户点赞过的帖子 -->
<select id="selectLikedPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
INNER JOIN like_post lp ON p.id = lp.post_id
LEFT JOIN user u ON p.user_id = u.id
WHERE lp.user_id = #{userId}
ORDER BY lp.created_at DESC
</select>
<!-- 查询用户收藏过的帖子 -->
<select id="selectFavoritedPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
INNER JOIN favorite_post fp ON p.id = fp.post_id
LEFT JOIN user u ON p.user_id = u.id
WHERE fp.user_id = #{userId}
ORDER BY fp.created_at DESC
</select>
public interface ForumMapper {
// 点赞表相关接口...
// 点赞数加1
int incrementLikeCount(@Param("postId") Integer postId);
// 点赞数减1,且不低于0
int decrementLikeCount(@Param("postId") Integer postId);
// 其他接口...
}
3. 添加 Mapper XML 实现
xml
<!-- 点赞数+1 -->
<update id="incrementLikeCount" parameterType="int">
UPDATE post
SET like_count = like_count + 1
WHERE id = #{postId}
</update>
<!-- 点赞数-1,且不低于0 -->
<update id="decrementLikeCount" parameterType="int">
UPDATE post
SET like_count = CASE WHEN like_count > 0 THEN like_count - 1 ELSE 0 END
WHERE id = #{postId}
</update>
4. 查询帖子详情及帖子列表的 SQL 确保读取 like_count
你已经有查询语句里的likeCount从post表正确返回了,不用变动:
举例(selectPostById):
xml
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
p.like_count AS likeCount
FROM post p
LEFT JOIN user u ON p.user_id = u.id
WHERE p.id = #{id}
类似地,帖子列表查询也要确保like_count字段被查询。
5. 你的点赞接口Controller保持不变
java
@PostMapping("/post/like")
public String likePost(@RequestBody LikePost likePost) {
int result = forumService.createLikePost(likePost);
return result > 0 ? "点赞成功" : "已经点赞过或点赞失败";
}
@DeleteMapping("/post/like")
public String unlikePost(@RequestParam Integer userId, @RequestParam Integer postId) {
int result = forumService.deleteLikePost(userId, postId);
return result > 0 ? "取消点赞成功" : "取消点赞失败";
}
<select id="selectPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
IFNULL(like_counts.count, 0) AS likeCount
FROM post p
LEFT JOIN user u ON p.user_id = u.id
LEFT JOIN (
SELECT post_id, COUNT(*) AS count
FROM like_post
GROUP BY post_id
) like_counts ON like_counts.post_id = p.id
WHERE p.user_id = #{userId}
ORDER BY p.created_at DESC
</select>
同样,子查询统计点赞数,联接帖子,返回点赞字段
4. 点赞、取消点赞接口逻辑(Service层)
点赞实现只往like_post表插入记录
取消点赞只删除like_post表对应记录
不维护post表的likeCount
java
// 点赞
public int createLikePost(LikePost likePost) {
LikePost exist = forumMapper.selectLikePost(likePost.getUserId(), likePost.getPostId());
if (exist != null) return 0; // 已经点赞
return forumMapper.insertLikePost(likePost);
}
// 取消点赞
public int deleteLikePost(Integer userId, Integer postId) {
return forumMapper.deleteLikePost(userId, postId);
}
-- 查询单个帖子点赞数
SELECT COUNT(*) FROM like_post WHERE post_id = #{postId};
-- 查询多个帖子的点赞数,用于列表
SELECT post_id, COUNT(*) AS count FROM like_post WHERE post_id IN (...) GROUP BY post_id;
你可以根据需要在自定义Mapper中扩展批量查询点赞数的接口。
有任何具体代码需求,都可随时告诉我!
@Resource
private ForumMapper forumMapper;
// 帖子相关服务
public int createPost(Post post) {
return forumMapper.insertPost(post);
}
public int deletePost(Integer postId) {
return forumMapper.deletePostById(postId);
}
public int updatePost(Post post) {
return forumMapper.updatePost(post);
}
public Post getPostById(Integer postId) {
return forumMapper.selectPostById(postId);
}
public List<Post> searchPostsByTitle(String title) {
return forumMapper.selectPostsByTitleLike(title);
}
// 评论相关服务
public int createComment(Comment comment) {
return forumMapper.insertComment(comment);
}
public int deleteComment(Integer commentId) {
return forumMapper.deleteCommentById(commentId);
}
public int updateComment(Comment comment) {
return forumMapper.updateComment(comment);
}
public List<Comment> getCommentsByPostId(Integer postId) {
return forumMapper.selectCommentsByPostId(postId);
}
// 点赞相关
public int createLikePost(LikePost likePost) {
// 先查询是否已有点赞,防止重复
LikePost exist = forumMapper.selectLikePost(likePost.getUserId(), likePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertLikePost(likePost);
}
public int deleteLikePost(Integer userId, Integer postId) {
return forumMapper.deleteLikePost(userId, postId);
}
public LikePost getLikePost(Integer userId, Integer postId) {
return forumMapper.selectLikePost(userId, postId);
}
// 收藏相关
public int createFavoritePost(FavoritePost favoritePost) {
FavoritePost exist = forumMapper.selectFavoritePost(favoritePost.getUserId(), favoritePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertFavoritePost(favoritePost);
}
public int deleteFavoritePost(Integer userId, Integer postId) {
return forumMapper.deleteFavoritePost(userId, postId);
}
public FavoritePost getFavoritePost(Integer userId, Integer postId) {
return forumMapper.selectFavoritePost(userId, postId);
}
// 举报相关
public int createReportPost(ReportPost reportPost) {
// 可以做举报重复校验,暂略
return forumMapper.insertReportPost(reportPost);
}
public List<ReportPost> getReportPostsByPostId(Integer postId) {
return forumMapper.selectReportPostsByPostId(postId);
}
public List<Post> getPostsByUserId(Integer userId) {
return forumMapper.selectPostsByUserId(userId);
}
public int getLikeCountByPostId(Integer postId) {
return forumMapper.selectLikeCountByPostId(postId);
}
/**
* 查询用户点赞过的帖子,并为每个帖子设置点赞数、已点赞和已收藏状态
*/
public List<Post> getLikedPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectLikedPostsByUserId(userId);
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
/**
* 查询用户收藏过的帖子,并为每个帖子设置点赞数、已点赞和已收藏状态
*/
public List<Post> getFavoritedPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectFavoritedPostsByUserId(userId);
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
/**
* 将帖子列表中的每个帖子设置点赞数 和 是否已点赞、已收藏
*/
private void enrichPostsWithLikeFavorite(Integer userId, List<Post> posts) {
for (Post post : posts) {
int likeCount = forumMapper.selectLikeCountByPostId(post.getId());
post.setLikeCount(likeCount);
LikePost likePost = forumMapper.selectLikePost(userId, post.getId());
post.setLiked(likePost != null);
FavoritePost favPost = forumMapper.selectFavoritePost(userId, post.getId());
post.setFavorited(favPost != null);
}
}
}
帮忙改,给我完整代码
package com.example.springboot.service;
import com.example.springboot.entity.Forum.*;
import com.example.springboot.mapper.ForumMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class ForumService {
@Resource
private ForumMapper forumMapper;
// ---------------- 帖子相关 ---------------- //
public int createPost(Post post) {
return forumMapper.insertPost(post);
}
public int deletePost(Integer postId) {
return forumMapper.deletePostById(postId);
}
public int updatePost(Post post) {
return forumMapper.updatePost(post);
}
/**
* 根据帖子ID查询帖子详情,注意likeCount是从 like_post 表动态统计的,需要你的Mapper支持
* 建议对应 Mapper 查询中联表统计点赞数
*/
public Post getPostById(Integer postId) {
return forumMapper.selectPostById(postId);
}
public List<Post> searchPostsByTitle(String title) {
return forumMapper.selectPostsByTitleLike(title);
}
/**
* 查询某用户的帖子列表
* 建议 SQL 联表统计点赞数
*/
public List<Post> getPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectPostsByUserId(userId);
// 补充点赞状态和收藏状态等
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
// ---------------- 评论相关 ---------------- //
public int createComment(Comment comment) {
return forumMapper.insertComment(comment);
}
public int deleteComment(Integer commentId) {
return forumMapper.deleteCommentById(commentId);
}
public int updateComment(Comment comment) {
return forumMapper.updateComment(comment);
}
public List<Comment> getCommentsByPostId(Integer postId) {
return forumMapper.selectCommentsByPostId(postId);
}
// ---------------- 点赞相关 ---------------- //
/**
* 点赞某帖子,重复点赞会返回0表示已点赞
* 只往 like_post 插入记录,不修改 post 表
*/
public int createLikePost(LikePost likePost) {
LikePost exist = forumMapper.selectLikePost(likePost.getUserId(), likePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertLikePost(likePost);
}
/**
* 取消点赞,删除 like_post 记录,不修改 post 表
*/
public int deleteLikePost(Integer userId, Integer postId) {
return forumMapper.deleteLikePost(userId, postId);
}
public LikePost getLikePost(Integer userId, Integer postId) {
return forumMapper.selectLikePost(userId, postId);
}
/**
* 查询某帖子点赞数量,动态查询 like_post 表
*/
public int getLikeCountByPostId(Integer postId) {
return forumMapper.selectLikeCountByPostId(postId);
}
// ---------------- 收藏相关 ---------------- //
public int createFavoritePost(FavoritePost favoritePost) {
FavoritePost exist = forumMapper.selectFavoritePost(favoritePost.getUserId(), favoritePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertFavoritePost(favoritePost);
}
public int deleteFavoritePost(Integer userId, Integer postId) {
return forumMapper.deleteFavoritePost(userId, postId);
}
public FavoritePost getFavoritePost(Integer userId, Integer postId) {
return forumMapper.selectFavoritePost(userId, postId);
}
// ---------------- 举报相关 ---------------- //
public int createReportPost(ReportPost reportPost) {
// 可加重复举报校验,暂略
return forumMapper.insertReportPost(reportPost);
}
public List<ReportPost> getReportPostsByPostId(Integer postId) {
return forumMapper.selectReportPostsByPostId(postId);
}
// ---------------- 用户点赞和收藏的帖子列表 ---------------- //
/**
* 查询用户点赞过的帖子列表
*/
public List<Post> getLikedPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectLikedPostsByUserId(userId);
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
/**
* 查询用户收藏过的帖子列表
*/
public List<Post> getFavoritedPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectFavoritedPostsByUserId(userId);
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
/**
* 给帖子列表补全点赞数,点赞状态,收藏状态
*/
private void enrichPostsWithLikeFavorite(Integer userId, List<Post> posts) {
if (posts == null || posts.isEmpty()) return;
for (Post post : posts) {
int likeCount = this.getLikeCountByPostId(post.getId());
post.setLikeCount(likeCount);
LikePost likePost = forumMapper.selectLikePost(userId, post.getId());
post.setLiked(likePost != null);
FavoritePost favPost = forumMapper.selectFavoritePost(userId, post.getId());
post.setFavorited(favPost != null);
}
}
}
如果你愿意,可以发一下你的 Mapper XML 里相关 SQL,我帮你写联表点赞数统计的SQL语句;或者发实体类定义确认。
需要帮你写具体 Mapper查询语句或其他配套的代码,随时告诉我!
3. 简单快速版本(如果不定义resultMap)
xml
<select id="selectPostById" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
IFNULL(like_counts.count, 0) AS likeCount
FROM post p
LEFT JOIN user u ON p.user_id = u.id
LEFT JOIN (
SELECT post_id, COUNT(*) AS count FROM like_post GROUP BY post_id
) like_counts ON like_counts.post_id = p.id
WHERE p.id = #{id}
</select>
SELECT p.*,
CASE WHEN lp.user_id IS NULL THEN false ELSE true END AS liked
FROM post p
LEFT JOIN like_post lp ON lp.post_id = p.id AND lp.user_id = #{userId}
WHERE ...
@Resource
private ForumMapper forumMapper;
// ---------------- 帖子相关 ---------------- //
public int createPost(Post post) {
return forumMapper.insertPost(post);
}
public int deletePost(Integer postId) {
return forumMapper.deletePostById(postId);
}
public int updatePost(Post post) {
return forumMapper.updatePost(post);
}
/**
* 根据帖子ID查询帖子详情,注意likeCount是从 like_post 表动态统计的,需要你的Mapper支持
* 建议对应 Mapper 查询中联表统计点赞数
*/
public Post getPostById(Integer postId) {
return forumMapper.selectPostById(postId);
}
public List<Post> searchPostsByTitle(String title) {
return forumMapper.selectPostsByTitleLike(title);
}
/**
* 查询某用户的帖子列表
* 建议 SQL 联表统计点赞数
*/
public List<Post> getPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectPostsByUserId(userId);
// 补充点赞状态和收藏状态等
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
// ---------------- 评论相关 ---------------- //
public int createComment(Comment comment) {
return forumMapper.insertComment(comment);
}
public int deleteComment(Integer commentId) {
return forumMapper.deleteCommentById(commentId);
}
public int updateComment(Comment comment) {
return forumMapper.updateComment(comment);
}
public List<Comment> getCommentsByPostId(Integer postId) {
return forumMapper.selectCommentsByPostId(postId);
}
// ---------------- 点赞相关 ---------------- //
/**
* 点赞某帖子,重复点赞会返回0表示已点赞
* 只往 like_post 插入记录,不修改 post 表
*/
public int createLikePost(LikePost likePost) {
LikePost exist = forumMapper.selectLikePost(likePost.getUserId(), likePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertLikePost(likePost);
}
/**
* 取消点赞,删除 like_post 记录,不修改 post 表
*/
public int deleteLikePost(Integer userId, Integer postId) {
return forumMapper.deleteLikePost(userId, postId);
}
public LikePost getLikePost(Integer userId, Integer postId) {
return forumMapper.selectLikePost(userId, postId);
}
/**
* 查询某帖子点赞数量,动态查询 like_post 表
*/
public int getLikeCountByPostId(Integer postId) {
return forumMapper.selectLikeCountByPostId(postId);
}
// ---------------- 收藏相关 ---------------- //
public int createFavoritePost(FavoritePost favoritePost) {
FavoritePost exist = forumMapper.selectFavoritePost(favoritePost.getUserId(), favoritePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertFavoritePost(favoritePost);
}
public int deleteFavoritePost(Integer userId, Integer postId) {
return forumMapper.deleteFavoritePost(userId, postId);
}
public FavoritePost getFavoritePost(Integer userId, Integer postId) {
return forumMapper.selectFavoritePost(userId, postId);
}
// ---------------- 举报相关 ---------------- //
public int createReportPost(ReportPost reportPost) {
// 可加重复举报校验,暂略
return forumMapper.insertReportPost(reportPost);
}
public List<ReportPost> getReportPostsByPostId(Integer postId) {
return forumMapper.selectReportPostsByPostId(postId);
}
// ---------------- 用户点赞和收藏的帖子列表 ---------------- //
/**
* 查询用户点赞过的帖子列表
*/
public List<Post> getLikedPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectLikedPostsByUserId(userId);
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
/**
* 查询用户收藏过的帖子列表
*/
public List<Post> getFavoritedPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectFavoritedPostsByUserId(userId);
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
/**
* 给帖子列表补全点赞数,点赞状态,收藏状态
*/
private void enrichPostsWithLikeFavorite(Integer userId, List<Post> posts) {
if (posts == null || posts.isEmpty()) return;
for (Post post : posts) {
// 如果SQL已经返回了 likeCount,说明此处可省略,或者为了双保险再写:
int likeCount = forumMapper.selectLikeCountByPostId(post.getId());
post.setLikeCount(likeCount);
LikePost likePost = forumMapper.selectLikePost(userId, post.getId());
post.setLiked(likePost != null);
FavoritePost favPost = forumMapper.selectFavoritePost(userId, post.getId());
post.setFavorited(favPost != null);
}
}
}
code
<!-- 插入帖子 -->
<insert id="insertPost" parameterType="com.example.springboot.entity.Forum.Post" useGeneratedKeys="true" keyProperty="id">
INSERT INTO post(user_id,title,content,created_at,updated_at)
VALUES(#{userId}, #{title}, #{content}, NOW(), NOW())
</insert>
<!-- 删除帖子 -->
<delete id="deletePostById" parameterType="int">
DELETE FROM post WHERE id = #{id}
</delete>
<!-- 更新帖子 -->
<update id="updatePost" parameterType="com.example.springboot.entity.Forum.Post">
UPDATE post
SET title = #{title}, content = #{content}, updated_at = NOW()
WHERE id = #{id}
</update>
<!-- 根据id查询帖子(带用户名) -->
<select id="selectPostById" parameterType="int" resultMap="postResultMap">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
IFNULL(like_counts.count, 0) AS likeCount
FROM post p
LEFT JOIN user u ON p.user_id = u.id
LEFT JOIN (
SELECT post_id, COUNT(*) AS count FROM like_post GROUP BY post_id
) like_counts ON like_counts.post_id = p.id
WHERE p.id = #{id}
</select>
<resultMap id="postResultMap" type="com.example.springboot.entity.Forum.Post">
<id property="id" column="id"/>
<result property="userId" column="userId"/>
<result property="username" column="username"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<result property="createdAt" column="createdAt"/>
<result property="updatedAt" column="updatedAt"/>
<result property="likeCount" column="likeCount"/>
<result property="liked" column="liked"/>
<result property="favorited" column="favorited"/>
</resultMap>
<!-- 配置resultMap对应属性映射到Post实体 -->
<!-- 模糊搜索帖子的标题 -->
<select id="selectPostsByTitleLike" parameterType="string" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
LEFT JOIN user u ON p.user_id = u.id
WHERE p.title LIKE CONCAT('%', #{title}, '%')
ORDER BY p.created_at DESC
</select>
<!-- 新增评论 -->
<insert id="insertComment" parameterType="com.example.springboot.entity.Forum.Comment" useGeneratedKeys="true" keyProperty="id">
INSERT INTO comment(post_id, user_id, parent_comment_id, content, created_at, updated_at)
VALUES(#{postId}, #{userId}, #{parentCommentId}, #{content}, NOW(), NOW())
</insert>
<!-- 删除评论 -->
<delete id="deleteCommentById" parameterType="int">
DELETE FROM comment WHERE id = #{id}
</delete>
<!-- 修改评论内容 -->
<update id="updateComment" parameterType="com.example.springboot.entity.Forum.Comment">
UPDATE comment
SET content = #{content}, updated_at = NOW()
WHERE id = #{id}
</update>
<!-- 查询某帖子下所有评论(带用户名) -->
<select id="selectCommentsByPostId" parameterType="int" resultType="com.example.springboot.entity.Forum.Comment">
SELECT
c.id,
c.post_id AS postId,
c.user_id AS userId,
u.username,
c.parent_comment_id AS parentCommentId,
c.content,
c.created_at AS createdAt,
c.updated_at AS updatedAt
FROM comment c
LEFT JOIN user u ON c.user_id = u.id
WHERE c.post_id = #{postId}
ORDER BY c.created_at ASC
</select>
<!-- 点赞 -->
<insert id="insertLikePost" parameterType="com.example.springboot.entity.Forum.LikePost" useGeneratedKeys="true" keyProperty="id">
INSERT INTO like_post(user_id, post_id, created_at) VALUES(#{userId}, #{postId}, NOW())
</insert>
<delete id="deleteLikePost" parameterType="map">
DELETE FROM like_post WHERE user_id = #{userId} AND post_id = #{postId}
</delete>
<select id="selectLikePost" parameterType="map" resultType="com.example.springboot.entity.Forum.LikePost">
SELECT * FROM like_post WHERE user_id = #{userId} AND post_id = #{postId} LIMIT 1
</select>
<!-- 收藏 -->
<insert id="insertFavoritePost" parameterType="com.example.springboot.entity.Forum.FavoritePost" useGeneratedKeys="true" keyProperty="id">
INSERT INTO favorite_post(user_id, post_id, created_at) VALUES(#{userId}, #{postId}, NOW())
</insert>
<delete id="deleteFavoritePost" parameterType="map">
DELETE FROM favorite_post WHERE user_id = #{userId} AND post_id = #{postId}
</delete>
<select id="selectFavoritePost" parameterType="map" resultType="com.example.springboot.entity.Forum.FavoritePost">
SELECT * FROM favorite_post WHERE user_id = #{userId} AND post_id = #{postId} LIMIT 1
</select>
<!-- 举报 -->
<insert id="insertReportPost" parameterType="com.example.springboot.entity.Forum.ReportPost" useGeneratedKeys="true" keyProperty="id">
INSERT INTO report_post(reporter_id, post_id, reason, created_at) VALUES(#{reporterId}, #{postId}, #{reason}, NOW())
</insert>
<select id="selectReportPostsByPostId" parameterType="int" resultType="com.example.springboot.entity.Forum.ReportPost">
SELECT * FROM report_post WHERE post_id = #{postId} ORDER BY created_at DESC
</select>
<!-- 查询某用户的所有帖子 -->
<select id="selectPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
IFNULL(like_counts.count, 0) AS likeCount
FROM post p
LEFT JOIN user u ON p.user_id = u.id
LEFT JOIN (
SELECT post_id, COUNT(*) AS count
FROM like_post
GROUP BY post_id
) like_counts ON like_counts.post_id = p.id
WHERE p.user_id = #{userId}
ORDER BY p.created_at DESC
</select>
<!-- 查询某帖子的点赞数量 -->
<select id="selectLikeCountByPostId" parameterType="int" resultType="int">
SELECT COUNT(*) FROM like_post WHERE post_id = #{postId}
</select>
<!-- 查询用户点赞过的帖子 -->
<select id="selectLikedPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
INNER JOIN like_post lp ON p.id = lp.post_id
LEFT JOIN user u ON p.user_id = u.id
WHERE lp.user_id = #{userId}
ORDER BY lp.created_at DESC
</select>
<!-- 查询用户收藏过的帖子 -->
<select id="selectFavoritedPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
INNER JOIN favorite_post fp ON p.id = fp.post_id
LEFT JOIN user u ON p.user_id = u.id
WHERE fp.user_id = #{userId}
ORDER BY fp.created_at DESC
</select>
<select id="selectLikedPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
INNER JOIN like_post lp ON p.id = lp.post_id
LEFT JOIN user u ON p.user_id = u.id
WHERE lp.user_id = #{userId}
ORDER BY lp.created_at DESC
</select>
<select id="selectLikedPostsByUserId" parameterType="int" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
true AS liked -- 直接返回true表示用户已点赞该帖子
FROM post p
INNER JOIN like_post lp ON p.id = lp.post_id
LEFT JOIN user u ON p.user_id = u.id
WHERE lp.user_id = #{userId}
ORDER BY lp.created_at DESC
</select>