实现评论商品详情的评论功能

本文介绍了如何使用MySQL、MVC架构、Servlet等技术实现一个商品详情的评论功能。包括动态页面渲染、用户登录、评论上传图片、过滤器筛选以及DAO、Service、Controller各层的详细分工。在商品详情页面,评论以List集合展示,并可通过Set集合进行重选操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习内容:

1.实现页面的渲染(从数据库拿数据去渲染页面,动态的可修改)

2.实现一个简单的登录功能

3.实现对新闻详情的评论

4.评论的时候可以上传照片

5.本次使用的是mysql+mvc+控制层+过滤器+js文件下的配置文件+webapp下的jsp文件+测试类

6.开发工具IDEA


  • domian层防止了用户+新闻类型+新闻详情+评论 对应MySQL中的四张表
  • dao下进行了数据交互
  • service进行了服务控制
  • controller是控制层
  • filter是过滤器

domain下的四个类:

package com.csi.news.domain;

import java.io.Serializable;

public class UserInfo implements Serializable {


    private Integer userId ;
    private String username ;
    private String password ;
    private String sex ;


    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
package com.csi.news.domain;

import java.io.Serializable;

public class NewsType implements Serializable {
    private Integer typeid ;
    private String  name ;
    private String  illustrate ;
    private String  img ;



    public Integer getTypeid() {
        return typeid;
    }

    public void setTypeid(Integer typeid) {
        this.typeid = typeid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIllustrate() {
        return illustrate;
    }

    public void setIllustrate(String illustrate) {
        this.illustrate = illustrate;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }
}
package com.csi.news.domain;
import java.io.Serializable;

import java.util.Date;
import java.util.Set;

public class NewsInfo implements Serializable {

    private Integer newsId;
    private String title;
    private Date time;
    private String source;
    private String context;
    private String edit;

    private NewsType newsType ;
    private Set<NewsComments> newsComments ;



    public Set<NewsComments> getNewsComments() {
        return newsComments;
    }

    public void setNewsComments(Set<NewsComments> newsComments) {
        this.newsComments = newsComments;
    }

    public NewsType getNewsType() {
        return newsType;
    }

    public void setNewsType(NewsType newsType) {
        this.newsType = newsType;
    }


    public Integer getNewsId() {
        return newsId;
    }

    public void setNewsId(Integer newsId) {
        this.newsId = newsId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public String getEdit() {
        return edit;
    }

    public void setEdit(String edit) {
        this.edit = edit;
    }


}



dao层下的接口和类

package com.csi.news.dao.impl;

import com.csi.news.dao.UserInfoDao;
import com.csi.news.domain.UserInfo;
import com.csi.news.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserInfoDaoImpl extends JDBCUtils implements UserInfoDao {

    @Override
    public UserInfo login(String username, String password) throws SQLException {

        final String SQL = "SELECT * FROM user WHERE username = ? AND password = ?" ;
        //2. 创建JDBC连接
        Connection connection = this.getConnection();
        //3. 创建PreparedStatement对象
        PreparedStatement ps = connection.prepareStatement(SQL);
        //3.1 设置占位符
        ps.setString(1,username);
        ps.setString(2,password);
        //4. 发起查询,返回ResultSet
        ResultSet rs = ps.executeQuery() ;
        UserInfo userInfo = null ;
        //5. 判断结果集是否存在数据
        if(rs.next()) {
            //6. 如果存在数据,封装对象,返回对象
            userInfo = new UserInfo() ;
            userInfo.setUserId(rs.getInt("UserId"));
            userInfo.setUsername(rs.getString("username"));
            userInfo.setSex(rs.getString("sex"));
        }
        //7. 关闭资源
        this.release(rs,ps,connection);

        return userInfo;
    }

    @Override
    public UserInfo findById(int userId) throws SQLException {

        final String SQL = "select * from user where userId = ?" ;
        Connection connection = this.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQL);
        ps.setInt(1,userId);
        ResultSet rs = ps.executeQuery();

        UserInfo userInfo = null ;
        if (rs.next()){
            userInfo = new UserInfo() ;
            userInfo.setUserId(rs.getInt("UserId"));
            userInfo.setUsername(rs.getString("Username"));
            userInfo.setSex(rs.getString("Sex"));
            userInfo.setPassword(rs.getString("Password"));
        }
        this.release(rs,ps,connection);
        return userInfo;
    }

}
package com.csi.news.dao.impl;

import com.csi.news.dao.NewsDao;
import com.csi.news.domain.NewsType;
import com.csi.news.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl extends JDBCUtils implements NewsDao {


    @Override
    public List<NewsType> findShow() throws SQLException {

        final String SQL = "SELECT * from `news_type`";
        Connection connection = this.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQL);
        ResultSet rs = ps.executeQuery();

        List<NewsType> news = new ArrayList<>();
        NewsType news1 = null;
        while (rs.next()) {
            news1 = new NewsType();
            news1.setTypeid(rs.getInt("Type_id"));
            news1.setName(rs.getString("name"));
            news1.setIllustrate(rs.getString("illustrate"));
            news1.setImg(rs.getString("img"));
            news.add(news1);

        }
        release(rs, ps, connection);

        return news;
    }

    @Override
    public NewsType select(int type_id) throws SQLException {

        final String SQL = "SELECT * from `news_type` WHERE type_id= ?";
        Connection connection = this.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQL);
        ps.setInt(1, type_id);
        ResultSet rs = ps.executeQuery();
        NewsType news = null;

        if (rs.next()) {
            news = new NewsType();
            news.setTypeid(rs.getInt("type_id"));
            news.setName(rs.getString("name"));
            news.setIllustrate(rs.getString("illustrate"));
            news.setImg(rs.getString("img"));

        }
        release(rs, ps, connection);

        return news;
    }
}
package com.csi.news.dao.impl;

import co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值