香烟商品销售网站

1 香烟商品销售网站概述

1.1 课题简介

本课题旨在设计并实现一个香烟商品销售网站,为消费者提供便捷的香烟购买渠道。该网站主要功能包括:

  1. 商品展示:展示各种香烟品牌、型号、价格、图片等信息,方便消费者浏览和选择。
  2. 用户管理:支持用户注册、登录、个人信息管理等功能,保障用户购物安全和隐私。
  3. 购物车管理:允许用户将心仪的香烟添加到购物车中,随时查看购物车内容并进行结算。
  4. 订单管理:处理用户订单,包括生成订单、查询订单状态、发货、退款等操作。
  5. 搜索功能:提供关键字搜索和品牌筛选功能,帮助用户快速找到所需香烟商品。
  6. 后台管理:管理员可以对商品信息、用户信息、订单信息等进行管理和维护。

1.2 设计目的

随着互联网的普及和电子商务的发展,越来越多的消费者选择在网上购买商品。香烟作为一种特殊商品,也逐渐在网上销售。本课题的设计目的在于:

  1. 满足消费者需求:为消费者提供一个方便、快捷、安全的香烟购买平台,满足消费者的购物需求。
  2. 提高销售效率:通过电子商务平台,香烟销售商可以降低运营成本,提高销售效率,扩大市场份额。
  3. 促进烟草行业发展:推动烟草行业的信息化建设,促进烟草行业的健康发展

1.3 系统开发所采用的技术

1.3.1 开发工具

IntelliJ IDEA:一款功能强大的 Java 集成开发环境,提供了丰富的代码编辑、调试、测试等功能,提高开发效率。

MySQL Workbench:一款可视化的数据库管理工具,方便数据库设计、创建、管理和维护。

Git:一款分布式版本控制系统,用于团队协作开发,方便代码管理和版本控制。

1.3.2 开发使用的数据库系统本系统使用 MySQL 8.0 作为数据库管理系统。MySQL 是一种开源的关系型数据库管理系统,具有性能高、稳定性好、易于使用等优点,广泛应用于各种 Web 应用程序中。

1.3.3 系统环境

操作系统:Windows 10 或以上版本。

内存:8GB 或以上。

硬盘空间:50GB 或以上。

Java 环境:JDK 1.8 或以上版本。

1.4 系统功能模块

  1. 商品管理:包括商品的添加、编辑、删除、查询等功能。

管理员可以通过商品管理功能对商品进行添加、编辑、删除等操作,同时可以根据关键字和品牌进行搜索,方便快捷地找到需要的商品。

  1. 订单管理:包括订单的生成、查询、处理等功能。

 订单管理:管理员可以通过订单管理功能对订单进行查询、处理等操作,及时了解订单的状态,提高订单处理效率。

图1-1订单流程图

  1. 用户管理:包括用户的注册、登录、信息修改等功能。

 用户管理:用户可以通过注册功能注册成为商城会员,登录后可以进行个人信息修改等操作,方便快捷地管理个人信息。

图1-2 登陆注册流程图

  1. 购物车管理:包括购物车的添加、删除、结算等功能。

 购物车管理:用户可以通过购物车管理功能将商品添加到购物车中,随时查看购物车中的商品信息,进行结算等操作,提高购物效率。

  1. 搜索功能:包括关键字搜索和品牌搜索功能。

 搜索功能:用户可以通过关键字搜索和品牌搜索功能快速找到需要的商品,提高购物体验。

1.4.1功能模块图

2 数据库设计

2.1 建立的数据库名称

建立的数据库名称:laptopmall

创建名为“laptopmall”的数据库的 SQL 脚本,其中包含了 6 个表:brand、cart、order、order_item、product 和 user。

2.2 所使用的表

 数据库设计是系统设计的重要组成部分,它直接影响到系统的性能和可扩展性。本系统采用了关系型数据库管理系统 MySQL,设计了商品表、订单表、用户表、购物车表等多张数据表,通过合理的表结构设计和索引优化,提高了数据库的查询效率和数据存储能力。

  1. brand:品牌表,用于存储商品的品牌信息。包含 id(品牌 ID,自增主键)和 name(品牌名称)两个字段。

