SpringBoot怎么整合Mybatis-plus实现多级评论功能
数据库设计
用户表
用户表用于存储注册用户的信息。
| 属性名 | 数据类型 | 描述 |
|---|---|---|
| id | int | 用户ID |
| username | varchar(20) | 用户名 |
| password | varchar(20) | 密码 |
| varchar(30) | 电子邮箱 | |
| avatar | varchar(50) | 头像 |
评论表
用于存储所有的评论信息。
| 属性名 | 数据类型 | 描述 |
|---|---|---|
| id | int | 评论ID |
| content | text | 评论内容 |
| create_time | datetime | 评论创建时间 |
| parent_id | int | 父级评论ID |
| user_id | int | 评论用户ID |
后端实现
相关依赖
首先,我们需要在pom.xml文件中添加以下依赖:
org.springframework.boot spring-boot-starter-web ${spring-boot-version} com.baomidou.mybatisplus mybatis-plus-boot-starter ${mybatis-plus-version} mysql mysql-connector-java ${mysql-version}
其中,${spring-boot-version}、${mybatis-plus-version}和${mysql-version}需要根据实际情况进行替换。
配置文件
接下来,我们需要在application.yml文件中配置MySQL的信息:
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: root # Mybatis-plus配置 mybatis-plus: # 实体包路径 typeAliasesPackage: cn.example.entity # Mybatis XML文件位置 mapperLocations: classpath:mapper/*.xml # 自动填充策略 global-config: db-config: id-type: auto field-strategy: not_empty
这里需要将dbname替换成实际的数据库名称。
实体类
然后,我们需要创建实体类User和Comment,分别对应用户和评论。
@Data
public class User {
private Long id;
private String username;
private String password;
private String email;
private String avatar;
}
@Data
public class Comment {
private Long id;
private String content;
private Date createTime;
private Long parentId;
private Long userId;
}Mapper接口
接着,我们需要创建Mapper接口UserMapper和CommentMapper,用于操作用户和评论的数据。
public interface UserMapper extends BaseMapper{ } public interface CommentMapper extends BaseMapper { /** * 获取一级评论列表(即parent_id为null的评论) * @return 一级评论列表 */ List listParentComments(); /** * 获取二级评论列表(即parent_id不为null的评论) * @param parentId 父级评论ID * @return 二级评论列表 */ List listChildComments(Long parentId); }
BaseMapper是Mybatis-plus提供的通用Mapper接口,在使用时需要继承并指定实体类。
除此之外,我们还添加了两个自定义的方法listParentComments和listChildComments,用于分别获取一级评论和二级评论的信息。
Service层和Controller层
最后,我们需要创建Service和Controller层,实现业务逻辑和接口。
首先是CommentService:
@Service
public class CommentService {
@Autowired
private CommentMapper commentMapper;
/**
* 获取一级评论列表
* @return 一级评论列表
*/
public List listParentComments() {
return commentMapper.listParentComments();
}
/**
* 获取二级评论列表
* @param parentId 父级评论ID
* @return 二级评论列表
*/
public List listChildComments(Long parentId) {
return commentMapper.listChildComments(parentId);
}
/**
* 添加评论
* @param comment 评论信息
*/
public void addComment(Comment comment) {
commentMapper.insert(comment);
}
} 然后是CommentController:
@RestController
@RequestMapping("/comment")
public class CommentController {
@Autowired
private CommentService commentService;
/**
* 获取一级评论列表
* @return 一级评论列表
*/
@GetMapping("/parent")
public ResultVo listParentComments() {
List comments = commentService.listParentComments();
return ResultUtil.success(comments);
}
/**
* 获取二级评论列表
* @param parentId 父级评论ID
* @return 二级评论列表
*/
@GetMapping("/child")
public ResultVo listChildComments(@RequestParam Long parentId) {
List comments = commentService.listChildComments(parentId);
return ResultUtil.success(comments);
}
/**
* 添加评论
* @param comment 评论信息
*/
@PostMapping("/add")
public ResultVo addComment(@RequestBody Comment comment) {
comment.setCreateTime(new Date());
commentService.addComment(comment);
return ResultUtil.success();
}
} 这里的ResultVo和ResultUtil是用于封装返回结果的工具类,这里不做过多解释。
前端实现
前端界面使用Vue实现。具体实现过程这里不做过多解释,在此提供代码供参考:
评论区域
发表评论
一级评论
{{comment.content}}
二级评论
{{comment.content}}