码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • JavaWeb评论功能实现步骤及代码


    文章目录

    目录

    文章目录

    前言

    一、分析功能

    二、实现功能

    1.建评论表

    2.Dao层、service层核心代码实现

     3.servlet层编写核心代码

     4.jsp核心代码

     三、展示效果图

    总结


    前言

    评论功能是后端要写常见的功能之一,一般性的网站也都会包含这一功能。像是购物网站、视频网站下方都会有用户评论的功能。

     

    一、分析功能

    首先要分析功能:1.用户登录点击商品后可查看所有普通用户的评论。

                                 2.用户可以添加评论,发送到评论区。

                                 3.用户可以删除该用户写的评论。(不能删除其他人的评论)

    二、实现功能

    1.建评论表

    外键约束:user_id关联user表、motorcycle_id关联商品表(motorcycle)。

     然后创建实体类。

    2.Dao层、service层核心代码实现

    查询评论:

    String sql="select c.id, c.user_id,c.motorcycle_id,c.motorcycle_comment,u.username from comment c left join user u on c.user_id=u.id where c.motorcycle_id=?";

    添加评论: 

    String sql = "insert into comment(user_id,motorcycle_id,motorcycle_comment) values(?,?,?)"

    删除评论:

    String sql = "delete from comment where id=?";

     service层直接调用,不做处理。

    clist = cDao.getMotorcycleComment(motorcycleId);

     3.servlet层编写核心代码

    将查询结果放到request域里。

    1. List clist=commentService.getMotorcycleComment(id);
    2. // for (Comment c:clist
    3. // ) {
    4. // System.out.println(c);
    5. // }
    6. request.getSession().setAttribute("MotorcycleId", id);
    7. request.setAttribute("clist", clist);

    调用删除后重定向到详情页。 

    1. commentService.deleteComment(commentId);
    2. resp.sendRedirect("/motorcycle_detail?id="+motorcycleId);

    添加也是,添加完后重定向到商品详情页。 

    1. CommentService commentService=new CommentService();
    2. int userId= Integer.parseInt(req.getParameter("userId"));
    3. int motorcycleId= Integer.parseInt(req.getParameter("motorcycleId"));
    4. String motorcycleComment=req.getParameter("comment");
    5. commentService.addComment(userId,motorcycleId,motorcycleComment);
    6. // req.getRequestDispatcher("/motorcycle_detail?id="+motorcycleId).forward(req, resp);
    7. resp.sendRedirect("/motorcycle_detail?id="+motorcycleId);

     4.jsp核心代码

     


     三、展示效果图

    效果查看

    添加一条后

     数据库变化:新增一条信息

     点击删除:发现已经没有该评论。

     刷新数据库后:

     

    总结

    效果展示完成。实现起来不算难,但要明白其中的外键约束关系,明白其中的逻辑。代码不是很多,大家快练起来~

  • 相关阅读:
    TP5.1 对数组进行排序
    价值几十亿美金的名字,Microsoft Windows的由来
    前端面试宝典React篇08 列举一种你了解的 React 状态管理框架
    常用的git指令
    Python经典习题(二)
    Error: TF_DENORMALIZED_QUATERNION: Ignoring transform forchild_frame_id
    Redux 4.2 版本发布由createStore --》configureStore
    基于dlib进行人脸识别demo
    1404. 将二进制表示减到1的步骤数
    Linux--进程间通讯--FIFO(open打开)
  • 原文地址:https://blog.csdn.net/weixin_52258054/article/details/127640840
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号