表2.3-1品牌表

表名

字段名

类型

描述

brand

id

INT(11)

品牌 ID,自增主键

brand

name

VARCHAR(20)

品牌名称

  1. cart:购物车表,用于存储用户添加到购物车中的商品信息。包含 id(购物车 ID,自增主键)、product_id(商品 ID)、user_id(用户 ID)和 quantity(购买商品数量)四个字段。

表2.3-2购物车表

表名

字段名

类型

描述

cart

id

INT(11)

购物车 ID,自增主键

cart

product_id

INT(11)

商品 ID

cart

user_id

INT(11)

用户 ID

cart

quantity

INT(11)

购买商品数量

  1. order:订单表,用于存储用户的订单信息。包含 id(订单 ID,自增主键)、user_id(用户 ID)、payment(付款金额)和 pay_time(下单时间)四个字段。

表2.3-3订单表

表名

字段名

类型

描述

order

id

INT(11)

订单 ID,自增主键

order

user_id

INT(11)

用户 ID

order

payment

DECIMAL(20,2)

付款金额

order

pay_time

DATETIME

下单时间

  1. order_item:订单项表,用于存储订单中的商品信息。包含 id(订单项 ID,自增主键)、user_id(用户 ID)、order_id(订单 ID)、product_name(商品名称)、product_image(商品图片)、product_price(商品单价)、quantity(购买数量)和 total_price(该订单项金额小计)八个字段。

表2.3-4订单项表

表名

字段名

类型

描述

order_item

id

INT(11)

订单项 ID,自增主键

order_item

user_id

INT(11)

用户 ID

order_item

order_id

INT(11)

订单 ID

order_item

product_name

VARCHAR(20)

商品名称

order_item

product_image

VARCHAR(100)

商品图片

order_item

product_price

DECIMAL(50,2)

商品单价

order_item

quantity

INT(11)

购买数量

order_item

total_price

DECIMAL(50,2)

该订单项金额小计

  1. product:商品表,用于存储商品的详细信息。包含 id(商品 ID,自增主键)、brand_id(品牌 ID)、name(商品名称)、image(商品图片)、detail(商品详情)、stock(商品库存)和 price(商品单价)七个字段。

表2.3-5商品表

表名

字段名

类型

描述

product

id

INT(11)

商品 ID,自增主键

product

brand_id

INT(11)

品牌 ID

product

name

VARCHAR(20)

商品名称

product

image

VARCHAR(50)

商品图片

product

detail

VARCHAR(100)

商品详情

product

stock

INT(11)

商品库存

product

price

DECIMAL(50,2)

商品单价


 

3 香烟商品销售网站设计与实现

  1. 注册登录:

用户注册:用户填写注册信息,包括登录名、密码、确认密码等,系统验证注册信息的合法性,将注册信息存储到数据库中。

图3-1注册

用户登录:用户输入登录名和密码,系统验证登录信息的合法性,将用户信息存储到 session 中,跳转到商城页面。

图3-2 登录

控制层(Action):

RegisterServlet:处理用户注册请求,验证注册信息的合法性,将注册信息存储到数据库中。

@WebServlet("/register")

public class RegisterServlet extends HttpServlet{

/**

 *

 */

private static final long serialVersionUID = 1L;

private UserDAO userDAO;

@Override

public void init() throws ServletException {

userDAO = new UserDAO();

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

req.setCharacterEncoding("utf-8");

String loginName = req.getParameter("login_name");

String password = req.getParameter("password");

String ck_password = req.getParameter("check_password");

if(!password.equals(ck_password)) {

req.setAttribute("pwdMsg", "前后密码不一致");

req.getRequestDispatcher("register.jsp").forward(req, resp);

return;

}

// 根据登录名查询数据库,判断用户是否存在

if(userDAO.getUserByLoginName(loginName)!=null) {

req.setAttribute("loginNameMsg", "用户名已存在,请重新输入");

req.getRequestDispatcher("register.jsp").forward(req, resp);

return;

}

User user = new User();

user.setLoginName(loginName);

user.setPassword(password);

userDAO.insertUser(user);

req.setAttribute("loginNameMsg", "注册成功");

req.getRequestDispatcher("register.jsp").forward(req, resp);

}

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doPost(req, resp);

}

}

