项目7-音乐播放器6+评论区

news/2024/9/24 4:17:19/

1.准备前端界面

前端小白:怎么为你的网页增加评论功能?(一)_为网页添加评论区怎么弄-CSDN博客

参考的上述文章的前端代码

我们从上述前端图片知道,我们数据库需要准备的字段:

id,commentuserName,coomentmusicId,comment,time

2.数据库的准备

DROP TABLE IF EXISTS `commentmusic`;
CREATE TABLE `commentmusic` (
`id` int PRIMARY KEY AUTO_INCREMENT,
`commentuser_name` varchar(20) NOT NULL,
`commentmusic_id` int(11) NOT NULL,
`commenttime` DATETIME DEFAULT now(),
`comment` varchar(100) NOT NULL
);

3.提交评论数据

路径:"/comment/upload"

3.1 后端代码书写 

1.MAPPER

@Mapper
public interface CommentMapper {@Insert("insert into commentmusic(commentmusic_id,commentuser_name,comment) values " +"(#{commentmusicId},#{commentuserName},#{comment})")Integer insertComment(Integer commentmusicId,String commentuserName,String comment);
}

2.SERVICE

@Service
@Slf4j
public class CommentService {@Autowiredprivate CommentMapper commentMapper;@Autowiredprivate MusicMapper musicMapper;public Result insertComment(Integer commentmusicId, String commentuserName, String comment){Comment commentable=new Comment();if(!StringUtils.hasLength(comment)){return Result.fail(Constant.RESULT_CODE_NO_CHOICE,"评论不能为空");}else if(musicMapper.selectByMusicId(commentmusicId)==null){return Result.fail(Constant.RESULT_CODE_NOTMP3_DELETEFAIL,"没有该音乐");}Integer factor=commentMapper.insertComment(commentmusicId,commentuserName,comment);if(factor<1){return Result.fail(Constant.RESULT_CODE_FAIL_SQL,"数据库插入失败");}commentable.setComment(comment);commentable.setCommentmusicId(commentmusicId);commentable.setCommentuserName(commentuserName);return Result.success(true);}
}

3.Controller

@RestController
@RequestMapping("/comment")
public class CommentController {@Autowiredprivate CommentService commentService;@RequestMapping("/upload")public Result insertComment(Integer commentmusicId, String comment, HttpSession httpSession){//获得当前的用户名User user= (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);String commentuserName=user.getUsername();return commentService.insertComment(commentmusicId, commentuserName, comment);}
}

3.2 后端测试

4.评论页面展示 

4.1 后端代码书写

MAPPER

@Select("select * from commentmusic where commentmusic_id=#{commentmusicId}")
List<Comment> selectAllComment(Integer commentmusicId);

SERVICE

public Result selectAllComment(Integer commentmusicId){return Result.success(commentMapper.selectAllComment(commentmusicId));
}

CONTROLLER

@RequestMapping("/list")
public Result selectAllComment(Integer commentmusicId){return Result.success(commentService.selectAllComment(commentmusicId));
}

4.2 后端测试

成功!!!

5.SECTION3和SECTION4的前端代码书写 

前端测试成功!!!

想加入删除评论的功能

这时候我们要让前端获取一些值,后端增加方法

@RequestMapping("/view")
public Result InsertUserInfo(HttpSession httpSession){User user=(User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);return Result.success(user.getUsername());
}

前端的书写 

        $(function(){

            $.ajax({

                type: "get",

                url: "/user/view",

                success: function(result){

                    if(result.status==200){

                        var userName=result.data;

                        load(userName);

                    }

                   

                }

            })

        });

        function load(userName){

            $.ajax({

                type: "get",

                url: "/comment/list"+location.search,              

                success: function(result){

                    if(result!=null&&result.status==200){

                        var data=result.data;

                        for(var i = 0; i < data.length;i++) {

                            var deleteflag='<td> <button class = "btn btn-primary"  οnclick="deleteInfo('+data[i].id+')"> 删除 </button>';

                            var value= data[i].comment+" ";

                            var p= document.createElement("p");

                            if(data[i].commentuserName==userName){

                                p.innerHTML="用户:"+data[i].commentuserName+'<hr>'+value+'<hr>'+data[i].commenttime+' '+deleteflag;

                            }else{

                                p.innerHTML="用户:"+data[i].commentuserName+'<hr>'+value+'<hr>'+data[i].commenttime;

                            }

                           

                            document.getElementById("commend").prepend(p);

                        }

                       

                    }

                },

                error: function(error){

                    if(error!=null&&error.status==401){

                        alert("请登录用户");

                        location.href = "login.html";

                    }

                }

            });

        }

