欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合Mybatis-plus實現(xiàn)多級評論功能

 更新時間:2023年05月04日 10:11:21   作者:慕言要努力  
本文介紹了如何使用SpringBoot整合Mybatis-plus實現(xiàn)多級評論功能,同時提供了數(shù)據(jù)庫的設計和詳細的后端代碼,前端界面使用的Vue2,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

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

在這里插入圖片描述

數(shù)據(jù)庫設計

本文的多級評論功能將采用MySQL數(shù)據(jù)庫實現(xiàn),下面是數(shù)據(jù)庫的設計:

用戶表

用戶表用于存儲注冊用戶的信息。

屬性名數(shù)據(jù)類型描述
idint用戶ID
usernamevarchar(20)用戶名
passwordvarchar(20)密碼
emailvarchar(30)電子郵箱
avatarvarchar(50)頭像

評論表

用于存儲所有的評論信息。

屬性名數(shù)據(jù)類型描述
idint評論ID
contenttext評論內容
create_timedatetime評論創(chuàng)建時間
parent_idint父級評論ID
user_idint評論用戶ID

后端實現(xià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驅動 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql-version}</version>
</dependency>

其中,${spring-boot-version}、${mybatis-plus-version}${mysql-version}需要根據(jù)實際情況進行替換。

配置文件

接下來,我們需要在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替換成實際的數(shù)據(jù)庫名稱。

實體類

然后,我們需要創(chuàng)建實體類UserComment,分別對應用戶和評論。

@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接口

接著,我們需要創(chuàng)建Mapper接口UserMapperCommentMapper,用于操作用戶和評論的數(shù)據(jù)。

public interface UserMapper extends BaseMapper<User> {
}
public interface CommentMapper extends BaseMapper<Comment> {
    /**
     * 獲取一級評論列表(即parent_id為null的評論)
     * @return 一級評論列表
     */
    List<Comment> listParentComments();
    /**
     * 獲取二級評論列表(即parent_id不為null的評論)
     * @param parentId 父級評論ID
     * @return 二級評論列表
     */
    List<Comment> listChildComments(Long parentId);
}

BaseMapper是Mybatis-plus提供的通用Mapper接口,在使用時需要繼承并指定實體類。

除此之外,我們還添加了兩個自定義的方法listParentCommentslistChildComments,用于分別獲取一級評論和二級評論的信息。

Service層和Controller層

最后,我們需要創(chuàng)建Service和Controller層,實現(xiàn)業(yè)務邏輯和接口。

首先是CommentService:

@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;
    /**
     * 獲取一級評論列表
     * @return 一級評論列表
     */
    public List<Comment> listParentComments() {
        return commentMapper.listParentComments();
    }
    /**
     * 獲取二級評論列表
     * @param parentId 父級評論ID
     * @return 二級評論列表
     */
    public List<Comment> 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<Comment> comments = commentService.listParentComments();
        return ResultUtil.success(comments);
    }
    /**
     * 獲取二級評論列表
     * @param parentId 父級評論ID
     * @return 二級評論列表
     */
    @GetMapping("/child")
    public ResultVo listChildComments(@RequestParam Long parentId) {
        List<Comment> 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();
    }
}

這里的ResultVoResultUtil是用于封裝返回結果的工具類,這里不做過多解釋。

前端實現(xiàn)

前端界面使用Vue實現(xiàn)。具體實現(xiàn)過程這里不做過多解釋,在此提供代碼供參考:

