【web 编程技术】基于 B/S 架构的电商平台(java web)

基于 B/S 架构的电商平台(java web)

  • 课程设计实验目的
  • 课程设计实验环境
  • 课程设计功能概述
  • 课程设计需求分析
    • 三层架构图
    • 功能列表
    • 系统用例图
    • 系统活动图-用户端
    • 需求分析
  • 课程设计详细设计
    • 实现过程
      • 数据库
      • BaseServlet 的实现
      • 商品显示模块-分页显示所有商品、查看单个商品详细信息
      • 用户模块-新用户注册、用户登录、用户退出
      • 支付模块-完成在线支付功能
      • 购物车模块-加入商品到购物车、修改所购商品数量
      • 订单模块-提交订单并显示订单信息、用户确认后生成正式订单、用户查看历史订单
      • 收货地址-管理收货地址(增删改查)
      • 管理模块-增加、删除、修改、查找会员、订单、商品信息
    • 测试过程
      • 用户模块-新用户注册、用户登录、用户退出
      • 商品显示模块-分页显示所有商品、查看单个商品详细信息
      • 购物车模块-加入商品到购物车、修改所购商品数量
      • 订单模块-提交订单并显示订单信息、用户确认后生成正式订单、用户查看历史订单
      • 支付模块-完成在线支付功能
      • 收货地址-管理收货地址 (增删改查)
      • 管理模块-增加、删除、修改、查找会员、订单、商品信息
  • 项目代码来源与下载

课程设计实验目的

本次实验旨在通过开发一个基于B/S架构的电商平台,深入学习Web开发技术,掌握相关知识和技能。通过本综合实验可以学到如下内容:

  • Web开发技术:掌握了B/S架构、前端技术(JSP、CSS、JavaScript)和后端技术(Java、Servlet),利用Ajax实现了前后端数据交互,提升了用户体验。
  • 数据库技术:学习了MySQL数据库的使用与管理,设计了安全稳定的数据库结构,并应用了C3P0连接池技术以提高开发效率。
  • 系统开发流程:深入了解了系统开发的全过程,包括需求分析、技术选型、架构设计、编码实现、测试调试等各个环节。
  • 安全防护:掌握了MD5和Base64等加密技术,保护用户隐私和信息安全,并学习了防止SQL注入、XSS攻击等安全措施。
  • 系统功能设计:熟悉了电商平台的业务流程和功能需求,涵盖了商品展示、用户管理、订单处理、支付等功能的设计和实现。
  • 代码设计和优化:运用反射技术创建BaseServlet,实现一个servlet对应多个方法,提高了代码的复用性和可维护性。同时,使用BeanUtils工具类实现了不同对象之间的映射。

课程设计实验环境

  • PyCharm - 用于编写和管理Java代码。
  • MySQL - 用作关系型数据库管理系统,存储和管理数据。
  • Chrome浏览器 -用于调试和展示Web应用。
  • Apache Tomcat 8.5.97 - 用作Web服务器,运行和管理Java Web应用程序

课程设计功能概述

MyShop商城是一个完整的电商购物平台,可通过正确的用户名及其密码进入系统购物。系统分为七大模块,商品展示模块、用户模块、购物车模块、订单模块、支付模块、后台模块。该系统分为用户端和管理端两部分。

  • 用户端:主要功能包括浏览商品,加入购物车,支付订单,查询订单。
  • 管理端:主要功能包括增加、删除、修改、查找会员、订单、商品信息。

该系统有三个参与者,分别为会员、游客和管理员。会员登录后可以浏览商品信息,将商品加入购物车,下单,支付订单;游客可以浏览商品信息,但在加入购物车时会弹出会员登录注册窗口;管理员登录后可以管理商品信息、会员信息以及订单信息。

课程设计需求分析

三层架构图

该系统采用了经典的三层架构模式,分为客户端、Web层、Service层和Dao层。
在这里插入图片描述
通过三层架构图可以看出,在这个系统中,数据流动遵循着清晰的路径:从客户端发起请求,经过Web层接收并解析请求,通过BeanUtils工具类映射参数后调用Service层处理业务逻辑。Service层需要数据库操作,于是调用Dao层与数据库进行交互执行相应的SQL操作。数据结果再经由Dao层返回给Service层,Service层处理结果并返回给Web层。最终,Web层将响应结果反馈给客户端,完成了整个数据流动和处理的过程。

注:由于功能较多,所以将所有功能的三层架构逻辑统一成一个三层架构图

功能列表

在这里插入图片描述

系统用例图

在这里插入图片描述

系统活动图-用户端

在这里插入图片描述

需求分析

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

课程设计详细设计

实现过程

数据库

drop table if exists address;
drop table if exists cart;
drop table if exists item;
drop table if exists orders;
drop table if exists product;
drop table if exists type;
drop table if exists user;-- 地址表:存储用户的收货地址信息
create table address(a_id     int not null auto_increment comment '地址实体的唯一主键列',u_id     int comment '用户实体的主键属性',a_name   varchar(30) comment '地址的收件人',a_phone  varchar(14) comment '收件人电话',a_detail varchar(200) comment '收货人详细地址',a_state  int comment '是否是默认地址 0 不是 1是默认地址',primary key (a_id)
);-- 购物车表:存储用户购物车中的商品信息
create table cart(c_id    int not null auto_increment comment '购物车的唯一标识',u_id    int comment '用户实体的主键属性',p_id    int comment '商品的唯一主键',c_count decimal(12, 2) comment '购物车小计',c_num   int comment '购物车商品数量',primary key (c_id)
);-- 订单项表:记录订单中的商品详情
create table item(i_id    int not null auto_increment comment '订单项的唯一标识',o_id    varchar(64) comment '订单编号是字符串类型但是也是唯一标识',p_id    int comment '商品的唯一主键',i_count decimal(12, 2) comment '订单项的小计',i_num   int comment '订单项的数量',primary key (i_id)
);-- 订单表:存储用户的订单信息
create table orders(o_id    varchar(64) not null comment '订单编号是字符串类型但是也是唯一标识',u_id    int comment '用户实体的主键属性',a_id    int comment '地址实体的唯一主键列',o_count decimal(12, 2) comment '订单的总金额',o_time  datetime comment '订单的详细时间',o_state int comment '订单状态 0 未付款,1已经付款未发货 2发货待收货 3 收货待评价 4订单完成 5 退货状态',primary key (o_id)
);-- 商品表:存储商品的详细信息
create table product(p_id    int not null auto_increment comment '商品的唯一主键',t_id    int comment '类别的主键id',p_name  varchar(50) comment '商品的名称',p_time  date comment '商品的上市时间',p_image varchar(100) comment '商品图片的路径',p_price decimal(12, 2) comment '商品的价格',p_state int comment '商品的热门指数',p_info  varchar(200) comment '商品的描述',primary key (p_id)
);-- 类别表:存储商品的类别信息
create table type(t_id   int not null auto_increment comment '类别的主键id',t_name varchar(20) comment '类别的名称',t_info varchar(200) comment '类别的描述',primary key (t_id)
);-- 用户表:存储用户信息,包括会员和管理员信息
create table user(u_id       int         not null auto_increment comment '用户实体的主键属性',u_name     varchar(20) not null comment '用户账号',u_password varchar(64) not null comment '用户密码',u_email    varchar(50) not null comment '用户的邮箱!用于激活使用!',u_sex      varchar(4) comment '用户性别!',u_status   int comment '用户的激活状态 0 未激活 1 激活',u_code     varchar(64) comment '邮件激活码',u_role     int comment '用户 0 管理员 1',primary key (u_id)
);

插入数据