        function send(){

            $.ajax({

                type: "get",

                url: "/comment/upload"+location.search,

                data:{

                    comment: $("#typing").val()

                },

                error: function(error){

                    if(error!=null&&error.status==401){

                        alert("请登录用户");

                        location.href = "login.html";

                    }

                },

                success: function(result){

                    location.reload();

                }

            });

        }

此时,自己写的评论可以看到删除按钮 

5.1 完成删除评论的功能

@RequestMapping("/delete")
public Result deleteComment(Integer id){return commentService.deleteComment(id);
}

function deleteInfo(id){

            $.ajax({

                type: "get",

                url: "/comment/delete",

                data:{

                    id: id,

                },

                success: function(result){

                    if(result.status==200){

                        alert("删除评论成功");

                        location.reload();

                    }

                }

            });

        }

 删除成功!!!


http://www.ppmy.cn/news/1429201.html

相关文章

Rust序列化和反序列化

Rust 编写python 模块 必备库 docker 启动 nginx 服务 NGINX 反向代理配置

第一次配置OpenWRT?首次使用需要修改这两个参数

前言 前几天更新了首篇OpenWRT的教程&#xff1a; 把OpenWRT固件写入U盘&#xff0c;开启x86软路由体验之旅 当系统开机之后&#xff0c;估计小伙伴都有点懵。因为OpenWRT的界面并不是咱们所熟悉的图形界面&#xff0c;开始前的配置都是需要从这个地方开始&#xff0c;这也是…

FFmpeg的详细介绍

FFmpeg是一套可以用来记录、转换数字音频、视频&#xff0c;并能将其转化为流的开源计算机程序。它提供了录制、转换数字音频、视频&#xff0c;并将其转化为流的库和工具。它包含了非常先进的音频/视频编解码库libavcodec&#xff0c;为了保证高可移植性和编解码质量&#xff…

CentOS命令大全:掌握关键命令及其精妙用法!

CentOS是一种流行的开源企业级Linux发行版&#xff0c;它基于Red Hat Enterprise Linux (RHEL)的源代码构建。对于系统管理员和运维工程师来说&#xff0c;掌握CentOS的常用命令至关重要。 这些命令不仅可以帮助管理服务器&#xff0c;还可以进行故障排查、性能监控和安全加固等…

【002_音频开发_基础篇_Linux音频架构简介】

002_音频开发_基础篇_Linux音频架构简介 文章目录 002_音频开发_基础篇_Linux音频架构简介创作背景Linux 音频架构ALSA 简介ASoC 驱动硬件架构软件架构MachinePlatformCodec ASoC 驱动 PCMALSA设备文件结构 ALSA 使用常用概念alsa-libALSA Open 流程ALSA Write 流程2种写入方法…

多端统一开发框架Taro、UniApp和WeApp这三个应用各自在前端开发领域有着独特的定位和功能

Taro、UniApp和WeApp这三个应用各自在前端开发领域有着独特的定位和功能&#xff0c;下面是对它们的详细介绍以及三者之间的对比&#xff0c;包括各自的优缺点和社区维护支持程度。 一、应用介绍 Taro Taro是一个多端统一开发框架&#xff0c;支持使用React语法编写一次代码…

postcss简介

PostCSS 是一个用 JavaScript 工具和插件生态系统来转换 CSS 代码的工具。它允许开发者使用现代 CSS 语法来编写样式&#xff0c;然后通过 PostCSS 插件将这些代码转换为大多数浏览器都能理解的格式。下面我将详细解释 PostCSS 的概念、功能以及它如何提高开发效率。 一&#…

【Python】自定义修改pip下载模块默认的安装路径

因为电脑下载了Anaconda提供的默认Python 3.9 以及后期下载的python3.10所以在Pychram进行项目开发时&#xff0c;发现一些库怎么导入都导入不了&#xff0c;手动install也是失败&#xff0c;后期在cmd里面发现python以及pip配置有点儿混乱&#xff0c;导致执行命令时&#xff…