<template>
  <div class="comment-box">
    <h2>評論區(qū)域</h2>
    <h3>發(fā)表評論</h3>
    <form @submit.prevent="addComment">
      <div class="form-item">
        <label>評論內容:</label>
        <textarea v-model="comment.content" required></textarea>
      </div>
      <button type="submit">提交</button>
    </form>
    <h3>一級評論</h3>
    <ul>
      <li v-for="comment in parentComments" :key="comment.id">
        <p>{{comment.content}}</p>
        <button @click="showChildComments(comment.id)">查看回復</button>
        <div v-show="showChildCommentId === comment.id">
          <h4>二級評論</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: {
    // 獲取一級評論列表
    getParentComments() {
      axios.get('/comment/parent').then(res => {
        this.parentComments = res.data.data;
      }).catch(err => {
        console.log(err);
      });
    },
    // 獲取二級評論列表
    getChildComments(parentId) {
      axios.get('/comment/child', {params: {parentId}}).then(res => {
        this.childComments = res.data.data;
      }).catch(err => {
        console.log(err);
      });
    },
    // 添加評論
    addComment() {
      axios.post('/comment/add', this.comment).then(res => {
        this.comment.content = '';
        this.getParentComments();
      }).catch(err => {
        console.log(err);
      });
    },
    // 顯示二級評論
    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>

總結

本文介紹了如何使用SpringBoot整合Mybatis-plus實現(xiàn)多級評論功能,同時提供了數(shù)據(jù)庫的設計和詳細的后端代碼,前端界面使用的Vue2,希望本文能夠對您有所幫助。

到此這篇關于SpringBoot整合Mybatis-plus實現(xiàn)多級評論的文章就介紹到這了,更多相關SpringBoot多級評論內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java8 將一個List<T>轉為Map<String,T>的操作

    Java8 將一個List<T>轉為Map<String,T>的操作

    這篇文章主要介紹了Java8 將一個List<T>轉為Map<String, T>的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 使用spring mail發(fā)送html郵件的示例代碼

    使用spring mail發(fā)送html郵件的示例代碼

    本篇文章主要介紹了使用spring mail發(fā)送html郵件的示例代碼,這里整理了詳細的示例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-09-09
  • javaWeb使用驗證碼實現(xiàn)簡單登錄

    javaWeb使用驗證碼實現(xiàn)簡單登錄

    這篇文章主要為大家詳細介紹了javaWeb使用驗證碼實現(xiàn)簡單登錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • java實現(xiàn)OpenGL ES紋理映射的方法

    java實現(xiàn)OpenGL ES紋理映射的方法

    這篇文章主要介紹了java實現(xiàn)OpenGL ES紋理映射的方法,以實例形式較為詳細的分析了紋理映射的實現(xiàn)技巧,需要的朋友可以參考下
    2015-06-06
  • SpringBoot3集成Kafka的方法詳解

    SpringBoot3集成Kafka的方法詳解

    Kafka是一個開源的分布式事件流平臺,常被用于高性能數(shù)據(jù)管道、流分析、數(shù)據(jù)集成和關鍵任務應用,下面我們就來看看SpringBoot3是如何集成Kafka的吧
    2023-08-08
  • 輕松掌握Java適配器模式

    輕松掌握Java適配器模式

    這篇文章主要幫助大家輕松掌握Java適配器模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • HashMap源碼中的位運算符&詳解

    HashMap源碼中的位運算符&詳解

    這篇文章主要介紹了HashMap源碼中的位運算符&詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • BMIDE環(huán)境導入項目報編碼錯誤解決方案

    BMIDE環(huán)境導入項目報編碼錯誤解決方案

    這篇文章主要介紹了BMIDE環(huán)境導入項目報編碼錯誤解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • JAVA正則表達式校驗qq號碼的方法

    JAVA正則表達式校驗qq號碼的方法

    Java作為一種開發(fā)語言,有許多值得推薦的地方,但是它一直以來沒有自帶對正則表達式的支持。下面小編給大家?guī)砹薐AVA正則表達式校驗qq號碼的方法,需要的朋友參考下吧
    2018-04-04
  • Java中RocketMQ使用方法詳解

    Java中RocketMQ使用方法詳解

    這篇文章主要介紹了RocketMQ和Kafka在SpringBoot中的使用方法,以及如何保證消息隊列的順序性、可靠性以及冪等性,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-02-02

最新評論