服务层(Service):

UserService:提供用户相关的业务逻辑,包括获取用户信息、注册用户、更新用户信息、删除用户等。

public User getUserByLoginName(String loginName) {

    return userDAO.getUserByLoginName(loginName);

}

public User getUserByLoginNameAndPassword(String loginName, String password) {

    return userDAO.getUserByLoginNameAndPassword(loginName, password);

}

public boolean insertUser(User user) {

    return userDAO.insertUser(user);

}

public boolean updateUser(User newUser) {

    return userDAO.updateUser(newUser);

}

public boolean deleteUserById(Integer id) {

    return userDAO.deleteUserById(id);

}

数据访问层(DAO):

UserDAO:与数据库进行交互,实现用户信息的增删改查操作。

public User getUserByLoginName(String loginName) {

    String sql = "select * from user where login_name=?";

    Connection conn = null;

    PreparedStatement ps = null;

    ResultSet rs = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setString(1, loginName);

        rs = ps.executeQuery();

        if (rs.next()) {

            User user = new User();

            user.setId(rs.getInt("id"));

            user.setLoginName(rs.getString("login_name"));

            user.setPassword(rs.getString("password"));

            user.setRole(rs.getInt("role"));

            user.setRealName(rs.getString("real_name"));

            user.setPhone(rs.getString("phone"));

            user.setAddress(rs.getString("address"));

            return user;

        }

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        JdbcUtil.close(conn, ps, rs);

    }

    return null;

}

public User getUserByLoginNameAndPassword(String loginName, String password) {

    String sql = "select * from user where login_name=? and password=?";

    Connection conn = null;

    PreparedStatement ps = null;

    ResultSet rs = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setString(1, loginName);

        ps.setString(2, password);

        rs = ps.executeQuery();

        if (rs.next()) {

            User user = new User();

            user.setId(rs.getInt("id"));

            user.setLoginName(rs.getString("login_name"));

            user.setPassword(rs.getString("password"));

            user.setRole(rs.getInt("role"));

            user.setRealName(rs.getString("real_name"));

            user.setPhone(rs.getString("phone"));

            user.setAddress(rs.getString("address"));

            return user;

        }

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        JdbcUtil.close(conn, ps, rs);

    }

    return null;

}

public boolean insertUser(User user) {

    String sql = "insert into user(login_name,password,role,real_name,phone,address) values(?,?,?,?,?,?)";

    Connection conn = null;

    PreparedStatement ps = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setString(1, user.getLoginName());

        ps.setString(2, user.getPassword());

        ps.setInt(3, user.getRole());

        ps.setString(4, user.getRealName());

        ps.setString(5, user.getPhone());

        ps.setString(6, user.getAddress());

        return ps.execute();

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        JdbcUtil.close(conn, ps, null);

    }

    return false;

}

public boolean updateUser(User newUser) {

    String sql = "update user set login_name=?,role=?,real_name=?,phone=?,address=? where id=?";

    Connection conn = null;

    PreparedStatement ps = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setString(1, newUser.getLoginName());

        ps.setInt(2, newUser.getRole());

        ps.setString(3, newUser.getRealName());

        ps.setString(4, newUser.getPhone());

        ps.setString(5, newUser.getAddress());

        ps.setInt(6, newUser.getId());

        return ps.execute();

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        JdbcUtil.close(conn, ps, null);

    }

    return false;

}

public boolean deleteUserById(Integer id) {

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

    Connection conn = null;

    PreparedStatement ps = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setInt(1, id);

        return ps.execute();

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        JdbcUtil.close(conn, ps, null);

    }

    return false;

}

  1. 分页查询:

商品分页查询:系统根据用户输入的关键字和品牌,查询数据库中的商品信息,将商品信息分页显示在商城页面上。

图3-3商品

控制层(Action):

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

req.setCharacterEncoding("utf-8");

User user = (User) req.getSession().getAttribute("CURRENT_USER");

String keyword = req.getParameter("keyword");

String brandIdStr = req.getParameter("brand_id");

