refactor(消息模块): 重构消息服务及控制器功能

重构消息模块,包括以下主要变更:
1. 将MessageRepository从CrudRepository扩展改为JpaRepository
2. 新增消息查询方法,支持按文章ID、父消息ID和昵称查询
3. 完善消息服务层逻辑,增加日志记录和错误处理
4. 扩展消息控制器API,新增获取根消息、回复消息等端点
5. 添加消息数据初始化组件和检查器
6. 优化全局异常处理,增加请求路径日志

同时调整文章模块:
1. 移除按作者查询文章功能
2. 统一分类查询参数命名
3. 优化文章服务层代码结构

配置变更:
1. 添加缓存相关依赖
2. 调整数据库连接配置
3. 暂时禁用Hibernate二级缓存
This commit is contained in:
qingfeng1121
2025-10-11 11:17:12 +08:00
parent 60f4752124
commit 470cf71713
15 changed files with 5025 additions and 6756 deletions

View File

@@ -4,7 +4,6 @@ import com.qf.myafterprojecy.pojo.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@@ -12,8 +11,6 @@ import java.util.List;
import java.util.Optional;
@Repository // 表明这是一个数据访问层组件,用于持久层操作
//public interface ArticleRepository extends CrudRepository<Article,Integer> {
//}
public interface ArticleRepository extends JpaRepository<Article, Integer> {
/**
* 根据文章ID查询文章信息的方法
@@ -28,7 +25,6 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
* 根据文章ID查询已发布的文章
* 使用JPQL查询语句只查询状态为1已发布且指定ID的文章
*
* @param id 作者ID作为查询参数传入
* @return 返回符合查询条件的文章列表
*/
@Query("SELECT a FROM Article a WHERE a.status = 1")
@@ -41,8 +37,8 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
* @param typeid 分类ID通过@Param注解映射到查询语句中的:typeid参数
* @return 返回符合条件Article对象的列表
*/
@Query("SELECT a FROM Article a WHERE a.status = 1 AND a.typeid = :categoryId")
List<Article> findPublishedByCategory(@Param("categoryId") Integer categoryId);
@Query("SELECT a FROM Article a WHERE a.status = 1 AND a.typeid = :typeid")
List<Article> findPublishedByCategory(@Param("typeid") Integer typeid);
/**
* 使用@Modifying注解标记这是一个修改操作通常用于UPDATE或DELETE语句

View File

@@ -1,8 +1,32 @@
package com.qf.myafterprojecy.repository;
import com.qf.myafterprojecy.pojo.Message;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
public interface MessageRepository extends CrudRepository<Message, Integer> {
import java.util.List;
@Repository
public interface MessageRepository extends JpaRepository<Message, Integer> {
// 根据文章ID查询消息
List<Message> findByArticleid(Integer articleid);
// 查询所有父消息(回复的根消息)
List<Message> findByParentidIsNull();
// 根据父消息ID查询回复
List<Message> findByParentid(Integer parentid);
// 根据昵称模糊查询消息
List<Message> findByNicknameContaining(String nickname);
// 查询指定文章下的所有父消息(根回复)
@Query("SELECT m FROM Message m WHERE m.articleid = ?1 AND m.parentid IS NULL ORDER BY m.createdAt DESC")
List<Message> findRootMessagesByArticleId(Integer articleId);
// 统计指定文章的评论数量
@Query("SELECT COUNT(m) FROM Message m WHERE m.articleid = ?1")
Long countByArticleId(Integer articleId);
}