feat:增加分页查询朋友圈功能

This commit is contained in:
2025-07-24 14:02:05 +08:00
parent 9dd7b4e146
commit e47a7a8510
6 changed files with 93 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
package com.cxx.food.controller; package com.cxx.food.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxx.food.dto.*; import com.cxx.food.dto.*;
import com.cxx.food.service.MomentService; import com.cxx.food.service.MomentService;
import com.fasterxml.jackson.annotation.JsonView; import com.fasterxml.jackson.annotation.JsonView;
@@ -43,6 +44,13 @@ public class MomentController {
return momentService.queryMomentList(); return momentService.queryMomentList();
} }
@Operation(summary = "分页查询所有朋友圈")
@GetMapping("/page")
public IPage<MomentDto> queryMomentByPage(@RequestParam("currentPage") Integer currentPage,
@RequestParam("pageSize") Integer pageSize) {
return momentService.queryMomentByPage(currentPage, pageSize);
}
@Operation(summary = "查询朋友圈") @Operation(summary = "查询朋友圈")
@GetMapping("/{id}") @GetMapping("/{id}")
public @JsonView(ReadView.class) List<MomentDto> queryMomentById(@PathVariable("id") long id) { public @JsonView(ReadView.class) List<MomentDto> queryMomentById(@PathVariable("id") long id) {

View File

@@ -1,5 +1,7 @@
package com.cxx.food.dao; package com.cxx.food.dao;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxx.food.dto.*; import com.cxx.food.dto.*;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@@ -10,6 +12,8 @@ import java.util.List;
public interface MomentDao { public interface MomentDao {
List<MomentDto> queryMomentList(); List<MomentDto> queryMomentList();
IPage<MomentDto> queryMomentByPage(Page<MomentDto> page);
List<MomentDto> queryMomentById(@Param("id") Long id); List<MomentDto> queryMomentById(@Param("id") Long id);
void insertMomentImage(@Param("momentId") Long momentId, void insertMomentImage(@Param("momentId") Long momentId,

View File

@@ -0,0 +1,41 @@
package com.cxx.food.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
import java.util.List;
@Getter
@Setter
public class MomentPageDto {
@Schema(description = "id")
private Long id;
@Schema(description = "用户id")
private Long userId;
@Schema(description = "姓名")
private String username;
@Schema(description = "头像")
private String avatar;
@Schema(description = "朋友圈内容")
private String content;
@Schema(description = "朋友圈图片")
private List<String> imageList;
@Schema(description = "时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date date;
@Schema(description = "点赞用户")
private List<Long> likeList;
@Schema(description = "评论内容")
private List<CommentDto> commentList;
}

View File

@@ -1,6 +1,7 @@
package com.cxx.food.service; package com.cxx.food.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxx.food.dto.*; import com.cxx.food.dto.*;
import java.util.List; import java.util.List;
@@ -14,6 +15,8 @@ public interface MomentService {
List<MomentDto> queryMomentList(); List<MomentDto> queryMomentList();
IPage<MomentDto> queryMomentByPage(int currentPage, int pageSize);
List<MomentDto> queryMomentById(Long id); List<MomentDto> queryMomentById(Long id);
Boolean addComment(Long momentId, String content); Boolean addComment(Long momentId, String content);

View File

@@ -1,6 +1,8 @@
package com.cxx.food.service.impl; package com.cxx.food.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxx.food.constant.BizType; import com.cxx.food.constant.BizType;
import com.cxx.food.dao.MomentDao; import com.cxx.food.dao.MomentDao;
import com.cxx.food.dto.MomentDto; import com.cxx.food.dto.MomentDto;
@@ -74,6 +76,12 @@ public class MomentServiceImpl implements MomentService {
return momentDao.queryMomentList(); return momentDao.queryMomentList();
} }
@Override
public IPage<MomentDto> queryMomentByPage(int currentPage, int pageSize) {
Page<MomentDto> page = new Page<>(currentPage, pageSize);
return momentDao.queryMomentByPage(page);
}
@Override @Override
public List<MomentDto> queryMomentById(Long id) { public List<MomentDto> queryMomentById(Long id) {
return momentDao.queryMomentById(id); return momentDao.queryMomentById(id);

View File

@@ -18,6 +18,23 @@
</collection> </collection>
</resultMap> </resultMap>
<resultMap id="momentPageResultMap" type="com.cxx.food.dto.MomentPageDto">
<id property="id" column="id"/>
<result property="userId" column="userId"/>
<result property="username" column="username"/>
<result property="avatar" column="avatar"/>
<result property="content" column="content"/>
<result property="date" column="date"/>
<collection property="imageList" ofType="java.lang.String"
column="id" select="queryMomentImage"/>
<collection property="likeList" ofType="long"
column="id" select="queryMomentLike">
</collection>
<collection property="commentList" ofType="com.cxx.food.dto.CommentDto"
column="id" select="queryMomentComment">
</collection>
</resultMap>
<resultMap id="commentResultMap" type="com.cxx.food.dto.CommentDto"> <resultMap id="commentResultMap" type="com.cxx.food.dto.CommentDto">
<id property="id" column="id"/> <id property="id" column="id"/>
<result property="username" column="username"/> <result property="username" column="username"/>
@@ -56,6 +73,18 @@
ORDER BY a.create_time DESC ORDER BY a.create_time DESC
</select> </select>
<select id="queryMomentByPage" resultMap="momentPageResultMap">
SELECT a.id AS id,
b.id AS userId,
b.username AS username,
COALESCE(b.avatar, '') AS avatar,
a.content AS content,
a.create_time AS date
FROM moment AS a
LEFT JOIN user AS b ON a.create_by = b.id
ORDER BY a.create_time DESC
</select>
<select id="queryMomentById" resultMap="momentResultMap"> <select id="queryMomentById" resultMap="momentResultMap">
SELECT a.id AS id, SELECT a.id AS id,
b.id AS userId, b.id AS userId,