SpringBoot整合Mybatis-plus實(shí)現(xiàn)多級(jí)評(píng)論功能
在本文中,我們將介紹如何使用SpringBoot整合Mybatis-plus實(shí)現(xiàn)多級(jí)評(píng)論功能。同時(shí),本文還將提供數(shù)據(jù)庫(kù)的設(shè)計(jì)和詳細(xì)的后端代碼,前端界面使用Vue2。

數(shù)據(jù)庫(kù)設(shè)計(jì)
本文的多級(jí)評(píng)論功能將采用MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn),下面是數(shù)據(jù)庫(kù)的設(shè)計(jì):
用戶表
用戶表用于存儲(chǔ)注冊(cè)用戶的信息。
| 屬性名 | 數(shù)據(jù)類型 | 描述 |
|---|---|---|
| id | int | 用戶ID |
| username | varchar(20) | 用戶名 |
| password | varchar(20) | 密碼 |
| varchar(30) | 電子郵箱 | |
| avatar | varchar(50) | 頭像 |
評(píng)論表
用于存儲(chǔ)所有的評(píng)論信息。
| 屬性名 | 數(shù)據(jù)類型 | 描述 |
|---|---|---|
| id | int | 評(píng)論ID |
| content | text | 評(píng)論內(nèi)容 |
| create_time | datetime | 評(píng)論創(chuàng)建時(shí)間 |
| parent_id | int | 父級(jí)評(píng)論ID |
| user_id | int | 評(píng)論用戶ID |
后端實(shí)現(xiàn)
相關(guān)依賴
首先,我們需要在pom.xml文件中添加以下依賴:
<!-- SpringBoot依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<!-- Mybatis-plus依賴 -->
<dependency>
<groupId>com.baomidou.mybatisplus</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-version}</version>
</dependency>
<!-- MySQL驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-version}</version>
</dependency>其中,${spring-boot-version}、${mybatis-plus-version}和${mysql-version}需要根據(jù)實(shí)際情況進(jìn)行替換。
配置文件
接下來(lái),我們需要在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:
# 實(shí)體包路徑
typeAliasesPackage: cn.example.entity
# Mybatis XML文件位置
mapperLocations: classpath:mapper/*.xml
# 自動(dòng)填充策略
global-config:
db-config:
id-type: auto
field-strategy: not_empty這里需要將dbname替換成實(shí)際的數(shù)據(jù)庫(kù)名稱。
實(shí)體類
然后,我們需要?jiǎng)?chuàng)建實(shí)體類User和Comment,分別對(duì)應(yīng)用戶和評(píng)論。
@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接口
接著,我們需要?jiǎng)?chuàng)建Mapper接口UserMapper和CommentMapper,用于操作用戶和評(píng)論的數(shù)據(jù)。
public interface UserMapper extends BaseMapper<User> {
}
public interface CommentMapper extends BaseMapper<Comment> {
/**
* 獲取一級(jí)評(píng)論列表(即parent_id為null的評(píng)論)
* @return 一級(jí)評(píng)論列表
*/
List<Comment> listParentComments();
/**
* 獲取二級(jí)評(píng)論列表(即parent_id不為null的評(píng)論)
* @param parentId 父級(jí)評(píng)論ID
* @return 二級(jí)評(píng)論列表
*/
List<Comment> listChildComments(Long parentId);
}BaseMapper是Mybatis-plus提供的通用Mapper接口,在使用時(shí)需要繼承并指定實(shí)體類。
除此之外,我們還添加了兩個(gè)自定義的方法listParentComments和listChildComments,用于分別獲取一級(jí)評(píng)論和二級(jí)評(píng)論的信息。
Service層和Controller層
最后,我們需要?jiǎng)?chuàng)建Service和Controller層,實(shí)現(xiàn)業(yè)務(wù)邏輯和接口。
首先是CommentService:
@Service
public class CommentService {
@Autowired
private CommentMapper commentMapper;
/**
* 獲取一級(jí)評(píng)論列表
* @return 一級(jí)評(píng)論列表
*/
public List<Comment> listParentComments() {
return commentMapper.listParentComments();
}
/**
* 獲取二級(jí)評(píng)論列表
* @param parentId 父級(jí)評(píng)論ID
* @return 二級(jí)評(píng)論列表
*/
public List<Comment> listChildComments(Long parentId) {
return commentMapper.listChildComments(parentId);
}
/**
* 添加評(píng)論
* @param comment 評(píng)論信息
*/
public void addComment(Comment comment) {
commentMapper.insert(comment);
}
}然后是CommentController:
@RestController
@RequestMapping("/comment")
public class CommentController {
@Autowired
private CommentService commentService;
/**
* 獲取一級(jí)評(píng)論列表
* @return 一級(jí)評(píng)論列表
*/
@GetMapping("/parent")
public ResultVo listParentComments() {
List<Comment> comments = commentService.listParentComments();
return ResultUtil.success(comments);
}
/**
* 獲取二級(jí)評(píng)論列表
* @param parentId 父級(jí)評(píng)論ID
* @return 二級(jí)評(píng)論列表
*/
@GetMapping("/child")
public ResultVo listChildComments(@RequestParam Long parentId) {
List<Comment> comments = commentService.listChildComments(parentId);
return ResultUtil.success(comments);
}
/**
* 添加評(píng)論
* @param comment 評(píng)論信息
*/
@PostMapping("/add")
public ResultVo addComment(@RequestBody Comment comment) {
comment.setCreateTime(new Date());
commentService.addComment(comment);
return ResultUtil.success();
}
}這里的ResultVo和ResultUtil是用于封裝返回結(jié)果的工具類,這里不做過(guò)多解釋。
前端實(shí)現(xiàn)
前端界面使用Vue實(shí)現(xiàn)。具體實(shí)現(xiàn)過(guò)程這里不做過(guò)多解釋,在此提供代碼供參考:
<template>
<div class="comment-box">
<h2>評(píng)論區(qū)域</h2>
<h3>發(fā)表評(píng)論</h3>
<form @submit.prevent="addComment">
<div class="form-item">
<label>評(píng)論內(nèi)容:</label>
<textarea v-model="comment.content" required></textarea>
</div>
<button type="submit">提交</button>
</form>
<h3>一級(jí)評(píng)論</h3>
<ul>
<li v-for="comment in parentComments" :key="comment.id">
<p>{{comment.content}}</p>
<button @click="showChildComments(comment.id)">查看回復(fù)</button>
<div v-show="showChildCommentId === comment.id">
<h4>二級(jí)評(píng)論</h4>
<ul>
<li v-for="comment in childComments" :key="comment.id">
<p>{{comment.content}}</p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
comment: {
content: '',
},
parentComments: [],
childComments: [],
showChildCommentId: null,
};
},
methods: {
// 獲取一級(jí)評(píng)論列表
getParentComments() {
axios.get('/comment/parent').then(res => {
this.parentComments = res.data.data;
}).catch(err => {
console.log(err);
});
},
// 獲取二級(jí)評(píng)論列表
getChildComments(parentId) {
axios.get('/comment/child', {params: {parentId}}).then(res => {
this.childComments = res.data.data;
}).catch(err => {
console.log(err);
});
},
// 添加評(píng)論
addComment() {
axios.post('/comment/add', this.comment).then(res => {
this.comment.content = '';
this.getParentComments();
}).catch(err => {
console.log(err);
});
},
// 顯示二級(jí)評(píng)論
showChildComments(parentId) {
if(this.showChildCommentId === parentId) {
this.showChildCommentId = null;
this.childComments = [];
}else {
this.showChildCommentId = parentId;
this.getChildComments(parentId);
}
}
},
};
</script>
<style>
.comment-box {
font-family: Arial, sans-serif;
max-width: 800px;
margin: auto;
}
.form-item {
margin-top: 10px;
}
.form-item label {
display: inline-block;
width: 80px;
font-weight: bold;
}
.form-item textarea {
width: 100%;
height: 100px;
border: 1px solid #ccc;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
margin-top: 10px;
}
li p {
margin: 0;
padding: 5px;
border: 1px solid #ccc;
border-radius: 3px;
}
</style>總結(jié)
本文介紹了如何使用SpringBoot整合Mybatis-plus實(shí)現(xiàn)多級(jí)評(píng)論功能,同時(shí)提供了數(shù)據(jù)庫(kù)的設(shè)計(jì)和詳細(xì)的后端代碼,前端界面使用的Vue2,希望本文能夠?qū)δ兴鶐椭?/p>
到此這篇關(guān)于SpringBoot整合Mybatis-plus實(shí)現(xiàn)多級(jí)評(píng)論的文章就介紹到這了,更多相關(guān)SpringBoot多級(jí)評(píng)論內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot3整合mybatis-plus的實(shí)現(xiàn)
- Springboot3整合Mybatis-plus3.5.3報(bào)錯(cuò)問(wèn)題解決
- SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁(yè)查詢功能
- springboot3.2整合mybatis-plus詳細(xì)代碼示例
- SpringBoot3和mybatis-plus整合出現(xiàn)的問(wèn)題解決辦法
- SpringBoot3.2.2整合MyBatis-Plus3.5.5依賴不兼容的問(wèn)題解決
- SpringBoot整合Mybatis-Plus實(shí)現(xiàn)關(guān)聯(lián)查詢
- 全網(wǎng)最新springboot整合mybatis-plus的過(guò)程
- SpringBoot3.3.X整合Mybatis-Plus的實(shí)現(xiàn)示例
相關(guān)文章
springboot返回modelandview頁(yè)面的實(shí)例
這篇文章主要介紹了springboot返回modelandview頁(yè)面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
SpringBoot項(xiàng)目配置數(shù)據(jù)庫(kù)密碼加密相關(guān)代碼
這篇文章主要介紹了SpringBoot項(xiàng)目配置數(shù)據(jù)庫(kù)密碼加密的相關(guān)資料,本文介紹了在Springboot項(xiàng)目中配置數(shù)據(jù)庫(kù)連接時(shí)存在的安全問(wèn)題,即用戶名和密碼以明文形式存儲(chǔ),容易泄露,提出了一種簡(jiǎn)單的加密方案,需要的朋友可以參考下2024-11-11
spring-boot報(bào)錯(cuò)javax.servlet.http不存在的問(wèn)題解決
當(dāng)springboot項(xiàng)目從2.7.x的升級(jí)到3.0.x的時(shí)候,會(huì)遇到j(luò)avax.servlet.http不存在,本文就來(lái)介紹一下這個(gè)問(wèn)題的解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
SpringMVC中的DispatcherServlet詳細(xì)解析
這篇文章主要介紹了SpringMVC中的DispatcherServlet詳細(xì)解析,DispatcherServlet也是一個(gè)Servlet,它也能通過(guò)Servlet的API來(lái)響應(yīng)請(qǐng)求,從而成為一個(gè)前端控制器,Web容器會(huì)調(diào)用Servlet的doGet()以及doPost()等方法,需要的朋友可以參考下2023-12-12