String curPageStr = req.getParameter("current_page");

keyword = keyword == null ? "" : keyword;

brandIdStr = brandIdStr == null ? "0" : brandIdStr;

// 封装查询信息

QueryObject qo = new QueryObject(keyword, Integer.parseInt(brandIdStr));

if (curPageStr != null) {

qo.setCurrentPage(Integer.parseInt(curPageStr));

}

// 从数据库获取商品的分页信息

PageInfo<Product> pageInfo = productDAO.listProducts(qo);

req.setAttribute("pageInfo", pageInfo);

// 品牌信息显示

List<Brand> brands = brandDAO.listBrands();

req.setAttribute("brands", brands);

// 高级查询数据回显

req.setAttribute("qo", qo);

if (user.getRole() == 1) {

// 跳转到后台管理界面

req.getRequestDispatcher("/backend/product_list.jsp").forward(req, resp);

} else {

// 跳转到用户购物界面

req.getRequestDispatcher("/portal/product_list.jsp").forward(req, resp);

}

}

服务层(Service):

public class PorductService {

    ProductDAO dao = new ProductDAO();

    public Product getProductById(Integer id) {

        Product productById = dao.getProductById(id);

        return productById;

    }

    public boolean deleteProductById(Integer id) {

        boolean b = dao.deleteProductById(id);

        return b;

    }

    public boolean insertProduct(Product product) {

        boolean insertProduct = dao.insertProduct(product);

        return insertProduct;

    }

    public boolean updateProduct(Product newProduct) {

        boolean updateProduct = dao.updateProduct(newProduct);

        return updateProduct;

    }

    public PageInfo<Product> listProducts(QueryObject qo) {

        PageInfo<Product> productPageInfo = dao.listProducts(qo);

        return productPageInfo;

    }

    public List<Product> listProducts() {

        List<Product> products = dao.listProducts();

        return products;

    }

}

数据访问层(DAO):

public Product getProductById(Integer id) {

String sql = "select * from product where id=?";

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, id);

rs = ps.executeQuery();

if (rs.next()) {

Product product = new Product();

product.setId(rs.getInt("id"));

product.setBrandId(rs.getInt("brand_id"));

product.setName(rs.getString("name"));

product.setImage(rs.getString("image"));

product.setDetail(rs.getString("detail"));

product.setStock(rs.getInt("stock"));

product.setPrice(rs.getBigDecimal("price"));

return product;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, rs);

}

return null;

}

public boolean deleteProductById(Integer id) {

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

Connection conn = null;

PreparedStatement ps = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, id);

return ps.execute();

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, null);

}

return false;

}

服务层(Service):

public class PorductService {

    ProductDAO dao = new ProductDAO();

    public Product getProductById(Integer id) {

        Product productById = dao.getProductById(id);

        return productById;

    }

    public boolean deleteProductById(Integer id) {

        boolean b = dao.deleteProductById(id);

        return b;

    }

    public boolean insertProduct(Product product) {

        boolean insertProduct = dao.insertProduct(product);

        return insertProduct;

    }

    public boolean updateProduct(Product newProduct) {

        boolean updateProduct = dao.updateProduct(newProduct);

        return updateProduct;

    }

    public PageInfo<Product> listProducts(QueryObject qo) {

        PageInfo<Product> productPageInfo = dao.listProducts(qo);

        return productPageInfo;

    }

    public List<Product> listProducts() {

        List<Product> products = dao.listProducts();

        return products;

    }

}

  1. 分页条件(精确、模糊)查询商品信息功能

用户可以根据商品的特定属性(如商品名称、编码、价格、库存量等)进行精确/模糊匹配查询。

图3-4 搜索

控制层(Action):

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

req.setCharacterEncoding("utf-8");

User user = (User) req.getSession().getAttribute("CURRENT_USER");

String keyword = req.getParameter("keyword");

String brandIdStr = req.getParameter("brand_id");

String curPageStr = req.getParameter("current_page");

keyword = keyword == null ? "" : keyword;

brandIdStr = brandIdStr == null ? "0" : brandIdStr;

// 封装查询信息