INSERT INTO product(t_id, p_name, p_time, p_image, p_price, p_state, p_info)
VALUES (1, "红米4", "1990-01-01", "image/gwc_xiaomi6.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_hongmin4.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_hongmin4x.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_hongmin42.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_xiaomi5.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_xiaomi5c.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_xiaomi5s.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_xiaomi6.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_xiaomimix.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_xiaomint2.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_hongmin4.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (1, "红米4", "1990-01-01", "image/liebiao_hongmin4.jpg", 1999, 4, "红米4手机,主打性价比!吸引年轻"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 5, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 5, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 5, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 5, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 9, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 8, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 6, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 7, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 5, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 4, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 3, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 2, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 4, "红米4手机壳,用心保护你的手机"), (2, "红米4手机壳", "1990-01-01", "image/peijian2.jpg", 20, 1, "红米4手机壳,用心保护你的手机")INSERT INTO type (t_name, t_info)
VALUES('笔记本', '这是笔记本类别的描述'),('平板', '这是平板类别的描述'),('家电', '这是家电类别的描述'),('路由器', '这是路由器类别的描述'),('服务中心', '这是服务中心类别的描述'),('社区', '这是社区类别的描述');

BaseServlet 的实现

创建一个BaseServlet类,让他去继承HttpServlet!在BaseServlet中写service方法!

BaseServlet是一个类,继承自HttpServlet,其中包含了一个service方法。在service方法中,它实现了以下逻辑:首先,它获取请求中的标识符,然后利用反射机制调用相应的业务逻辑。这样设计的好处在于每个模块的Controller只需要继承BaseServlet,无需重复编写service方法,便于代码的组织和管理。另外,BaseServlet还对返回值进行了优化处理,统一了方法的返回值为字符串,并约定了特殊的标识符格式,如“forward:路径”,以便统一处理转发、重定向或返回字符串的情况。在标识符异常情况下,BaseServlet会默认使用index方法进行处理,确保每个Controller都有默认的入口方法。

package com.itqf.controller;import com.itqf.utils.Constants;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;/*** BaseServlet用于集中处理方法的调用!* 以及返回值处理!* 以及默认页对应方法!*/
public class BaseServlet extends HttpServlet {/*** service方法用于处理所有的HTTP请求** @param req HTTP请求对象* @param res HTTP响应对象* @throws ServletException Servlet异常* @throws IOException      IO异常*/@Overridepublic void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {//1.获取请求参数(标识符)String methodStr = req.getParameter(Constants.TAG);//2.参数异常:如果method没有获取到值!则跳转到首页(标识符异常处理)if (methodStr == null && methodStr.equals("")) {methodStr = Constants.INDEX;}//3.反射调用对应的业务逻辑方法//.获取类的class对象Class clazz = this.getClass();//2.获取方法 参数1:方法名 参数2:方法类型try {Method method = clazz.getMethod(methodStr, HttpServletRequest.class, HttpServletResponse.class);//执行方法 参数1:要执行方法的对象 参数2:执行方法传入句的参数 返回值:执行方法的返回值!如果方法为void,返回值为nullObject result = method.invoke(this, req, res);//集中处理返回值响应if (result != null) {//转发 重定向  返回字符String str = (String) result;if (str.startsWith(Constants.FORWARD)) {//转发String path = str.substring(str.indexOf(Constants.FLAG) + 1);req.getRequestDispatcher(path).forward(req, res);} else if (str.startsWith(Constants.REDIRECT)) {//重定向String path = str.substring(str.indexOf(Constants.FLAG) + 1);res.sendRedirect(path);} else {res.getWriter().println(str);}}} catch (Exception e) {//controller 和 service dao 有异常都会到此处!e.printStackTrace();req.getSession().setAttribute("msg", "程序异常!请稍后再试!");res.sendRedirect("/message.jsp");}}/*** 当method标识符‘没有值’ 默认赋 index 访问每个controller的index方法!* 将方法提取到baseservlet中减少代码冗余!* 默认处理:跳转到程序的首页!** @param req  HTTP请求对象* @param resp HTTP响应对象* @return 返回转发到程序首页的路径* @throws IOException      IO异常* @throws ServletException Servlet异常*/public String index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {return Constants.FORWARD + "/index.jsp";}
}

商品显示模块-分页显示所有商品、查看单个商品详细信息

由于代码量较多,只展示三层架构中 DAO层、WEB层、Service层的代码

  1. Dao
package com.itqf.dao.impl;import com.itqf.dao.ProductDao;import com.itqf.entity.Product;import com.itqf.utils.C3P0Utils;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import org.apache.commons.dbutils.handlers.ScalarHandler;import java.sql.SQLException;import java.util.List;public class ProductDaoImpl implements ProductDao {/*** 根据商品类别ID查询该类别商品总数* @param tid 商品类别ID* @return 商品总数* @throws SQLException SQL异常*/@Overridepublic long selectCountByTid(String tid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());// COUNT()是一个SQL聚合函数,用于计算指定列的行数(记录数)String sql = "select count(1) from product where t_id = ?";Object result = queryRunner.query(sql, new ScalarHandler(), tid);Long total = (Long) result;return total;}/*** 分页查询指定类别的商品列表* @param page 当前页码* @param pageSize 每页显示数量* @param tid 商品类别ID* @return 商品列表* @throws SQLException SQL异常*/@Overridepublic List<Product> selectProductByPage(int page, int pageSize, String tid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "select p_id as pid, t_id as tid, p_name as pname, p_time as ptime," +"p_image as pimage, p_state as pstate, p_info as pinfo, p_price as pprice " +"from product where t_id = ? limit ?, ?";List<Product> list = queryRunner.query(sql, new BeanListHandler<>(Product.class),tid, (page - 1) * pageSize, pageSize);return list;}/*** 根据商品ID查询商品详细信息* @param pid 商品ID* @return 商品详细信息* @throws SQLException SQL异常*/@Overridepublic Product selectProductByPid(String pid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "select p_id as pid, t_id as tid, p_name as pname, p_time as ptime," +"p_image as pimage, p_state as pstate, p_info as pinfo, p_price as pprice " +"from product where p_id = ?";Product product = queryRunner.query(sql, new BeanHandler<>(Product.class), pid);return product;}}
  1. Service层
package com.itqf.service.impl;import com.itqf.dao.ProductDao;import com.itqf.dao.impl.ProductDaoImpl;import com.itqf.entity.PageBean;import com.itqf.entity.Product;import com.itqf.service.ProductService;import java.sql.SQLException;import java.util.List;public class ProductServiceImpl implements ProductService {/*** 根据类别ID和页码查询对应商品信息并封装成分页对象* @param tid 商品类别ID* @param page 当前页码* @param pageSize 每页显示数量* @return 分页对象* @throws SQLException SQL异常*/@Overridepublic PageBean<Product> findPage(String tid, int page, int pageSize) throws SQLException {ProductDao productDao = new ProductDaoImpl();long count = productDao.selectCountByTid(tid); // 查询总条数List<Product> list = productDao.selectProductByPage(page, pageSize, tid); // 查询当前页对应的商品return new PageBean<>(list, page, pageSize, count);}/*** 根据商品ID查询商品详细信息* @param pid 商品ID* @return 商品详细信息* @throws SQLException SQL异常*/@Overridepublic Product findProductByPid(String pid) throws SQLException {ProductDao productDao = new ProductDaoImpl();Product product = productDao.selectProductByPid(pid);return product;}}
  1. Web层(对应controller包的内容)
package com.itqf.controller;import com.itqf.entity.PageBean;import com.itqf.entity.Product;import com.itqf.service.ProductService;import com.itqf.service.impl.ProductServiceImpl;import com.itqf.utils.Constants;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.sql.SQLException;/*** 分页商品展示的controller*/@WebServlet("/product")public class ProductController extends BaseServlet {/*** 展示商品列表* @param request HTTP请求对象* @param response HTTP响应对象* @return 页面转发路径* @throws SQLException SQL异常*/public String show(HttpServletRequest request, HttpServletResponse response) throws SQLException {// 1.接收请求参数tid 类别idString tid = request.getParameter("tid");// 页容量int pageSize = 8;// 从前端获取当前页数String currentPage = request.getParameter("currentPage");int page = 1; // 默认第一页if (currentPage != null) {page = Integer.parseInt(currentPage);}// 2.调用业务逻辑得到前端需要展示的PageBeanProductService productService = new ProductServiceImpl();PageBean<Product> pageBean = productService.findPage(tid, page, pageSize);// 3.响应request.setAttribute("pageBean", pageBean);return Constants.FORWARD + "/goodsList.jsp";}/*** 展示商品详情* @param request HTTP请求对象* @param response HTTP响应对象* @return 页面转发路径* @throws SQLException SQL异常*/public String detail(HttpServletRequest request, HttpServletResponse response) throws SQLException {// 1.获取请求参数String pid = request.getParameter("pid");// 2.调用业务逻辑ProductService productService = new ProductServiceImpl();Product product = productService.findProductByPid(pid);// 3.响应request.setAttribute("product", product);return Constants.FORWARD + "/goodsDetail.jsp";}}\end{lstlisting}
  1. 前端页面(对应goodsList.jsp 和goodsDetail.jsp的内容)

用户模块-新用户注册、用户登录、用户退出

  1. DAO层(对应UserDaoImpl.java包的内容)
	package com.itqf.dao.impl;import com.itqf.dao.UserDao;import com.itqf.entity.User;import com.itqf.utils.C3P0Utils;import com.itqf.utils.Constants;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import java.sql.SQLException;/*** 数据库访问实现类*/public class UserDaoImpl implements UserDao {@Overridepublic User selectUserByUname(String username) throws SQLException {//1.创建一个QueryRunner对象 获取一个数据库连接池数据源。C3P0是一个流行的开源数据库连接池QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());//2.执行SQL语句String sql = "select u_id as uid , u_name as username , u_password as upassword" +", u_sex as usex , u_status as ustatus , u_code as code , u_email as email " +", u_role as urole from user where u_name = ?";User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username); //将查询结果映射到一个Java对象中return user;}@Overridepublic int insertUser(User user) throws SQLException {//1.创建一个QueryRunner对象 获取一个数据库连接池数据源。C3P0是一个流行的开源数据库连接池QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());//2.执行SQL语句 插入数据String sql = "insert into user (u_name,u_password,u_sex,u_status," +"u_code,u_email,u_role) value (?,?,?,?,?,?,?)";int rows = queryRunner.update(sql, user.getUsername(), user.getUpassword(), user.getUsex(),user.getUstatus(), user.getCode(), user.getEmail(), user.getUrole());return rows;}@Overridepublic User selectUserByCode(String code) throws SQLException {//1.创建一个QueryRunner对象 获取一个数据库连接池数据源。C3P0是一个流行的开源数据库连接池QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());//2.执行SQL语句String sql = "select u_id as uid , u_name as username , u_password as upassword" +", u_sex as usex , u_status as ustatus , u_code as code , u_email as email " +", u_role as urole from user where u_code = ?";User user = queryRunner.query(sql, new BeanHandler<User>(User.class), code); //将查询结果映射到一个Java对象中return user;}@Overridepublic int updataStatusByid(int uid) throws SQLException {//1.创建一个QueryRunner对象 获取一个数据库连接池数据源。C3P0是一个流行的开源数据库连接池QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "update user set u_status = ? where u_id = ?";int row = queryRunner.update(sql, Constants.USER_ACTIVE, uid);return row;}}
  1. Service层(对应UserDaoImpl.java包的内容)
package com.itqf.service.impl;import com.itqf.dao.UserDao;
import com.itqf.dao.impl.UserDaoImpl;
import com.itqf.entity.User;
import com.itqf.service.UserService;
import com.itqf.utils.Constants;
import com.itqf.utils.EmailUtils;
import com.itqf.utils.MD5Utils;import java.sql.SQLException;public class UserServiceImpl implements UserService {/*** 验证注册用户名是否可用** @param username 被检测的用户名* @return* @throws SQLException*/@Overridepublic boolean checkedUser(String username) throws SQLException {//1.创建dao访问对象UserDao userDao = new UserDaoImpl();//2.执行结果User user = userDao.selectUserByUname(username);//3.处理返回值if (user != null) {return true;}return false;}/*** 用户注册逻辑** @param user* @return* @throws SQLException*/@Overridepublic int registerUser(User user) throws SQLException {//1.用户保存到数据库UserDao userDao = new UserDaoImpl();int row = userDao.insertUser(user);//2.发送一封邮件EmailUtils.sendEmail(user);return row;}/*** 激活** @param code 根据激活码进行激活* @return* @throws SQLException*/@Overridepublic int activeUser(String code) throws SQLException {UserDao userDao = new UserDaoImpl();//1.根据激活码查找用户User user = userDao.selectUserByCode(code);if (user == null) {return Constants.ACTIVE_FAIL;//0激活失败}//2.判断用户是否激活if (user.getUstatus().equals(Constants.USER_ACTIVE)) {return Constants.ACTIVE_ALREADY;}//3.进行激活操作int i = userDao.updataStatusByid(user.getUid());if (i > 0) {return Constants.ACTIVE_SUCCESS;}return Constants.ACTIVE_FAIL;}/*** 登录业务** @param username* @param password* @return*/@Overridepublic User login(String username, String password) throws SQLException {//1.需要密码用md5处理String md5password = MD5Utils.md5(password);//2.根据用户名查找用户UserDao userDao = new UserDaoImpl();User user = userDao.selectUserByUname(username);if (user != null && user.getUpassword().equals(md5password)) {return user;}return null;}
}
  1. Web层(对应UserController.java包的内容)
package com.itqf.controller;import com.itqf.entity.User;
import com.itqf.service.UserService;
import com.itqf.service.impl.UserServiceImpl;
import com.itqf.utils.Base64Utils;
import com.itqf.utils.Constants;
import com.itqf.utils.MD5Utils;
import com.itqf.utils.RandomUtils;
import org.apache.commons.beanutils.BeanUtils;import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;/**
* 用户用户模块的controller
*/
@WebServlet("/user")
public class UserController extends BaseServlet {/*用于检查用户是否存在的方法,根据请求中的用户名,调用 UserService 中的方法检查用户是否存在。*/public String check(HttpServletRequest request, HttpServletResponse response) throws SQLException {//1.获取用户名String username = request.getParameter("username");if (username == null) {return Constants.HAS_USER; //不能注册}// 2.调用业务逻辑判断用户名是否存在UserService userService = new UserServiceImpl();boolean b = userService.checkedUser(username);// 3.响应字符串  存在:1 不存在:0if (b) {//用户存在return Constants.HAS_USER;}return Constants.NOT_HAS_USER;}/*用于处理用户注册请求,获取用户信息并调用 UserService 完成用户注册。*/public String register(HttpServletRequest request, HttpServletResponse response) throws SQLException {//1.获取用户信息Map<String, String[]> parameterMap = request.getParameterMap();User user = new User();try {BeanUtils.populate(user, parameterMap); //映射到user} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}//2.完善用户信息user.setUstatus(Constants.USER_NOT_ACTIVE); //默认未激活状态0,激活1user.setUrole(Constants.ROLE_CUSTOMER); //普通客户0 管理员1user.setCode(RandomUtils.createActive());//激活码// 密码加密,md5进行加密user.setUpassword(MD5Utils.md5(user.getUpassword()));//3.调用用户业务逻辑进行注册UserService userService = new UserServiceImpl();try {userService.registerUser(user);} catch (SQLException e) {e.printStackTrace();request.setAttribute("registerMsg", "注册失败!");return Constants.FORWARD + "/register.jsp";}//4.响应 注册成功跳转激活页面return Constants.FORWARD + "/registerSuccess.jsp";}public String active(HttpServletRequest request, HttpServletResponse response) throws SQLException {//1.获取激活码//已经转成base64String c = request.getParameter("c");System.out.println("c="+c);//base64翻转String code = Base64Utils.decode(c);//2.调用激活的业务逻辑UserService userService = new UserServiceImpl();int i = userService.activeUser(code);//3.响应(激活失败(code没有找到) 已经激活 激活成功)if (i == Constants.ACTIVE_FAIL) {request.setAttribute("msg", "未激活成功!");} else if (i == Constants.ACTIVE_SUCCESS) {request.setAttribute("msg", "激活成功,请登录!");} else {request.setAttribute("msg", "已经激活");}return Constants.FORWARD + "/message.jsp";}/*** 1.前端提交账号密码和验证码* 2.对比验证码 成功 ---》 对比账号密码* 3.对比账号密码* 失败: --》 回到登录页面 进行提示* 成功: --》 未激活  登录页面 进行提示* --》 已激活  程序的首页  将用户放入session共享域*/public String login(HttpServletRequest request, HttpServletResponse response) throws SQLException {//1.获取请求参数(用户名,密码,验证码)String username = request.getParameter("username");String password = request.getParameter("password");String code = request.getParameter("code");//用户输入的验证码String auto = request.getParameter("auto"); //自动登录标识//正确的验证码HttpSession session = request.getSession();String codestr = (String) session.getAttribute("code");//2.判断验证码是否正确// 不考虑大小写比较//        System.out.println("输入:"+code);//        System.out.println("正确"+codestr);//        System.out.println(code == null || !code.equalsIgnoreCase(codestr));if (code == null || !code.equalsIgnoreCase(codestr)) {request.setAttribute("msg", "验证码错误");return Constants.FORWARD + "/login.jsp";}//3.调用业务逻辑判断账户密码UserService userService = new UserServiceImpl();User user = userService.login(username, password);//4.响应//user 等于null证明账号或者密码错误//user 不为null 但是user的状态是未激活状态if (user == null) {request.setAttribute("msg", "账号或者密码错误");return Constants.FORWARD + "/login.jsp";}if (user.getUstatus().equals(Constants.USER_NOT_ACTIVE)) {request.setAttribute("msg", "账号未激活!");return Constants.FORWARD + "/login.jsp";}//把用户放在共享域session中session.setAttribute("loginUser", user);//判断是否勾选自动登录if (auto == null) {//没有勾选//将本地浏览器的存储的cookie清空Cookie cookie = new Cookie(Constants.AUTO_NAME, "");cookie.setPath("/");//将 Cookie 的路径设置为根路径,表示该 Cookie 对于整个网站都是可见的cookie.setMaxAge(0);//最大存活时间设置为 0response.addCookie(cookie);} else {String content = username + Constants.FLAG + password;content = Base64Utils.encode(content);//自动登录数据库存储2周Cookie cookie = new Cookie(Constants.AUTO_NAME, content);cookie.setPath("/");cookie.setMaxAge(14 * 24 * 60 * 60);response.addCookie(cookie);}return Constants.REDIRECT + "/index.jsp";}/*** 注销登录!清空数据!跳转到登录页面** @param request* @param response* @return*/public String logOut(HttpServletRequest request, HttpServletResponse response) {//1.清空session中用户数据HttpSession session = request.getSession();session.removeAttribute("loginUser");//2.清空和覆盖cooki存储的自动登录Cookie cookie = new Cookie(Constants.AUTO_NAME, "");cookie.setPath("/");cookie.setMaxAge(0);response.addCookie(cookie);//3.转发到登录页面request.setAttribute("msg", "注销登录成功");return  Constants.FORWARD + "/login.jsp";}}
  1. 前端页面(对应login.jsp的内容)

支付模块-完成在线支付功能

  1. DAO层(对应OrdersDaoImpl.java的内容)
 public void updateStateByOid(String oid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "update orders set o_state = ? where o_id = ?;";queryRunner.update(sql, 2, oid);}
  1. Service层(对应OrdersServiceImpl.java的内容)
public void updateStateByOid(String oid) throws SQLException {OrdersDao ordersDao = new OrdersDaoImpl();ordersDao.updateStateByOid(oid);}
  1. Web层(对应PayController.java的内容)
package com.itqf.controller;import com.alipay.api.AlipayApiException;import com.alipay.api.AlipayClient;import com.alipay.api.DefaultAlipayClient;import com.alipay.api.domain.AlipayTradePagePayModel;import com.alipay.api.internal.util.AlipaySignature;import com.alipay.api.request.AlipayTradePagePayRequest;import com.itqf.service.OrdersService;import com.itqf.service.impl.OrdersServiceImpl;import com.itqf.utils.AlipayConfig1;import com.itqf.utils.Constants;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.sql.SQLException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;@WebServlet("/pay")public class PayController extends BaseServlet {public void createAiliPayForm(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {//接收订单信息String orderID = request.getParameter("orderId");String orderPrice = request.getParameter("price");String orderBody = request.getParameter("body");if (orderID != null) {// 商户订单号,商户网站订单系统中唯一订单号,必填String out_trade_no = new String(orderID.getBytes("ISO-8859-1"), "UTF-8");// 订单名称,必填String subject = new String(orderBody.getBytes("ISO-8859-1"), "UTF-8");// 付款金额,必填String total_amount = new String(orderPrice.getBytes("ISO-8859-1"), "UTF-8");// 商品描述,可空String body = "";// 超时时间 可空String timeout_express = "8m";// 销售产品码 必填String product_code = "FAST_INSTANT_TRADE_PAY";/**********************/// SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签//调用RSA签名方式AlipayClient client = new DefaultAlipayClient(AlipayConfig1.URL, AlipayConfig1.APPID, AlipayConfig1.RSA_PRIVATE_KEY, AlipayConfig1.FORMAT, AlipayConfig1.CHARSET, AlipayConfig1.ALIPAY_PUBLIC_KEY, AlipayConfig1.SIGNTYPE);AlipayTradePagePayRequest alipay_request = new AlipayTradePagePayRequest();//  封装请求支付信息// AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();AlipayTradePagePayModel model = new AlipayTradePagePayModel();model.setOutTradeNo(out_trade_no);model.setSubject(subject);model.setTotalAmount(total_amount);model.setBody(body);model.setTimeoutExpress(timeout_express);model.setProductCode(product_code);alipay_request.setBizModel(model);// 设置异步通知地址alipay_request.setNotifyUrl(AlipayConfig1.notify_url);// 设置同步地址alipay_request.setReturnUrl(AlipayConfig1.return_url);// form表单生产String form = "";try {// 调用SDK生成表单form = client.pageExecute(alipay_request).getBody();response.setContentType("text/html;charset=" + AlipayConfig1.CHARSET);response.getWriter().write(form);//直接将完整的表单html输出到页面response.getWriter().flush();response.getWriter().close();} catch (AlipayApiException | IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public String AiliPayReturnNotice(HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException, SQLException {PrintWriter out = response.getWriter();// 功能:支付宝服务器同步通知页面//获取支付宝GET过来反馈信息Map<String, String> params = new HashMap<String, String>();Map<String, String[]> requestParams = request.getParameterMap();for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {String name = (String) iter.next();String[] values = (String[]) requestParams.get(name);String valueStr = "";for (int i = 0; i < values.length; i++) {valueStr = (i == values.length - 1) ? valueStr + values[i]: valueStr + values[i] + ",";}//乱码解决,这段代码在出现乱码时使用valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");params.put(name, valueStr);}boolean verify_result = AlipaySignature.rsaCheckV1(params, AlipayConfig1.ALIPAY_PUBLIC_KEY, AlipayConfig1.CHARSET, AlipayConfig1.SIGNTYPE); //调用SDK验证签名String out_trade_no1 = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");OrdersService ordersService1 = new OrdersServiceImpl();ordersService1.updateStateByOid(out_trade_no1);if (!verify_result) {return Constants.FORWARD + "order?method=show";} else {out.println("支付失败");return null;}}}
  1. 支付宝配置文件(对应com/itqf/utils/AlipayConfig1.java的内容)
package com.itqf.utils;public class AlipayConfig1 {// 商户appidpublic static String APPID = "9021033645090";// 私钥 pkcs8格式的public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCZIvxaCWfWs0ZrfoTFCaGiP3w4Y/54TZBjncd5216fBRdyAc1wqP+XcSthm4E0P/6WfEFtC81EU1GBJje0em4m0CNCz985UuMHonGxPKEjH6oQuZT9sWoyqDlBSwqrq5p7CFosDrHJc13ih284kNGTUbnTC8e4q7sVIO2JGHNY1Bd6tStX5QbZwTjfO6crMM8iTYlrX2dIWzuOgXvYzbRn7x2NGg1004tiH1f8Slqa3RxoICKeniWPkvZVFRVFKPHeC+yPSkeudP7AX38kXhpEYg9UNxnL1Y1ZgiZ9ZIjNCQwXnZk9qLeJtJwwxunxYoAt5GnrjkUb48xuOSlrFBJFAgMBAAECggEAIQh7+QwYWyb25M844qwOMsqRCDUBun+4ytRJKE8a3SCjCxO6djfuUoO2AOsjdtrrFxkcn+Iw2DFE3qAY2hSi2DqDsLDYjSwFDyuKbNoQRFxbOhpeOk9gAidpml0OozN0Ml/9240b6sG5GdlvM3uByc6SUCU1Yu+gfpFIzA77KsjXwEDC6tBlMBp/BdF8mtA6Pj6+oC9QkSIvUIkVLDjd1kgMxKQXkmrYx3zlaysSpSzd4IzulaqYO80AFnBxL6dTOze0L3s+5u3jnHs8Wy/esn6PslkPzPtj5XXhkuENVvk57dJzo8oVslt3s7kVa+eTiQTC3Qy/T2OQxAKPauJNLQKBgQDXoMbtYj4yA0nXrQuptuSruqtJaWI+fDsU/9DjZWRMzU07rgo4kiOrXo0nrADbogzLGqmauonqOiTNB5l3GAC1R9I26OMF2TH6UbWkpLhXgtuNb0CGtVWXAwNQd9yWNshqyvbe40kwE4KkKtr0qKcuaMGDCqCnIGT0zYIoqx3JfwKBgQC1zvKVwd+xLE5vHaSn0E9OZ2qtZw23DLkfiU3tcle2LJbH6MnkhaNN30mX6Ubo65p2QScWGrWLT9SfDemUXB8TapXItwTnJHjhXhQwiugqsTR47qx4yyWaXhGfe62ALUkIwKEfwZ1hBwYpzMsSCmb66S9c10Pqbx7dSQuwTcZeOwKBgQCIQzLSun3BTzCv2cwAAPL6zukFaFJMWGdhutN/kAhVPHvtFeuhmkBuQD8Fbf3QzdQ12XCKtg0Hdpzg8Ed76jAwXuWqC3vDq2CY5l/aXrmbm7cw5rgpKJig6KeEh2pfhtkaG47shUcv2XR6WBar+5RCkLKRI/soAiGqTeBN4Oze8wKBgDMUgsman+uaZCsJ7BIOD8oaejlf49WTKIv4usFEx+nyGybte5PODDS70GRy8S5Ny6b/5Shrtqf+dOF/Oiopu71ry8uJyEHc2G2RTiDZViNwiCqV/DUv9k+psE/bosKJdqST4nEWp83z+EJUPJXEAGzsiRcPkBBUyhooBLWzFFGzAoGATM1Qk27OkQ6TYNEqd0Oypm1BE5L1DLFiU5EBXWxeUM1PCOqXSemFjN3X1PIkaZ5ASLM8yqZk+XKHMXjF1T3iL9q76ohj0LGnahTF48H6m76R9Az8vmEztops9XZi9YVRMlgoDTyedHhsbzboHZA8YSykYqteX+F2KFWYpQ="; // 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问public static String notify_url = "http://localhost/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp";// 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址public static String return_url = "http://localhost:8080/pay?method=AiliPayReturnNotice";// 请求网关地址public static String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";// 编码public static String CHARSET = "UTF-8";// 返回格式public static String FORMAT = "json";// 支付宝公钥public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmSL8Wgln1rNGa36ExQmhoj98OGP+eE2QY53HedtenwUXcgHNcKj/l3ErYZuBND/+lnxBbQvNRFNRgSY3tHpuJtAjQs/fOVLjB6JxsTyhIx+qELmU/bFqMqg5QUsKq6uaewhaLA6xyXNd4odvOJDRk1G50wvHuKu7FSDtiRhXerUrV+UG2cE43zunKzDPIk2Ja19nSFs7joF72M20Z+8djRoNdNOLYh9X/Epamt0caCAinp4lj5L2VRUVRSjx3gvsj0pHrnT+wF9/JF4aRGIPVDcZy9WNWYImfWSIzQkMF52ZPai3ibScMMbp8WKALeRp645FG+PMbjkpaxQSRQIDAQAB"; // 日志记录目录public static String log_path = "/log";// RSA2public static String SIGNTYPE = "RSA2";}

购物车模块-加入商品到购物车、修改所购商品数量

  1. DAO层(对应CartDaoImpl.java的内容)
package com.itqf.dao.impl;import com.itqf.dao.CartDao;
import com.itqf.entity.Cart;
import com.itqf.entity.Product;
import com.itqf.utils.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;public class CartDaoImpl implements CartDao {@Overridepublic Cart hasCart(int uid, String pid) throws SQLException, InvocationTargetException, IllegalAccessException {//cart --> product 连接查询 多表查询QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid,p.p_time as ptime,p.p_image as pimage,p_state as pstate,p.p_info as pinfo, p.p_price as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount ,c.c_num as cnum from product p join cart c on p.p_id = c.p_id where c.u_id = ? and c.p_id = ?";Map<String, Object> query = queryRunner.query(sql, new MapHandler(), uid, pid);if (query == null) {return null;}Cart cart = new Cart();Product product = new Product();BeanUtils.populate(cart, query);BeanUtils.populate(product, query);cart.setProduct(product);//        System.out.println("Query result: " + query);//        System.out.println("cart"+cart);//        System.out.println("product"+product);//方法2//        List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(),  uid, pid);//        if (list == null) {//            return null;//        }//        List<Cart> carts = new ArrayList<>();//        Cart cart = new Cart();//        Product product = new Product();//        BeanUtils.populate(cart, list.get(0));//        BeanUtils.populate(product, list.get(0));//        cart.setProduct(product);//        carts.add(cart);//方法3//        product.setPname((String) query.get("pname"));//        product.setPid((Integer) query.get("pid"));//        product.setTid((Integer) query.get("tid"));//        product.setPtime((Date) query.get("ptime"));//        product.setPimage((String) query.get("pimage"));//        product.setPstate((Integer) query.get("pstate"));//        product.setPinfo((String) query.get("pinfo"));//        product.setPprice((BigDecimal) query.get("pprice"));////        cart.setCid((Integer) query.get("cid"));//        cart.setUid((Integer) query.get("uid"));//        cart.setCcount((BigDecimal) query.get("ccount"));//        cart.setCnum((Integer) query.get("cnum"));////        cart.setProduct(product);return cart;}@Overridepublic void updateCart(Cart cart) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "update cart set c_num = ? , c_count = ? where c_id = ?";int n = queryRunner.update(sql, cart.getCnum(), cart.getCcount(), cart.getCid());}@Overridepublic void insertCart(Cart cart) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "insert into cart (u_id,p_id,c_num,c_count) value(?,?,?,?)";int n = queryRunner.update(sql, cart.getUid(), cart.getProduct().getPid(), cart.getCnum(), cart.getCcount());}@Overridepublic List<Cart> selectCartsByUid(int uid) throws SQLException, InvocationTargetException, IllegalAccessException {//注意 查询cart需要关联到商品表//cart --> product 连接查询 多表查询QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid," +"p.p_time as ptime,p.p_image as pimage,p_state as pstate," +"p.p_info as pinfo, p.p_price as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount," +"c.c_num as cnum from product p join cart c on p.p_id = c.p_id where" +" c.u_id = ?;";List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), uid);if (list == null) {return null;}List<Cart> carts = new ArrayList<>();for (Map<String, Object> map : list) {//cart + productCart cart = new Cart();Product product = new Product();BeanUtils.populate(cart, map);BeanUtils.populate(product, map);cart.setProduct(product);carts.add(cart);}return carts;}@Overridepublic void deleteCartByCid(String cid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "delete from cart where c_id = ?;";int update = queryRunner.update(sql, cid);}@Overridepublic void updateByCid(BigDecimal count, BigDecimal cnumbig, String cid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "update cart set c_count = ? , c_num = ? where c_id = ?;";queryRunner.update(sql, count, cnumbig, cid);}@Overridepublic void deleteCartByUid(String uid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "delete from cart where u_id = ?;";queryRunner.update(sql, uid);}}
  1. Service层(对应CartServiceImpl.java的内容)
package com.itqf.service.impl;import com.itqf.dao.CartDao;
import com.itqf.dao.ProductDao;
import com.itqf.dao.impl.CartDaoImpl;
import com.itqf.dao.impl.ProductDaoImpl;
import com.itqf.entity.Cart;
import com.itqf.entity.Product;
import com.itqf.service.CartService;import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;public class CartServiceImpl implements CartService {@Overridepublic void createCart(int uid, String pid) throws SQLException, InvocationTargetException, IllegalAccessException {//1.判断是否已存在CartDao cartDao = new CartDaoImpl();Cart cart = cartDao.hasCart(uid, pid);if (cart != null) {//2.存在 则修改购物车cart.setCnum(cart.getCnum() + 1);cartDao.updateCart(cart);} else {//3.不存在 则添加//根据商品id查询商品ProductDao productDao = new ProductDaoImpl();Product product = productDao.selectProductByPid(pid);cart = new Cart();cart.setCnum(1);cart.setPid(Integer.parseInt(pid));cart.setProduct(product);cart.setUid(uid);cartDao.insertCart(cart);}}@Overridepublic List<Cart> findAll(int uid) throws SQLException, InvocationTargetException, IllegalAccessException {CartDao cartDao = new CartDaoImpl();List<Cart> carts = cartDao.selectCartsByUid(uid);return carts;}@Overridepublic void deleteCart(String cid) throws SQLException {CartDao cartDao = new CartDaoImpl();cartDao.deleteCartByCid(cid);}@Overridepublic void updateCar(String cid, String price, String cnum) throws SQLException {BigDecimal cnumbig = new BigDecimal(cnum);BigDecimal pricebig = new BigDecimal(price);BigDecimal count = pricebig.multiply(cnumbig);CartDao cartDao = new CartDaoImpl();cartDao.updateByCid(count, cnumbig, cid);}@Overridepublic void clearCart(String uid) throws SQLException {CartDao cartDao = new CartDaoImpl();cartDao.deleteCartByUid(uid);}}
  1. Web层(对应CartController.java包的内容)
package com.itqf.controller;import com.itqf.entity.Cart;
import com.itqf.entity.User;
import com.itqf.service.CartService;
import com.itqf.service.impl.CartServiceImpl;
import com.itqf.utils.Constants;import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;@WebServlet("/cart")
public class CartController extends BaseServlet {// 创建购物车public String create(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {// 1.判断是否已经登录HttpSession session = request.getSession();User user = (User) session.getAttribute("loginUser");if (user == null) {session.setAttribute("msg", "添加购物车必须先登录!");return Constants.REDIRECT + "/login.jsp";}// 2.商品的id和用户idint uid = user.getUid();String pid = request.getParameter("pid");CartService cartService = new CartServiceImpl();cartService.createCart(uid, pid);return Constants.FORWARD + "/cartSuccess.jsp";}// 展示购物车public String show(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {// 1.判断是否已经登录HttpSession session = request.getSession();User user = (User) session.getAttribute("loginUser");if (user == null) {session.setAttribute("msg", "添加购物车必须先登录!");return Constants.REDIRECT + "/login.jsp";}// 2.获取参数int uid = user.getUid();// 3.调用业务逻辑进行数据查询CartService cartService = new CartServiceImpl();List<Cart> list = cartService.findAll(uid);request.setAttribute("list", list);return Constants.FORWARD + "/cart.jsp";}// 删除购物车中的商品public String delete(HttpServletRequest request, HttpServletResponse response) throws SQLException {// 1.获取cidString cid = request.getParameter("cid");// 2.调用业务逻辑进行删除CartService cartService = new CartServiceImpl();cartService.deleteCart(cid);// 3.转发到展示的处理方法中return Constants.FORWARD + "/cart?method=show";}// 更新购物车中商品的数量和价格public String update(HttpServletRequest request, HttpServletResponse response) throws SQLException {// 1.获取cidString cid = request.getParameter("cid");String price = request.getParameter("price"); // 商品的单价String cnum = request.getParameter("cnum"); // 修改后的数量// 2.调用业务逻辑进行更新CartService cartService = new CartServiceImpl();cartService.updateCar(cid, price, cnum);// 3.转发到展示的处理方法中return Constants.FORWARD + "/cart?method=show";}// 清空购物车public String clear(HttpServletRequest request, HttpServletResponse response) throws SQLException {// 1.获取uidString uid = request.getParameter("uid");// 2.调用业务逻辑进行清空购物车CartService cartService = new CartServiceImpl();cartService.clearCart(uid);// 3.转发到展示的处理方法中return Constants.FORWARD + "/cart?method=show";}
}
  1. 前端页面(对应cart.jsp 和cartSuccess.jsp的内容)

订单模块-提交订单并显示订单信息、用户确认后生成正式订单、用户查看历史订单

  1. DAO层(对应OrdersDaoImpl.java的内容)
package com.itqf.dao.impl;import com.itqf.dao.OrdersDao;
import com.itqf.entity.Address;
import com.itqf.entity.Item;
import com.itqf.entity.Orders;
import com.itqf.entity.Product;
import com.itqf.utils.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;public class OrdersDaoImpl implements OrdersDao {@Overridepublic void insertOrders(Orders orders) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "insert into orders (o_id,a_id,u_id,o_count,o_state,o_time) " +"value(?,?,?,?,?,?);";queryRunner.update(sql, orders.getOid(), orders.getAid(), orders.getUid(), orders.getOcount(), orders.getOstate(), orders.getOtime());}@Overridepublic void insertItems(List<Item> items) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());Object[][] params = new Object[items.size()][];String sql = "insert into item(o_id,p_id,i_count,i_num) value(?,?,?,?);";for (int i = 0; i < items.size(); i++) {Item item = items.get(i);params[i] = new Object[]{item.getOid(), item.getPid(), item.getIcount(), item.getInum()};}queryRunner.batch(sql, params);}@Overridepublic List<Orders> selectOrdersByUid(int uid) throws InvocationTargetException, IllegalAccessException, SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "select o.o_id as oid,o.u_id as uid,o.a_id as aid," +"o.o_count as ocount,o.o_time as otime,o.o_state as ostate," +"a.a_name as aname,a.a_phone as aphone,a.a_detail as adetail, " +"a.a_state as astate from address a join orders o on a.a_id = o.a_id " +"where o.u_id = ?;";List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), uid);if (list == null) {return null;}List<Orders> ordersList = new ArrayList<>();for (Map<String, Object> map : list) {Orders orders = new Orders();Address address = new Address();BeanUtils.populate(orders, map);BeanUtils.populate(address, map);orders.setAddress(address);ordersList.add(orders);}return ordersList;}@Overridepublic Orders selectOrdersByOid(String oid) throws SQLException, InvocationTargetException, IllegalAccessException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "select o.o_id as oid,o.u_id as uid,o.a_id as aid," +"o.o_count as ocount,o.o_time as otime,o.o_state as ostate," +"a.a_name as aname,a.a_phone as aphone,a.a_detail as adetail, " +"a.a_state as astate from address a join orders o on a.a_id = o.a_id " +"where o.o_id = ?;";Map<String, Object> map = queryRunner.query(sql, new MapHandler(), oid);if (map == null) {return null;}Orders orders = new Orders();Address address = new Address();BeanUtils.populate(orders, map);BeanUtils.populate(address, map);orders.setAddress(address);return orders;}@Overridepublic List<Item> selectItemsByOid(String oid) throws SQLException, InvocationTargetException, IllegalAccessException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());//订单项和商品String sql = "select p.p_id as pid, p.t_id as tid,p.p_name as pname,p.p_time as ptime ," +"p.p_image as pimage,p.p_state as pstate ,p.p_info as pinfo ,p.p_price as pprice," +"i.o_id as oid, i.i_id as iid ,i.i_count as icount,i.i_num as inum " +"from product p join item i on p.p_id = i.p_id  where i.o_id = ?;";List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), oid);if (list == null) {return null;}List<Item> items = new ArrayList<>();for (Map<String, Object> map : list) {Item item = new Item();Product product = new Product();BeanUtils.populate(product, map);BeanUtils.populate(item, map);item.setProduct(product);items.add(item);}return items;}@Overridepublic void updateStateByOid(String oid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "update orders set o_state = ? where o_id = ?;";queryRunner.update(sql, 2, oid);}}
  1. Service层(对应OrdersServiceImpl.java的内容)
package com.itqf.service.impl;import com.itqf.dao.CartDao;
import com.itqf.dao.OrdersDao;
import com.itqf.dao.impl.CartDaoImpl;
import com.itqf.dao.impl.OrdersDaoImpl;
import com.itqf.entity.Cart;
import com.itqf.entity.Item;
import com.itqf.entity.Orders;
import com.itqf.service.OrdersService;
import com.itqf.utils.RandomUtils;import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class OrdersServiceImpl implements OrdersService {@Overridepublic void createOrder(String aid, String uid, String sum) throws SQLException, InvocationTargetException, IllegalAccessException {//1.创建一个订单对象进行保存Orders orders = new Orders();BigDecimal bsum = new BigDecimal(sum);String orderId = RandomUtils.createOrderId();orders.setOid(orderId);orders.setAid(Integer.parseInt(aid));orders.setUid(Integer.parseInt(uid));orders.setOtime(new Date());orders.setOcount(bsum);orders.setOstate(1); //未支付状态//2.保存订单OrdersDao ordersDao = new OrdersDaoImpl();ordersDao.insertOrders(orders);//3.将购物车转成订单项CartDao cartDao = new CartDaoImpl();List<Cart> carts = cartDao.selectCartsByUid(Integer.parseInt(uid));List<Item> items = new ArrayList<>();for (Cart cart : carts) {Item item = new Item();item.setOid(orderId);item.setPid(cart.getPid());item.setInum(cart.getCnum());item.setIcount(cart.getCcount());items.add(item);}//4.保存订单对应的订单项!ordersDao.insertItems(items);//5.清空购物车cartDao.deleteCartByUid(uid);}@Overridepublic List<Orders> findOrders(int uid) throws SQLException, InvocationTargetException, IllegalAccessException {OrdersDao ordersDao = new OrdersDaoImpl();List<Orders> list = ordersDao.selectOrdersByUid(uid);return list;}@Overridepublic Orders findOrder(String oid) throws SQLException, InvocationTargetException, IllegalAccessException {OrdersDao ordersDao = new OrdersDaoImpl();//1.oid查询订单和订单地址信息//订单和地址Orders orders = ordersDao.selectOrdersByOid(oid);//2.oid对应的对应的订单项和商品信息//订单项和商品信息List<Item> items = ordersDao.selectItemsByOid(oid);//3.订单项集合设置给订单对象orders.setItems(items);return orders;}@Overridepublic void updateStateByOid(String oid) throws SQLException {OrdersDao ordersDao = new OrdersDaoImpl();ordersDao.updateStateByOid(oid);}
}
  1. Web层(对应OrdersController.java包的内容)
package com.itqf.controller;import com.google.gson.Gson;
import com.itqf.entity.*;
import com.itqf.service.AddressService;
import com.itqf.service.CartService;
import com.itqf.service.OrdersService;
import com.itqf.service.impl.AddressServiceImpl;
import com.itqf.service.impl.CartServiceImpl;
import com.itqf.service.impl.OrdersServiceImpl;
import com.itqf.utils.Constants;import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;@WebServlet("/order")
public class OrdersController extends BaseServlet {public String preView(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {//1.获取请求参数uidString uid = request.getParameter("uid");//2.调用业务逻辑 (地址,购物车)AddressService addressService = new AddressServiceImpl();List<Address> addressList = addressService.findAddress(Integer.parseInt(uid));CartService cartService = new CartServiceImpl();List<Cart> cartList = cartService.findAll(Integer.parseInt(uid));//3.放入共享域即可request.setAttribute("addressList", addressList);request.setAttribute("cartList", cartList);return Constants.FORWARD + "/order.jsp";}public String create(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {//1.获取请求参数String aid = request.getParameter("aid");String uid = request.getParameter("uid");String sum = request.getParameter("sum");//2.调用业务逻辑生成订单OrdersService ordersService = new OrdersServiceImpl();ordersService.createOrder(aid, uid, sum);//3.转发到订单展示的方法return Constants.FORWARD + "order?method=show";}public String show(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {HttpSession session = request.getSession();User user = (User) session.getAttribute("loginUser");if (user == null) {session.setAttribute("msg", "登录后可以查看订单!");return Constants.REDIRECT + "/login.jsp";}OrdersService ordersService = new OrdersServiceImpl();List<Orders> ordersList = ordersService.findOrders(user.getUid());request.setAttribute("ordersList", ordersList);return Constants.FORWARD + "/orderList.jsp";}public String detail(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {String oid = request.getParameter("oid");OrdersService ordersService = new OrdersServiceImpl();Orders order = ordersService.findOrder(oid);request.setAttribute("order", order);return Constants.FORWARD + "/orderDetail.jsp";}public String success(HttpServletRequest request, HttpServletResponse response) throws SQLException {//1.获取请求参数String oid = request.getParameter("oid");String result = request.getParameter("result");System.out.println("oid" + oid);System.out.println("result" + result);Gson gson = new Gson();WeiXin weiXin = gson.fromJson(result, WeiXin.class);String result_code = weiXin.getResult().getResult_code();if (result_code != null && result_code.equals("SUCCESS")) {//支付成功//修改状态//订单列表页面OrdersService ordersService = new OrdersServiceImpl();ordersService.updateStateByOid(oid);return Constants.FORWARD + "/order?method=show";} else {//支付失败HttpSession session = request.getSession();session.setAttribute("msg", "订单:" + oid + " 支付失败!");//message.jsp页面return Constants.REDIRECT + "/message.jsp";}}}
  1. 前端页面(对应order.jsp、orderList.jsp 和orderDetail.jsp的内容)

收货地址-管理收货地址(增删改查)

  1. DAO层(对应AddressDaoImpl.java的内容)
package com.itqf.dao.impl;import com.itqf.dao.AddressDao;
import com.itqf.entity.Address;
import com.itqf.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;import java.sql.SQLException;
import java.util.List;public class AddressDaoImpl implements AddressDao {@Overridepublic List<Address> selectAddressByUid(int uid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "select a_id as aid, u_id as uid,a_name as aname,a_phone " +"as aphone,a_detail as adetail ,a_state as astate from " +"address where u_id = ? order by a_state desc;";List<Address> list = queryRunner.query(sql, new BeanListHandler<Address>(Address.class), uid);return list;}@Overridepublic void insertAddress(Address address) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "insert into address (u_id,a_name,a_phone,a_detail,a_state) value(?,?,?,?,?)";queryRunner.update(sql, address.getUid(), address.getAname(),address.getAphone(), address.getAdetail(), address.getAstate());}@Overridepublic void deleteAddressByAid(String aid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "delete from address where a_id = ?;";queryRunner.update(sql, aid);}@Overridepublic void updateAddressToDefault(String aid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "update address set a_state = 1 where a_id = ?";queryRunner.update(sql, aid);}@Overridepublic void updateAddressToCommons(String aid, int uid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "update address set a_state = 0 where a_id != ? and u_id = ?";queryRunner.update(sql, aid, uid);}@Overridepublic void updateAddressByAid(Address address) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "update address set a_state = ?,a_name=?,a_phone=?,a_detail = ? where a_id = ?;";queryRunner.update(sql, address.getAstate(), address.getAname(), address.getAphone(), address.getAdetail(), address.getAid());}
}
  1. Service层(对应AddressServiceImpl.java的内容)
package com.itqf.service.impl;import com.itqf.dao.AddressDao;
import com.itqf.dao.impl.AddressDaoImpl;
import com.itqf.entity.Address;
import com.itqf.service.AddressService;import java.sql.SQLException;
import java.util.List;public class AddressServiceImpl implements AddressService {@Overridepublic List<Address> findAddress(int uid) throws SQLException {AddressDao addressDao = new AddressDaoImpl();List<Address> list = addressDao.selectAddressByUid(uid);return list;}@Overridepublic void saveAddress(Address address) throws SQLException {AddressDao addressDao = new AddressDaoImpl();addressDao.insertAddress(address);}@Overridepublic void deleteAddress(String aid) throws SQLException {AddressDao addressDao = new AddressDaoImpl();addressDao.deleteAddressByAid(aid);}@Overridepublic void setAddressToDefault(String aid, int uid) throws SQLException {AddressDao addressDao = new AddressDaoImpl();//1.将aid状态改为1 默认地址addressDao.updateAddressToDefault(aid);//2.将非aid状态改为0 非默认地址addressDao.updateAddressToCommons(aid, uid);}@Overridepublic void updateAddress(Address address) throws SQLException {AddressDao addressDao = new AddressDaoImpl();addressDao.updateAddressByAid(address);}
}
  1. Web层(对应AddressController.java的内容)
package com.itqf.controller;import com.itqf.entity.Address;
import com.itqf.entity.User;
import com.itqf.service.AddressService;
import com.itqf.service.impl.AddressServiceImpl;
import com.itqf.utils.Constants;
import org.apache.commons.beanutils.BeanUtils;import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;@WebServlet("/address")
public class AddressController extends BaseServlet {public String show(HttpServletRequest request, HttpServletResponse response) throws SQLException {HttpSession session = request.getSession();User user  = (User) session.getAttribute("loginUser");if (user == null){session.setAttribute("msg", "需要先登录!");return Constants.REDIRECT + "/login.jsp";}int uid = user.getUid();AddressService addressService = new AddressServiceImpl();List<Address> addresses = addressService.findAddress(uid);request.setAttribute("List", addresses);return Constants.FORWARD + "/self_info.jsp";}public String add(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException {//1.获取请求参数Map<String, String[]> map = request.getParameterMap();Address address = new Address();BeanUtils.populate(address,map);//2.调用业务逻辑进行地址添加AddressService addressService = new AddressServiceImpl();addressService.saveAddress(address);//3。转发到展示的方法return Constants.FORWARD + "/address?method=show";}public String delete(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException {//1.获取请求参数String aid = request.getParameter("aid");//2.调用业务逻辑进行地址添加AddressService addressService = new AddressServiceImpl();addressService.deleteAddress(aid);//3。转发到展示的方法return Constants.FORWARD + "/address?method=show";}public String setDefault(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException {//1.获取请求参数String aid = request.getParameter("aid");HttpSession session = request.getSession();User user  = (User) session.getAttribute("loginUser");if (user == null){session.setAttribute("msg", "需要先登录!");return Constants.REDIRECT + "/login.jsp";}//2.调用业务逻辑进行地址添加AddressService addressService = new AddressServiceImpl();addressService.setAddressToDefault(aid,user.getUid());//3。转发到展示的方法return Constants.FORWARD + "/address?method=show";}public String update(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException {//1.获取请求参数Map<String, String[]> map = request.getParameterMap();Address address = new Address();BeanUtils.populate(address,map);//2.调用业务逻辑进行地址添加AddressService addressService = new AddressServiceImpl();addressService.updateAddress(address);//3。转发到展示的方法return Constants.FORWARD + "/address?method=show";}
}
  1. 前端页面(对应self_info.jsp的内容)

管理模块-增加、删除、修改、查找会员、订单、商品信息

  1. DAO层(对应AdminDaoImpl.java的内容)
package com.itqf.dao.impl;import com.itqf.dao.AdminDao;
import com.itqf.entity.Cart;
import com.itqf.entity.Product;
import com.itqf.entity.User;
import com.itqf.utils.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;public class AdminDaoImpl implements AdminDao {@Overridepublic void updateProduct(Product product) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "INSERT INTO product (t_id, p_name, p_time, p_image, p_price, p_state, p_info) VALUES (?, ?, ?, ?, ?, ?, ?);";queryRunner.update(sql, product.getTid(), product.getPname(), product.getPtime(), product.getPimage(),product.getPprice(), product.getPstate(), product.getPinfo());}@Overridepublic List<Product> selectAllGoods() throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "select p_id as pid,t_id as tid,p_name as pname,p_time as ptime ," +"p_image as pimage,p_state as pstate ,p_info as pinfo ,p_price as pprice " +"from product ;";List<Product> productList = queryRunner.query(sql, new BeanListHandler<>(Product.class));return productList;}@Overridepublic List<User> selectAllUser() throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "select u_id as uid , u_name as username , u_password as upassword" +", u_sex as usex , u_status as ustatus , u_code as code , u_email as email " +", u_role as urole from user;";List<User> userList = queryRunner.query(sql, new BeanListHandler<>(User.class));return userList;}@Overridepublic void deleteuserByuid(String uid) throws SQLException {QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());String sql = "delete from user where u_id = ?;";queryRunner.update(sql, uid);}
}
  1. Service层(对应AdminServiceImpl.java的内容)
package com.itqf.service.impl;import com.itqf.dao.AddressDao;
import com.itqf.dao.AdminDao;
import com.itqf.dao.impl.AddressDaoImpl;
import com.itqf.dao.impl.AdminDaoImpl;
import com.itqf.entity.Orders;
import com.itqf.entity.Product;
import com.itqf.entity.User;
import com.itqf.service.AdminService;import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;public class AdminServiceImpl implements AdminService {@Overridepublic void updateProduct(Product product) throws SQLException {AdminDao addressDao = new AdminDaoImpl();addressDao.updateProduct(product);}@Overridepublic List<Product> getAllGoods() throws SQLException, InvocationTargetException, IllegalAccessException {AdminDao addressDao = new AdminDaoImpl();List<Product> productList = addressDao.selectAllGoods();return productList;}@Overridepublic List<User> getAllUser() throws SQLException {AdminDao addressDao = new AdminDaoImpl();List<User> users = addressDao.selectAllUser();return users;}@Overridepublic void deleteuserByuid(String uid) throws SQLException {AdminDao addressDao = new AdminDaoImpl();addressDao.deleteuserByuid(uid);}}
  1. Web层(对应AdminController.java的内容)
package com.itqf.controller;import com.google.gson.Gson;
import com.itqf.entity.Product;
import com.itqf.entity.User;
import com.itqf.service.AdminService;
import com.itqf.service.UserService;
import com.itqf.service.impl.AdminServiceImpl;
import com.itqf.service.impl.UserServiceImpl;
import com.itqf.utils.Constants;import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;/**
* Admin模块的controller
*/
@MultipartConfig(
maxFileSize = 1024 * 1024 * 5, // 最大文件大小限制为5MB
maxRequestSize = 1024 * 1024 * 10 // 最大请求大小限制为10MB
)
@WebServlet("/admin/admin")
public class AdminController extends BaseServlet {public String login(HttpServletRequest request, HttpServletResponse response) throws SQLException {//1.获取请求参数(用户名,密码,验证码)String username = request.getParameter("username");String password = request.getParameter("password");//3.调用业务逻辑判断账户密码UserService userService = new UserServiceImpl();User adminUser = userService.login(username, password);//4.响应//adminUser 等于null证明账号或者密码错误//adminUser 不为null 但是user的状态是未激活状态if (adminUser == null) {request.setAttribute("tip", "账号或者密码错误");return Constants.FORWARD + "/admin//login.jsp";}if (adminUser.getUstatus().equals(Constants.USER_NOT_ACTIVE)) {request.setAttribute("tip", "账号未激活!");return Constants.FORWARD + "/admin/login.jsp";}//        //把用户放在共享域session中HttpSession session = request.getSession();session.setAttribute("admin", adminUser);return Constants.REDIRECT + "/admin/admin.jsp";}/*** 添加商品** @param request* @param response* @return* @throws InvocationTargetException* @throws IllegalAccessException* @throws SQLException*/public String addGoods(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, SQLException, ParseException, ServletException, IOException {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Product product = new Product();// 获取单个表单字段的值product.setPname(request.getParameter("pname"));product.setTid(Integer.parseInt(request.getParameter("tid")));product.setPtime(sdf.parse(request.getParameter("ptime")));product.setPprice(new BigDecimal(request.getParameter("pprice")));product.setPstate(Integer.parseInt(request.getParameter("pstate")));product.setPinfo(request.getParameter("pinfo"));product.setPimage(request.getParameter("pimage"));System.out.println(product);//2.调用业务逻辑进行地址添加AdminService adminService = new AdminServiceImpl();adminService.updateProduct(product);return Constants.REDIRECT + "/admin/addGoods.jsp";}/*** 注销登录!清空数据!跳转到登录页面** @param request* @param response* @return*/public String logOut(HttpServletRequest request, HttpServletResponse response) {//1.清空session中用户数据HttpSession session = request.getSession();session.removeAttribute("admin");//3.转发到登录页面request.setAttribute("tip", "注销登录成功");return Constants.FORWARD + "/admin/login.jsp";}/*** 查看商品** @param request* @param response* @return*/public void showGoods(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {//2.调用业务逻辑进行地址添加AdminService adminService = new AdminServiceImpl();List<Product> allGoods = adminService.getAllGoods();//把用户放在共享域session中HttpSession session = request.getSession();session.setAttribute("goodsList", allGoods);}/*** 会员信息管理** @param request* @param response* @throws SQLException* @throws InvocationTargetException* @throws IllegalAccessException*/public void userList(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException, IOException {//2.调用业务逻辑进行地址添加AdminService adminService = new AdminServiceImpl();List<User> allGoods = adminService.getAllUser();// 将用户列表转换为JSON格式String json = new Gson().toJson(allGoods);// 将JSON数据写入响应response.setContentType("application/json");response.setCharacterEncoding("UTF-8");response.getWriter().write(json);}public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException, IOException {//1.获取请求参数String uid = request.getParameter("uid");//2.调用业务逻辑进行地址添加AdminService adminService = new AdminServiceImpl();adminService.deleteuserByuid(uid);}}
  1. 前端页面(对应/admin//login.jsp和/admin/admin.jsp的内容)

测试过程

用户模块-新用户注册、用户登录、用户退出

在这里插入图片描述

在这里插入图片描述

商品显示模块-分页显示所有商品、查看单个商品详细信息

在这里插入图片描述
在这里插入图片描述

购物车模块-加入商品到购物车、修改所购商品数量

在这里插入图片描述
在这里插入图片描述

订单模块-提交订单并显示订单信息、用户确认后生成正式订单、用户查看历史订单

在这里插入图片描述
在这里插入图片描述

支付模块-完成在线支付功能

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

收货地址-管理收货地址 (增删改查)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

管理模块-增加、删除、修改、查找会员、订单、商品信息

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

项目代码来源与下载

代码编写参考自博主:千锋教育_JavaWeb毕业设计<商城MyShop>_Java项目案例可上手

在博主的基础上增加了支付宝支付功能与后台管理功能
我的项目代码及报告的下载链接

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/639060.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ranger概述及安装配置

一、前序 希望拥有一个框架,可以管理大多数框架的授权,包括: hdfs的目录读写权限各种大数据框架中的标的权限,列级(字段)权限,甚至行级权限,函数权限(UDF)等相关资源的权限是否能帮忙做书库脱敏Ranger框架应运而生。 二、Ranger 2.1、什么是ranger Apache Ranger…

适合初学者的 机器学习 资料合集(可快速下载)

AI时代已经来临&#xff0c;机器学习成为了当今的热潮。但是&#xff0c;很多人在面对机器学习时却不知道如何开始学习。 今天&#xff0c;我为大家推荐几个适合初学者的机器学习开源项目&#xff0c;帮助大家更好地了解和掌握机器学习的知识。这些项目都是开源的&#xff0c;…

压缩数据处理的艺术:Go语言compress库完全指南

压缩数据处理的艺术:Go语言compress库完全指南 引言compress库概览gzip的使用与示例bzip2的使用与示例flate的使用与示例lzw的使用与示例zlib的使用与示例结语引言 在当今数据驱动的世界里,有效的数据处理变得至关重要。特别是在互联网通信和数据存储领域,数据压缩技术发挥…

Mysql-全局锁、表锁、行锁

本文已收录于专栏 《数据库》 目录 全局锁概述说明开启方式应用场景 表锁概念说明实现方式意向锁 开启方式 行锁概念说明实现方式记录锁&#xff1a;间隙锁临键锁 总结提升 全局锁 概述说明 全局锁是是对整个数据库实例加锁&#xff0c;加锁后整个实例就处于只读状态&#xff…

立体视觉几何 (二)

1.视差 2.立体匹配 立体匹配的基本概念: 匹配目标: 在立体匹配中&#xff0c;主要目标是确定左图像中像素的右图像中的对应像素。这个对应像素通常位于相同的行。视差&#xff08;Disparity&#xff09;: 视差 d 是右图像中对应像素 xr 和左图像中像素 xl 之间的水平位置差。视…

对MODNet 主干网络 MobileNetV2的剪枝探索

目录 1 引言 1.1 MODNet 原理 1.2 MODNet 模型分析 2 MobileNetV2 剪枝 2.1 剪枝过程 2.2 剪枝结果 2.2.1 网络结构 2.2.2 推理时延 2.3 实验结论 3 模型嵌入 3.1 模型保存与加载 法一&#xff1a;保存整个模型 法二&#xff1a;仅保存模型的参数 小试牛刀 小结…

MSPM0L1306例程学习-UART部分(2)

MSPM0L1306例程学习系列 1.背景介绍 写在前边的话&#xff1a; 这个系列比较简单&#xff0c;主要是围绕TI官网给出的SDK例程进行讲解和注释。并没有针对模块的具体使用方法进行描述。所有的例程均来自MSPM0 SDK的安装包&#xff0c;具体可到官网下载并安装: https://www.ti…

YOLOv8改进 | 主干篇 | 低照度图像增强网络SCINet改进黑暗目标检测(全网独家首发)

一、本文介绍 本文给大家带来的改进机制是低照度图像增强网络SCINet,SCINet(自校正照明网络)是一种专为低光照图像增强设计的框架。它通过级联照明学习过程和权重共享机制来处理图像,优化了照明部分以提升图像质量。我将该网络集成在YOLOv8的主干上针对于图像的输入进行增…

【QT+QGIS跨平台编译】之三:【OpenSSL+Qt跨平台编译】(一套代码、一套框架,跨平台编译)

文章目录 一、OpenSSL介绍二、OpenSSL配置三、Window环境下配置四、Linux环境下配置五、Mac环境下配置 一、OpenSSL介绍 OpenSSL是一个开放源代码的软件库包&#xff0c;应用程序可以使用这个包来进行安全通信&#xff0c;避免窃听&#xff0c;同时确认另一端连接者的身份。这…

vectorCast手动添加测试用例配置输入参数和期望值

1.选中函数&#xff0c;点击右键选择插入测试用例。这里所选择的插入测试用例区别于之前的测试用例的地方在于&#xff0c;这里插入测试用例是手动配置的&#xff0c;之前的是自动生成的。手动配置可以自定义选择输入参数和期望值。 2.添加测试用例后&#xff0c;点击测试用例&…

Mybatis 动态SQL删除操作

实现动态删除多个数据,这里我们需要用到 foreach 标签,这个标签还可以运用到批量插入,反正需要对集合进行遍历时就可以使用该标签,标签有如下属性 : 新建了一个 userInfo2Mapper 接口,然后写下如下代码,声明 batchDelete 方法 package com.example.mybatisdemo.mapper; import…

城市道路智慧养护顶层设计建议书

项目通过对国家在推动智慧交通领域的健康发展上发布的如&#xff1a;《交通强国建设纲要》、《推进综合交通运输大数据发展行动纲要(2020-2025年)》、《数字交通发展规划纲要》等相关政策的深入理解与研究&#xff0c;结合“互联网”、“智能化”、“智慧化”等理念,对国家提出…

【算法】利用模拟算法、规律解算法题(C++)

文章目录 1. 前言2. 算法题1576.替换所有的问号495.提莫攻击6.Z字形变换38.外观数列1419.数青蛙 1. 前言 模拟算法 即模拟问题过程来解决问题的算法。 对于一些算法题&#xff0c;我们只需要将题目的过程 用代码编写出来&#xff0c;再结合其他方法&#xff0c;就可以解决。 …

Geogebra绘制分段函数、导数

分段函数绘制 一、下载Geogebra6&#xff0c;输入下面的代码: f(x)x g(x)sin(x) h(x)x^3 p(x)如果(x<0, f(x), x<2, h(x), g(x)) 二、我们会得到下面的图 三、把左边的三个圆圈点击取消掉&#xff0c;右侧就变成了分段函数 导数绘制 f(x)x^3-4x^2x-6 f 效果如下&…

JavaSE复习流程

一.初识JAVA 1.JAVA语言之父--高斯林。 2.javac--字节码文件 3.注释&#xff1a;单行注释&#xff0c;多行注释&#xff0c;文档注释。 二.数据类型与变量 1.数据类型 类型 byte shortintlongfloatdoublecharboolean大小1字节2字节4字节8字节4字节8字节1字节包装类型Byt…

SCTP, TCP, UDP, IP, ICMP都在哪一层?(TCP/IP网络通信协议学习)

TCP/IP网络通信协议最早是由罗伯特卡恩&#xff08;Robert E. Kahn&#xff09;和文顿瑟夫&#xff08;Vinton G. Cerf&#xff09;于1972年提出的&#xff0c;它是一个实际的协议栈。 OSI七层网络通信协议最早是由国际标准化组织&#xff08;ISO&#xff09;于1977年提出的&am…

使用AFPN渐近特征金字塔网络优化YOLOv8改进小目标检测效果(不适合新手)

目录 简单概述 算法概述 优化效果 参考文献 文献地址&#xff1a;paper 废话少说&#xff0c;上demo源码链接&#xff1a; 简单概述 AFPN的核心思想&#xff1a;AFPN主要通过引入渐近的特征融合策略&#xff0c;逐步整合底层、高层和顶层的特征到目标检测过程中。这种融合…

文件上传笔记整理

文件上传 web渗透的核心&#xff0c;内网渗透的基础 通过上传webshell文件到对方的服务器来获得对方服务器的控制权 成功条件 文件成功上传到对方的服务器&#xff08;躲过杀软&#xff09; 知道文件上传的具体路径 上传的文件可以执行成功 文件上传的流程 前端JS对上传文件进行…

Sqoop与Kafka的集成:实时数据导入

将Sqoop与Kafka集成是实现实时数据导入和流处理的关键步骤之一。Sqoop用于将数据从关系型数据库导入到Hadoop生态系统中&#xff0c;而Kafka则用于数据流的传输和处理。本文将深入探讨如何使用Sqoop与Kafka集成&#xff0c;提供详细的步骤、示例代码和最佳实践&#xff0c;以确…

【UEFI基础】EDK网络框架(TCP4)

TCP4 TCP4协议说明 相比UDP4&#xff0c;TCP4是一种面向连接的通信协议&#xff0c;因此有更好的可靠性。 TCP4的首部格式如下&#xff1a; 各个参数说明如下&#xff1a; 字段长度&#xff08;bit&#xff09;含义Source Port16源端口&#xff0c;标识哪个应用程序发送。D…