购物车系统(内附完整代码)

本文详细描述了如何开发一个便利店系统,包括创建数据库表结构(用户、商品分类、商品、购物车)、编写登录验证逻辑以及展示商品、添加商品到购物车和计算购物车总价的功能。

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

效果图在最下面😊😊😊😊😊😊😊😊

关注一下叭❤❤❤❤

便利店系统,主页面如下:

😊😊😊😊😊😊😊😊😊😊😊😊😊😊❤❤❤❤😊😊😊😊😊😊😊😊😊😊😊😊😊😊 

1、创建数据库和表:

user 用户表(用户id,账号,密码,金额)

category 商品分类表(商品分类id,分类名称)

product 商品表(商品id,商品名称,商品价格,库存,商品图片,过期时间,商品分类外键id)

gouwu 购物车表(购物车id,商品id,商品数量,用户id)

😊😊😊😊😊😊😊😊😊😊😊😊😊😊❤❤❤❤😊😊😊😊😊😊😊😊😊😊😊😊😊😊 

2、编写登录

如果登录的用户不存在则提示不存在,否则保存在域中,在前台打印当前登陆人和余额

后端servlet
    String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user= productService.login(username,password);
        if(user!=null){
            request.getSession().setAttribute("user",user);
            response.sendRedirect("ShowAllServlet");
        }else{
            request.setAttribute("error","错误");
            request.getRequestDispatcher("login.jsp").forward(request,response);
    }
sql语句部分
   public User login(String username, String password) {
        String sql="select * from user where username=? and password=?";
        User query = null;
        try {
            query = runner.query(sql, new BeanHandler<>(User.class),username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

😊😊😊😊😊😊😊😊😊😊😊😊😊😊❤❤❤❤😊😊😊😊😊😊😊😊😊😊😊😊😊😊  

3、展示所有

后端servlet
        String cid = request.getParameter("cid");
        String name = request.getParameter("name");
        //通过三层架构进行查询,返回的是一个list集合
        List<Product> productList=productService.findAll(cid,name);
        //将查到的数据存在域里面
        request.setAttribute("productList",productList);
        request.setAttribute("name",name);
        request.setAttribute("cid",cid);
        //通过三层架构进行查询,返回的是一个list集合
        List<Category> categoryList=productService.findCate();
        request.getSession().setAttribute("categoryList",categoryList);
        //转发到jsp页面
        request.getRequestDispatcher("show.jsp").forward(request,response);
sql语句部分
public List<Product> findAll(String cid, String name) {
        String sql="select product.*,category.name cname from product join category on product.cid = category.id where 1=1 ";
        if(name!=null && !name.equals("")){
            sql+=" and product.name like '%"+name+"%'";
        }
        if(cid!=null && !cid.equals("")){
            sql+=" and category.id ="+cid;
        }
        List<Product> query = null;
        try {
            query = runner.query(sql, new BeanListHandler<>(Product.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

 public List<Category> findCate() {
        String sql="select * from category ";
        List<Category> query = null;
        try {
            query = runner.query(sql, new BeanListHandler<>(Category.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

😊😊😊😊😊😊😊😊😊😊😊😊😊😊❤❤❤❤😊😊😊😊😊😊😊😊😊😊😊😊😊😊  

4、添加功能

后端servlet:
         String id = request.getParameter("id");
        GouWu gouWu=productService.chongadd(id);
        if(gouWu!=null){
            productService.updateNumber(id);
        }else{
            User user = (User) request.getSession().getAttribute("user");
            productService.addgouwu(id,user);
        }
        //重定向到servlet
        response.sendRedirect("ShowAllServlet");
sql语句部分
public GouWu chongadd(String id) {
        String sql="select * from gouwu where id=?";
        GouWu query = null;
        try {
            query = runner.query(sql, new BeanHandler<>(GouWu.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
 public void updateNumber(String id) {
        String sql="update gouwu set number=number+1 where id=?";
        try {
            runner.update(sql,id);
            String sql3="update product set sumkc=sumkc-1 where id=?";
            runner.update(sql3,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

public void addgouwu(String id,User user) {
        try {
            String sql="select product.*,category.name cname from product join category on product.cid = category.id where product.id=?";
            Product product = runner.query(sql, new BeanHandler<>(Product.class), id);
            String sql2="insert into gouwu value(null,?,?,?)";
            runner.update(sql2,product.getId(),1,user.getId());
            String sql3="update product set sumkc=sumkc-1 where id=?";
            runner.update(sql3,id);

        } catch (Exception e) {
            e.printStackTrace();
        }
}

解释:

①点击前台添加购物车按钮之后会把这一条商品信息添加到购物车中,所以需要获取当前的商品id

②添加到购物车之后那当前商品库存就要-1

③如果添加到购物车中的商品是同一个就在基础上+1,若多加一条信息则功能未实现

😊😊😊😊😊😊😊😊😊😊😊😊😊😊❤❤❤❤😊😊😊😊😊😊😊😊😊😊😊😊😊😊  

5、批量添加功能

后端servlet
        String ids = request.getParameter("ids");
        String[] split = ids.split(",");
        for (String id : split) {
            GouWu gouWu=productService.chongadd(id);
            //        System.out.println(gouWu);
            if(gouWu!=null){
                productService.updateNumber(id);
            }else{
                User user = (User) request.getSession().getAttribute("user");
                productService.addgouwu(id,user);
            }
        }

sql语句部分

和添加功能所调用的方法一样,只有servlet层做了改动

😊😊😊😊😊😊😊😊😊😊😊😊😊😊❤❤❤❤😊😊😊😊😊😊😊😊😊😊😊😊😊😊  

6、购物车页面展示

后端servlet

        //通过三层架构进行查询,返回的是一个list集合
        List<GouWuPro> gouWuList=productService.chaxun();
        request.setAttribute("gouWuList",gouWuList);
        //通过三层架构进行查询,返回的是一个对象
        GouWu gouWu=productService.findMoney();
        //将查到的数据存在域里面
        request.setAttribute("gouwu",gouWu);
        int sum=0;
        double sumPrice=0;
        for (GouWuPro gouWuPro : gouWuList) {
            sum+=gouWuPro.getNumber();
            sumPrice+=gouWuPro.getNumber()*gouWuPro.getPrice();
        }

        User user = (User) request.getSession().getAttribute("user");
        double yue=user.getMoney();
        yue-=sumPrice;
        //将查到的数据存在域里面
        request.setAttribute("sum",sum);
        //将查到的数据存在域里面
        request.setAttribute("sumPrice",sumPrice);
        //将查到的数据存在域里面
        request.setAttribute("yue",yue);
        //转发到jsp页面
        request.getRequestDispatcher("showgouwu.jsp").forward(request,response);

sql语句部分

public List<GouWuPro> chaxun() {
        List<GouWuPro> query = null;
        try {
           Product product=new Product();
            GouWu gouWu=new GouWu();
            if(gouWu.getPid()==product.getId()){
                String sql="select product.*,gouwu.id gid,pid,number,uid,category.name cname from gouwu join product on pid=product.id join category on product.cid = category.id ";
                query = runner.query(sql, new BeanListHandler<>(GouWuPro.class));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
public GouWu findMoney() {
        String sql="select * from gouwu ";
        GouWu query = null;
        try {
            query = runner.query(sql, new BeanHandler<>(GouWu.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

 注重后端开发,前端无美化

        可以私我要源码!!!

😊😊😊😊😊😊😊😊😊😊😊😊😊😊❤❤❤❤😊😊😊😊😊😊😊😊😊😊😊😊😊😊 

最终效果如下:

便利店

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橘猫_A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值