QueryObject qo = new QueryObject(keyword, Integer.parseInt(brandIdStr));

if (curPageStr != null) {

qo.setCurrentPage(Integer.parseInt(curPageStr));

}

// 从数据库获取商品的分页信息

PageInfo<Product> pageInfo = productDAO.listProducts(qo);

req.setAttribute("pageInfo", pageInfo);

// 品牌信息显示

List<Brand> brands = brandDAO.listBrands();

req.setAttribute("brands", brands);

// 高级查询数据回显

req.setAttribute("qo", qo);

if (user.getRole() == 1) {

// 跳转到后台管理界面

req.getRequestDispatcher("/backend/product_list.jsp").forward(req, resp);

} else {

// 跳转到用户购物界面

req.getRequestDispatcher("/portal/product_list.jsp").forward(req, resp);

}

}

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doPost(req, resp);

}

}

数据访问层(DAO):

public Product getProductById(Integer id) {

String sql = "select * from product where id=?";

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, id);

rs = ps.executeQuery();

if (rs.next()) {

Product product = new Product();

product.setId(rs.getInt("id"));

product.setBrandId(rs.getInt("brand_id"));

product.setName(rs.getString("name"));

product.setImage(rs.getString("image"));

product.setDetail(rs.getString("detail"));

product.setStock(rs.getInt("stock"));

product.setPrice(rs.getBigDecimal("price"));

return product;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, rs);

}

return null;

}

服务层(Service):

package service;

import bean.Product;

import dao.ProductDAO;

import bean.PageInfo;

import bean.QueryObject;

import java.util.List;

public class PorductService {

    ProductDAO dao = new ProductDAO();

    public Product getProductById(Integer id) {

        Product productById = dao.getProductById(id);

        return productById;

    }

    public boolean deleteProductById(Integer id) {

        boolean b = dao.deleteProductById(id);

        return b;

    }

    public boolean insertProduct(Product product) {

        boolean insertProduct = dao.insertProduct(product);

        return insertProduct;

    }

    public boolean updateProduct(Product newProduct) {

        boolean updateProduct = dao.updateProduct(newProduct);

        return updateProduct;

    }

    public PageInfo<Product> listProducts(QueryObject qo) {

        PageInfo<Product> productPageInfo = dao.listProducts(qo);

        return productPageInfo;

    }

    public List<Product> listProducts() {

        List<Product> products = dao.listProducts();

        return products;

    }

}

  1. 购物车功能:

添加商品到购物车:用户在商城页面上选择商品,点击“加入购物车”按钮,系统将商品信息添加到购物车中。

查看购物车:用户点击“我的购物车”按钮,系统查询购物车中的商品信息,将商品信息显示在购物车页面上。

移除商品:用户在购物车页面上选择商品,点击“移除”按钮,系统将商品信息从购物车中移除。

提交订单:用户在购物车页面上点击“提交订单”按钮,系统将购物车中的商品信息生成订单,将订单信息存储到数据库中,跳转到订单管理页面。

图3-5购物车

控制层(Action):

AddToCartServlet:

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

req.setCharacterEncoding("utf-8");

User user = (User)req.getSession().getAttribute("CURRENT_USER");

String prodIdStr = req.getParameter("product_id");

String quantityStr = req.getParameter("quantity");

// 查询数据库,查看当前用户的购物车是否已经有该商品

Cart cart = cartDAO.getCartByProductIdAndUserId(user.getId(), Integer.parseInt(prodIdStr));

if(cart==null) {

//购物车没该商品,添加进购物车

Cart newCart = new Cart();

newCart.setProductId(Integer.parseInt(prodIdStr));

newCart.setQuantity(Integer.parseInt(quantityStr));

newCart.setUserId(user.getId());

cartDAO.insertCart(newCart);

}else {

//购物车已有该商品,修改其数量

cartDAO.updateQuantityByCartId(cart.getId(), cart.getQuantity()+Integer.parseInt(quantityStr));

}

req.getRequestDispatcher("/portal/add_cart_success.jsp").forward(req, resp);

}

服务层(Service):

package service;

import bean.Cart;

import dao.CartDAO;

import java.util.List;

public class CartService {

