项目方案:Java 商品评价表设计

1. 项目简介

本项目旨在设计一个可以存储和管理商品评价的数据表,使用Java编程语言实现。该表将包含商品评价的相关信息,如评价内容、评分、评价时间等,并提供相应的操作接口,以方便对评价数据的增删改查操作。

2. 数据库设计

为了实现商品评价表,我们需要设计一个数据库表来存储评价数据。下面是一个示例的评价表的数据库设计:

CREATE TABLE evaluation (
    id INT PRIMARY KEY AUTO_INCREMENT,
    product_id INT NOT NULL,
    content TEXT,
    rating INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

评价表包含以下字段:

  • id: 评价ID,为唯一标识符,采用自增方式生成。
  • product_id: 商品ID,用于关联商品信息。
  • content: 评价内容,使用TEXT类型存储。
  • rating: 评分,使用INT类型存储。
  • created_at: 创建时间,记录评价的创建时间。
  • updated_at: 更新时间,记录评价的最后更新时间。

3. Java 类设计

3.1 评价类(Evaluation)

我们首先需要设计一个评价类,用于封装评价的相关信息。以下是一个示例评价类的代码:

public class Evaluation {
    private int id;
    private int productId;
    private String content;
    private int rating;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    // 构造函数
    public Evaluation(int id, int productId, String content, int rating, LocalDateTime createdAt, LocalDateTime updatedAt) {
        this.id = id;
        this.productId = productId;
        this.content = content;
        this.rating = rating;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
    }

    // Getter 和 Setter 方法
    // ...
}

3.2 数据访问对象(DAO)

为了方便对评价数据进行数据库操作,我们可以设计一个数据访问对象(DAO)来封装与数据库的交互。以下是一个示例评价DAO的代码:

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EvaluationDAO {
    private static final String URL = "jdbc:mysql://localhost:3306/mydatabase";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";

    // 获取所有评价
    public List<Evaluation> getAllEvaluations() {
        List<Evaluation> evaluations = new ArrayList<>();
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM evaluation")) {
            while (rs.next()) {
                int id = rs.getInt("id");
                int productId = rs.getInt("product_id");
                String content = rs.getString("content");
                int rating = rs.getInt("rating");
                LocalDateTime createdAt = rs.getTimestamp("created_at").toLocalDateTime();
                LocalDateTime updatedAt = rs.getTimestamp("updated_at").toLocalDateTime();

                evaluations.add(new Evaluation(id, productId, content, rating, createdAt, updatedAt));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return evaluations;
    }

    // 根据商品ID获取评价
    public List<Evaluation> getEvaluationsByProductId(int productId) {
        List<Evaluation> evaluations = new ArrayList<>();
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement stmt = conn.prepareStatement("SELECT * FROM evaluation WHERE product_id = ?")) {
            stmt.setInt(1, productId);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    int id = rs.getInt("id");
                    String content = rs.getString("content");
                    int rating = rs.getInt("rating");
                    LocalDateTime createdAt = rs.getTimestamp("created_at").toLocalDateTime();
                    LocalDateTime updatedAt = rs.getTimestamp("updated_at").toLocalDateTime();

                    evaluations.add(new Evaluation(id, productId, content, rating, createdAt, updatedAt));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return evaluations;
    }

    // 插入评价
    public void insertEvaluation(Evaluation evaluation) {
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement stmt = conn.prepareStatement("INSERT INTO evaluation (product_id, content, rating) VALUES (?, ?, ?)")) {
            stmt.setInt(1, evaluation.getProductId());
            stmt.setString(2, evaluation.getContent());
            stmt.setInt(3, evaluation.getRating());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 更新评价
    public void updateEvaluation(Evaluation evaluation) {
        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);