分页 将mybatis-plus依赖移到子模块

This commit is contained in:
苏元皓
2024-08-13 14:02:11 +08:00
parent e2a408488f
commit b755ecfeef
14 changed files with 815 additions and 68 deletions

View File

@@ -1,5 +1,6 @@
package com.ivmiku.tutorial.service;
import com.github.pagehelper.PageInfo;
import com.ivmiku.tutorial.entity.Comment;
import com.baomidou.mybatisplus.extension.service.IService;
@@ -49,10 +50,9 @@ public interface CommentService extends IService<Comment> {
void deleteComment(Long commentId, String userId);
List<Comment> getPostComments(Long postId);
List<Comment> getCommentReplies(Long commentId);
PageInfo<Comment> getPostComments(Long postId, int pageNum, int pageSize);
PageInfo<Comment> getCommentReplies(Long commentId, int pageNum, int pageSize);
/**
* 回复评论。

View File

@@ -1,5 +1,6 @@
package com.ivmiku.tutorial.service;
import com.github.pagehelper.PageInfo;
import com.ivmiku.tutorial.entity.Post;
import com.baomidou.mybatisplus.extension.service.IService;
@@ -11,11 +12,13 @@ public interface PostService extends IService<Post> {
void updatePost(Long postId, String userId, Post post);
void deletePost(Long postId, String userId);
List<Post> getPostList(String userId);
PageInfo<Post> getPostList(String userId, int pageNum, int pageSize);
List<Post> getCommunityPostList(Long communityId);
PageInfo<Post> getCommunityPostList(Long communityId, int pageNum, int pageSize);
List<Post> getOfficialPosts(); // 新增获取官方帖子的方法
PageInfo<Post> getOfficialPosts(int pageNum, int pageSize);
List<Post> getNonOfficialPosts(); // 新增获取非官方帖子的方法
PageInfo<Post> getNonOfficialPosts(int pageNum, int pageSize);
void changePublic(Long postId, Integer isPublic);
}

View File

@@ -2,6 +2,8 @@ package com.ivmiku.tutorial.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ivmiku.tutorial.entity.Comment;
import com.ivmiku.tutorial.mapper.CommentMapper;
import com.ivmiku.tutorial.service.CommentService;
@@ -11,7 +13,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
@@ -102,19 +103,28 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> impl
* @return List<Comment> 返回评论列表
*/
@Override
public List<Comment> getPostComments(Long postId) {
logger.info("开始获取帖子ID{}的评论列表", postId);
public PageInfo<Comment> getPostComments(Long postId, int pageNum, int pageSize) {
logger.info("开始获取帖子ID{}的评论列表,第{}页,每页{}条", postId, pageNum, pageSize);
// 设置分页参数
PageHelper.startPage(pageNum, pageSize);
// 查询评论
LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Comment::getPostId, postId).eq(Comment::getIsDeleted, 0);
List<Comment> comments = commentMapper.selectList(wrapper);
if (comments != null) {
logger.info("获取帖子ID{}的评论列表成功", postId);
} else {
logger.warn("帖子ID{}的评论列表为空", postId);
}
return comments;
// 使用 PageInfo 包装结果
return new PageInfo<>(comments);
}
/**
* 获取评论下的所有回复。
*
@@ -122,17 +132,25 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> impl
* @return List<Comment> 返回回复列表
*/
@Override
public List<Comment> getCommentReplies(Long commentId) {
logger.info("开始获取评论ID{}的回复列表", commentId);
public PageInfo<Comment> getCommentReplies(Long commentId, int pageNum, int pageSize) {
logger.info("开始获取评论ID{}的回复列表,第{}页,每页{}条", commentId, pageNum, pageSize);
// 设置分页参数
PageHelper.startPage(pageNum, pageSize);
// 查询回复
LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Comment::getParentCommentId, commentId).eq(Comment::getIsDeleted, 0);
List<Comment> replies = commentMapper.selectList(wrapper);
if (replies != null) {
logger.info("获取评论ID{}的回复列表成功", commentId);
} else {
logger.warn("评论ID{}的回复列表为空", commentId);
}
return replies;
// 使用 PageInfo 包装结果
return new PageInfo<>(replies);
}
/**
* 回复评论。

View File

@@ -1,6 +1,8 @@
package com.ivmiku.tutorial.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ivmiku.tutorial.entity.Post;
import com.ivmiku.tutorial.mapper.PostMapper;
import com.ivmiku.tutorial.service.PostService;
@@ -10,7 +12,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
@@ -35,7 +36,14 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements Po
@Override
public Post getPostById(Long postId) {
logger.info("开始根据ID获取帖子详情帖子ID{}", postId);
return getById(postId);
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Post::getIsPublic, 1);
Post post = postMapper.selectById(wrapper);
if (post == null) {
logger.warn("帖子ID{}不存在", postId);
return null;
}
return post;
}
@Override
@@ -62,38 +70,91 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements Po
}
@Override
public List<Post> getPostList(String userId) {
logger.info("用户ID{}开始获取帖子列表", userId);
public PageInfo<Post> getPostList(String userId, int pageNum, int pageSize) {
logger.info("用户ID{}开始获取帖子列表,第{}页,每页{}条", userId, pageNum, pageSize);
// 设置分页参数
PageHelper.startPage(pageNum, pageSize);
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Post::getUserOpenid, userId);
wrapper.eq(Post::getIsDeleted, 0);
wrapper.eq(Post::getIsPublic, 1);
List<Post> posts = postMapper.selectList(wrapper);
return posts;
// 使用PageInfo封装分页结果
PageInfo<Post> pageInfo = new PageInfo<>(posts);
return pageInfo;
}
@Override
public List<Post> getCommunityPostList(Long communityId) {
logger.info("开始获取社区ID{}的帖子列表", communityId);
public PageInfo<Post> getCommunityPostList(Long communityId, int pageNum, int pageSize) {
logger.info("开始获取社区ID{}的帖子列表,第{}页,每页{}条", communityId, pageNum, pageSize);
// 设置分页参数
PageHelper.startPage(pageNum, pageSize);
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Post::getCommunityId, communityId);
wrapper.eq(Post::getIsDeleted, 0);
wrapper.eq(Post::getIsPublic, 1);
List<Post> posts = postMapper.selectList(wrapper);
return posts;
// 使用PageInfo封装分页结果
PageInfo<Post> pageInfo = new PageInfo<>(posts);
return pageInfo;
}
@Override
public List<Post> getOfficialPosts() {
logger.info("开始获取所有官方创建的帖子");
public PageInfo<Post> getOfficialPosts(int pageNum, int pageSize) {
logger.info("开始获取所有官方创建的帖子,第{}页,每页{}条", pageNum, pageSize);
// 设置分页参数
PageHelper.startPage(pageNum, pageSize);
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Post::getIsOfficial, 1);
wrapper.eq(Post::getIsDeleted, 0);
wrapper.eq(Post::getIsPublic, 1);
List<Post> posts = postMapper.selectList(wrapper);
return posts;
// 使用PageInfo封装分页结果
PageInfo<Post> pageInfo = new PageInfo<>(posts);
return pageInfo;
}
@Override
public List<Post> getNonOfficialPosts() {
logger.info("开始获取所有非官方创建的帖子");
public PageInfo<Post> getNonOfficialPosts(int pageNum, int pageSize) {
logger.info("开始获取所有非官方创建的帖子,第{}页,每页{}条", pageNum, pageSize);
// 设置分页参数
PageHelper.startPage(pageNum, pageSize);
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Post::getIsOfficial, 0);
wrapper.eq(Post::getIsDeleted, 0);
wrapper.eq(Post::getIsPublic, 1);
List<Post> posts = postMapper.selectList(wrapper);
return posts;
// 使用PageInfo封装分页结果
PageInfo<Post> pageInfo = new PageInfo<>(posts);
return pageInfo;
}
@Override
public void changePublic(Long postId, Integer isPublic) {
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Post::getPostId, postId);
Post post = postMapper.selectById(postId);
post.setIsPublic(isPublic);
postMapper.update(post, wrapper);
}
}