   CartDAO dao = new CartDAO();

   public List<Cart> listCartByUserId(Integer userId) {

      List<Cart> carts = dao.listCartByUserId(userId);

      return carts;

   }

   public Cart getCartByProductIdAndUserId(Integer userId,Integer productId) {

      Cart cartByProductIdAndUserId = dao.getCartByProductIdAndUserId(userId, productId);

      return cartByProductIdAndUserId;

   }

   public boolean deleteByCartId(Integer cartId) {

      boolean b = dao.deleteByCartId(cartId);

      return b;

   }

   public boolean insertCart(Cart cart) {

      boolean insertCart = dao.insertCart(cart);

      return insertCart;

   }

   public boolean deleteCartByUserId(Integer userId) {

      boolean deleteCartByUserId = dao.deleteCartByUserId(userId);

      return deleteCartByUserId;

   }

}

数据访问层(DAO):

CartDAO:

public Cart getCartByProductIdAndUserId(Integer userId, Integer productId) {

    String sql = "select * from cart where user_id =? and product_id =?";

    Connection conn = null;

    PreparedStatement ps = null;

    ResultSet rs = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setInt(1, userId);

        ps.setInt(2, productId);

        rs = ps.executeQuery();

        if (rs.next()) {

            Cart cart = new Cart();

            cart.setId(rs.getInt("id"));

            cart.setProductId(rs.getInt("product_id"));

            cart.setQuantity(rs.getInt("quantity"));

            cart.setUserId(rs.getInt("user_id"));

            return cart;

        }

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        JdbcUtil.close(conn, ps, rs);

    }

    return null;

}

public List<CartItem> listCartItems(Integer userId) {

    String sql = "select c.id, c.product_id, c.quantity, p.name, p.price " +

            "from cart c " +

            "inner join product p on c.product_id = p.id " +

            "where c.user_id =?";

    List<CartItem> cartItems = new ArrayList<>();

    Connection conn = null;

    PreparedStatement ps = null;

    ResultSet rs = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setInt(1, userId);

        rs = ps.executeQuery();

        while (rs.next()) {

            CartItem cartItem = new CartItem();

            cartItem.setId(rs.getInt("id"));

            cartItem.setProductId(rs.getInt("product_id"));

            cartItem.setQuantity(rs.getInt("quantity"));

            cartItem.setProductName(rs.getString("name"));

            cartItem.setProductPrice(rs.getBigDecimal("price"));

            cartItems.add(cartItem);

        }

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        JdbcUtil.close(conn, ps, rs);

    }

    return cartItems;

}

public void updateQuantityByCartId(Integer cartId, Integer quantity) {

    String sql = "update cart set quantity =? where id =?";

    Connection conn = null;

    PreparedStatement ps = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setInt(1, quantity);

        ps.setInt(2, cartId);

        ps.executeUpdate();

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        JdbcUtil.close(conn, ps, null);

    }

}

public void deleteByCartId(Integer cartId) {

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

    Connection conn = null;

    PreparedStatement ps = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setInt(1, cartId);

        ps.executeUpdate();

    } catch (SQLException e) {

        e.printStackTrace();

    } finally {

        JdbcUtil.close(conn, ps, null);

    }

}

public void deleteCartByUserId(Integer userId) {

    String sql = "delete from cart where user_id =?";

    Connection conn = null;

    PreparedStatement ps = null;

    try {

        conn = JdbcUtil.getConnection();

        ps = conn.prepareStatement(sql);

        ps.setInt(1, userId);

  1. 订单管理功能:

查看订单:用户在订单管理页面上查询订单信息。

图3-6订单

控制层(Action):

@Override

public void init() throws ServletException {

orderItemDAO = new OrderItemDAO();

orderDAO = new OrderDAO();

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

req.setCharacterEncoding("utf-8");

User user = (User)req.getSession().getAttribute("CURRENT_USER");

String orderIdStr = req.getParameter("order_id");

// 获取当前订单下所有订单项

List<OrderItem> orderItems = orderItemDAO.ListOrderItemsByUserIdAndOrderId(user.getId(), Integer.parseInt(orderIdStr));

req.setAttribute("orderItems", orderItems);

// 获取当前订单的信息

Order order = orderDAO.getOrderById(Integer.parseInt(orderIdStr));

req.setAttribute("order", order);

req.getRequestDispatcher("/portal/order_detail.jsp").forward(req, resp);

}

服务层(Service):

public class OrderService {

    OrderDAO dao = new OrderDAO();

    public PageInfo<Order> listOrders(Integer userId, Integer currentPage, Integer pageSize) {

        PageInfo<Order> orderPageInfo = dao.listOrders(userId, currentPage, pageSize);

        return orderPageInfo;

    }

    public PageInfo<Order> listOrders(Integer currentPage, Integer pageSize) {

        PageInfo<Order> orderPageInfo = dao.listOrders(currentPage, pageSize);

        return orderPageInfo;

    }

    public Order getOrderById(Integer id) {

        Order orderById = dao.getOrderById(id);

        return orderById;

    }

    public boolean insertOder(Order order) {

        boolean b = dao.insertOder(order);

        return b;

    }

    public boolean updateOrder(Order newOrder) {

        boolean updateOrder = dao.updateOrder(newOrder);

        return updateOrder;

    }

    public List<Order> listOrders(Integer userId) {

        List<Order> orders = dao.listOrders(userId);

        return orders;

    }

    public List<Order> listOrders() {

        List<Order> order = dao.listOrders();

        return order;

    }

数据访问层(DAO):

public PageInfo<Order> listOrders(Integer userId, Integer currentPage, Integer pageSize) {

String sql = "select * from `order` where user_id=? order by pay_time desc limit ?,?";

List<Order> list = new ArrayList<>();

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, userId);

ps.setInt(2, (currentPage - 1) * pageSize);

ps.setInt(3, pageSize);

rs = ps.executeQuery();

while (rs.next()) {

Order order = new Order();

order.setId(rs.getInt("id"));

order.setUserId(rs.getInt("user_id"));

order.setPayment(rs.getBigDecimal("payment"));

order.setPayTime(new Date(rs.getTimestamp("pay_time").getTime()));

list.add(order);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, rs);

}

return new PageInfo<>(list, currentPage, pageSize, getOrderCount(userId));

}

// 计算当前用户有多少订单记录

private int getOrderCount(Integer userId) {

String sql = "select count(1) from `order` where user_id=?";

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, userId);

rs = ps.executeQuery();

rs.next();

return rs.getInt(1);

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, rs);

}

return 0;

}

/**

 * 获取系统全部订单信息 分页显示

 *

 * @param currentPage

 * @param pageSize

 * @return

 */

public PageInfo<Order> listOrders(Integer currentPage, Integer pageSize) {

String sql = "select * from `order` order by pay_time desc limit ?,?";

List<Order> list = new ArrayList<>();

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, (currentPage - 1) * pageSize);

ps.setInt(2, pageSize);

rs = ps.executeQuery();

while (rs.next()) {

Order order = new Order();

order.setId(rs.getInt("id"));

order.setUserId(rs.getInt("user_id"));

order.setPayment(rs.getBigDecimal("payment"));

order.setPayTime(new Date(rs.getTimestamp("pay_time").getTime()));

list.add(order);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, rs);

}

return new PageInfo<>(list, currentPage, pageSize, getOrderCount());

}

// 计算当前系统内有多少订单记录

private int getOrderCount() {

String sql = "select count(1) from `order`";

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

rs.next();

return rs.getInt(1);

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, rs);

}

return 0;

}

/**

 * 根据订单号获取某一订单信息

 *

 * @param id

 * @return

 */

public Order getOrderById(Integer id) {

String sql = "select * from `order` where id=?";

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, id);

rs = ps.executeQuery();

if (rs.next()) {

Order order = new Order();

order.setId(rs.getInt("id"));

order.setUserId(rs.getInt("user_id"));

order.setPayment(rs.getBigDecimal("payment"));

order.setPayTime(new Date(rs.getTimestamp("pay_time").getTime()));

return order;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, rs);

}

return null;

}

/**

 * 新增订单

 *

 * @param order

 * @return

 */

public boolean insertOder(Order order) {

String sql = "insert into `order`(user_id,payment,pay_time) values(?,?,now())";

Connection conn = null;

PreparedStatement ps = null;

try {

conn = JdbcUtil.getConnection();

// 自动返回主键

ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

ps.setInt(1, order.getUserId());

ps.setBigDecimal(2, order.getPayment());

// ps.setDate(3, new Date(order.getPayTime().getTime()));

boolean result = ps.execute();

ResultSet rs = ps.getGeneratedKeys();

rs.next();

order.setId(rs.getInt(1));

return result;

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, null);

}

return false;

}

/**

 * 更新Order信息

 *

 * @param newOrder

 * @return

 */

public boolean updateOrder(Order newOrder) {

String sql = "update `order` set user_id=?,payment=?,pay_time=now() where id=?";

Connection conn = null;

PreparedStatement ps = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, newOrder.getUserId());

ps.setBigDecimal(2, newOrder.getPayment());

ps.setInt(3, newOrder.getId());

return ps.execute();

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, null);

}

return false;

}

// 查看某个用户的全部订单信息 不分页

public List<Order> listOrders(Integer userId) {

String sql = "select * from `order` where user_id=? order by pay_time desc";

List<Order> list = new ArrayList<>();

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, userId);

rs = ps.executeQuery();

while (rs.next()) {

Order order = new Order();

order.setId(rs.getInt("id"));

order.setUserId(rs.getInt("user_id"));

order.setPayment(rs.getBigDecimal("payment"));

order.setPayTime(new Date(rs.getTimestamp("pay_time").getTime()));

list.add(order);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, rs);

}

return list;

}

// 查看全部订单信息 不分页

public List<Order> listOrders() {

String sql = "select * from `order` order by pay_time desc";

List<Order> list = new ArrayList<>();

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

while (rs.next()) {

Order order = new Order();

order.setId(rs.getInt("id"));

order.setUserId(rs.getInt("user_id"));

order.setPayment(rs.getBigDecimal("payment"));

order.setPayTime(new Date(rs.getTimestamp("pay_time").getTime()));

list.add(order);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

JdbcUtil.close(conn, ps, rs);

}

return list;

}

4 课程设计总结

通过本次香烟商品销售网站的设计与实现,我不仅深入了解了 JavaWeb 应用开发的相关知识和技术,还提高了自己的编程能力和解决问题的能力。

在系统开发过程中,我遇到了许多挑战和困难。例如,如何设计合理的数据库结构,如何实现复杂的业务逻辑,如何保证系统的安全性和稳定性等。但是,通过不断地学习和实践,我逐渐克服了这些困难,并成功地完成了系统的开发。

在系统设计方面,我采用了 MVC 架构模式,将系统分为模型层、视图层和控制层。这种架构模式使得系统的结构更加清晰,易于维护和扩展。同时,我还使用了 MySQL 数据库来存储系统的数据,并通过 JDBC 技术实现了数据库的连接和操作。

在系统实现方面,我使用了 Java 语言来编写系统的代码,并使用了 Servlet、JSP、JavaBean 等技术来实现系统的功能。同时,我还使用了 Ajax 技术来实现异步请求,提高了系统的响应速度和用户体验。

在系统测试方面,我进行了充分的测试,包括单元测试、集成测试和系统测试。通过测试,我发现并解决了系统中存在的许多问题,保证了系统的质量和稳定性。

通过本次课程设计,我深刻体会到了软件开发的复杂性和挑战性。同时,我也意识到了团队合作的重要性。在开发过程中,我与小组成员密切合作,共同解决了许多问题,提高了工作效率和质量。

此外,我还发现了自己在编程方面的不足之处,例如对某些技术的掌握不够深入,对代码的规范和风格不够重视等。在今后的学习和工作中,我将继续努力学习和提高自己的编程能力,注重代码的规范和风格,提高代码的质量和可读性。

总的来说,本次课程设计是一次非常有意义的实践活动,它让我学到了很多知识和技能,也让我认识到了自己的不足之处。在今后的学习和工作中,我将继续努力,不断提高自己的能力和水平,为软件开发事业做出更大的贡献。

完整代码:

  • 20
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值