javaweb校园订餐系统(javaEE+mysql)

系统主要内容:基于SSM框架实现一个校园点餐系统,包括用户端和管理员端; 前台主要功能有用户注册, 用户登录, 我的购物车、我的订单、商品评论、校园资讯等; 管理员端主要功能有:用户管理、商品管理、订单管理、评论管理、资讯管理等。

技术栈:jdk8+tomcat8+mysql5.6+Eclipse+maven+spring+spring mvc+mybatis+layui+jsp+jquery

系统展示:

 

 

 

 代码展示:

package com.byh.biyesheji.controller;

import com.byh.biyesheji.pojo.Category;
import com.byh.biyesheji.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 商品分类模块controller
 */
@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/list")
    public String list(Model model){
        List<Category> list = categoryService.list();
        model.addAttribute("list",list);
        model.addAttribute("size",list.size());
        return "productmodule/category-list";
    }

    @RequestMapping("/addCategory")
    public String add(@RequestParam(value = "name")String name){
        Category category = new Category();
        category.setName(name);
        categoryService.save(category);
        return "productmodule/category-list";
    }

    @RequestMapping("/delCategory")
    public String del(@RequestParam(value = "id")int id){
        categoryService.del(id);
        return "redirect:list";
    }

    @RequestMapping("/editCategory")
    public String edit(@RequestParam(value = "id")int id,Model model){
        Category category = categoryService.get(id);
        model.addAttribute("category",category);
        return "productmodule/category-edit";
    }

    @RequestMapping("/updateCategory")
    public String update(Category category,Model model){
        categoryService.update(category);
        return "redirect:list";
    }
}

 

package com.byh.biyesheji.controller;

import com.byh.biyesheji.pojo.Customer;
import com.byh.biyesheji.pojo.Product;
import com.byh.biyesheji.service.CustomerService;
import com.byh.biyesheji.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户模块controller
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @RequestMapping("/list")
    public String list(Model model, Page page)
    {
        PageHelper.offsetPage(page.getStart(),page.getCount());//分页查询
        List<Customer> list= customerService.list();
        int total = (int) new PageInfo<>(list).getTotal();//总条数
        page.setTotal(total);
        model.addAttribute("list",list);
        model.addAttribute("totals",total);
        return "cstpage/cst-list";
    }
    /**
     * 设置会员
     * @param id
     * @return
     */
    @RequestMapping("/shezhihuiyuan")
    @ResponseBody
    public String shezhihuiyuan(int id){
        customerService.shezhihuiyuan(id);
        return "success";
    }

    @RequestMapping("/del")
    public String del(int id)
    {
        customerService.del(id);
        return "redirect:list";
    }
}
package com.byh.biyesheji.controller;

import com.byh.biyesheji.pojo.*;
import com.byh.biyesheji.service.*;
import com.byh.biyesheji.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 前台所有请求controller
 */
@Controller
@RequestMapping("/fore")
public class ForeController {

    @Autowired
    private ForeService foreService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ReviewService reviewService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ZiXunService ziXunService;

    public String PNAME=null;

    /**
     * 前台首页
     * @param model
     * @return
     */
    @RequestMapping("/foreIndex")
    public String index(Model model,HttpSession session){

        //传入3个分类
        List<Category> categories = foreService.listToThree();
        List<Category> categories1 = categoryService.list();
        //给每个分类设置商品
        for (Category c:categories){
            List<Product> products = productService.getProductsByCid(c.getId());
            //如果分类下的商品超过4个,则只显示4个给前端
            if(products.size()>5){
                List<Product> products1 = new ArrayList<>();
                for(int i=0;i<=4;i++){
                    products1.add(products.get(i));
                }
                c.setProducts(products1);
            }else{
                c.setProducts(products);
            }
        }
        model.addAttribute("categories",categories);
        session.setAttribute("categories",categories1); //保存在session  使其他页面也能获取到分类列表 而不用每次都去查询
        return "forepage/index2";
    }


    /**
     * 商品详情跳转
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/foreDetailUI")
    public String detailUI(@RequestParam(value = "id")int id,Model model){
        Product product = productService.get(id);
        if(product==null) return "forepage/noPro";

        User user = productService.getUserByBid(product.getBid());
        Category category = productService.getCategoryByCid(product.getCid());
        product.setCategory(category);
        product.setUser(user);

        List<Product> fivePro = foreService.getFivePro();

        model.addAttribute("product",product);
        model.addAttribute("fivePro",fivePro);

        List<Review> list = reviewService.getReviewListByPid(id);
        model.addAttribute("reviews",list);
        model.addAttribute("rs",list.size());

        return "forepage/proDetail";
    }

    /**
     * 注册页面
     * @return
     */
    @RequestMapping("/foreRegisterUI")
    public String registerUI(){
        return "forepage/foreRegister";
    }

    /**
     * 登陆页面
     * @return
     */
    @RequestMapping("/foreLoginUI")
    public String foreLoginUI(){
        return "forepage/forelogin";
    }

    /**
     * 模态窗口登陆 验证
     * @param customer
     * @param session
     * @return
     */
    @RequestMapping("/foreMtLogin")
    @ResponseBody
    public String foreIsLogin(Customer customer,HttpSession session){
        Customer cst = customerService.foreLogin(customer);
        if(null==cst){
            return "false";
        }
        session.setAttribute("cst", cst);
        return "true";
    }

    /**
     * ajax判断客户是否登陆
     * @param session
     * @return
     */
    @RequestMapping("/foreIsLogin")
    @ResponseBody
    public String isLogin(HttpSession session){
        Customer cst = (Customer) session.getAttribute("cst");
        return cst==null?"false":"true";
    }

    /**
     * 注册
     * @param customer
     * @return
     */
    @RequestMapping("/foreRegister")
    public String register(Customer customer){
        customer.setStatus(0);
        customerService.save(customer);
        return "forepage/registerSuccess";
    }

    /**
     * 客户登陆
     * @param customer
     * @param session
     * @return
     */
    @RequestMapping("/foreLogin")
    public String foreLogin(Customer customer, HttpSession session,Model model){
        Customer cst = customerService.foreLogin(customer);
        if (cst!=null){
            session.setAttribute("cst",cst);
            return "redirect:foreIndex";
        }else {
            return "redirect:foreLoginMsg";
        }
    }

    /**
     * 用户登陆返回信息
     * @return
     */
    @RequestMapping("/foreLoginMsg")
    public String foreLoginMsg(HttpServletRequest request){
        request.setAttribute("msg","true");
        return "forepage/forelogin";
    }

    /**
     * 客户注销
     * @param session
     * @return
     */
    @RequestMapping("/foreCstLoginOut")
    public String cstLoginOut(HttpSession session){
        session.setAttribute("cst",null);
        return "redirect:foreIndex";
    }

    /**
     * 立即购买
     * @param session
     * @param pid  商品id
     * @param number  商品数量
     * @return  重定向到支付 , 传入订单项id
     */
    @RequestMapping("/forebuyone")
    public String forebuyone(HttpSession session,int pid,int number,float totalPrice){
        Customer cst = (Customer) session.getAttribute("cst");
        Product product = productService.get(pid);

        int oiid = 0;

        boolean find = false;
        List<OrderItem> orderItems = orderItemService.listByCustomer(cst.getId());//获得订单项表中该用户的所有订单id为空的订单项
        for (OrderItem oi : orderItems) {
            //基于用户对象customer,查询没有生成订单的订单项集合
            // 如果产品是一样的话,就进行数量追加
            if(oi.getProduct().getId().intValue()==product.getId().intValue()){
                //如果已经存在这个产品对应的OrderItem,并且还没有生成订单,即还在购物车中。 那么就应该在对应的OrderItem基础上,调整数量
                oi.setNumber(oi.getNumber()+number);
                orderItemService.update(oi);
                find = true;
                //获取这个订单项的 id
                oiid = oi.getId();
                break;
            }
        }
        //如果不存在对应的OrderItem,那么就新增一个订单项OrderItem
        if(!find){
            OrderItem oi = new OrderItem();
            oi.setCstid(cst.getId());
            oi.setNumber(number);
            oi.setPid(pid);
            orderItemService.save(oi);
            //获取这个刚添加的订单项的 id
            oiid = oi.getId();
        }

        return "redirect:forebuy?oiid="+oiid;
    }

    /**
     * 立即购买、购物车提交到订单页面调用  根据oiid计算订单项的总价、购买数量 , 订单项放session
     * 订单-支付  上一次的购物信息会被下次单个挤掉  根据oiid获得订单项
     * @param model
     * @param oiid 立即购买生成的订单项id
     * @param session
     * @return 返回订单项集合   |   返回所有订单项加起来的总价
     */
    @RequestMapping("/forebuy")
    public String forebuy(Model model,String[] oiid,HttpSession session){
        System.out.println(oiid);

        List<OrderItem> ois = new ArrayList<>();

        Customer cst = (Customer)session.getAttribute("cst");

        float total = 0;
        int number = 0;
        for (String strid : oiid) {
            int id = Integer.parseInt(strid);
            OrderItem oi= orderItemService.get(id);
            if (cst.getStatus()==1){
                total +=oi.getProduct().getPrice()*0.8*oi.getNumber();
            }else{
                total +=oi.getProduct().getPrice()*oi.getNumber();
            }
            number += oi.getNumber();
            ois.add(oi);
        }
        /*
          累计这些ois的价格总数,赋值在total上
          把订单项集合放在session的属性 "ois" 上,方便下订单时候直接获取
          把总价格放在 model的属性 "total" 上
          服务端跳转到buy.jsp
          */
        session.setAttribute("ois", ois);
        model.addAttribute("total", total);
        model.addAttribute("number", number);

        return "forepage/foreBuy";
    }

    /**
     * 添加购物车
     * @param pid  商品id
     * @param number  购买数量
     * @param model
     * @param session
     * @return  boolean
     */
    @RequestMapping("/foreAddCart")
    @ResponseBody
    public String addCart(int pid, int number, Model model,float totalPrice,HttpSession session) {
        Customer customer =(Customer)  session.getAttribute("cst");
        if(customer==null){
            return "false";
        }
        Product p = productService.get(pid);

        boolean found = false;
        //获得订单项表中该用户的所有订单id为空的订单项
        List<OrderItem> ois = orderItemService.listByCustomer(customer.getId());
        for (OrderItem oi : ois) {
            //基于用户对象customer,查询没有生成订单的订单项集合
            // 如果产品是一样的话,就进行数量追加
            if(oi.getProduct().getId().intValue()==p.getId().intValue()){
                //如果已经存在这个产品对应的OrderItem,并且还没有生成订单,即还在购物车中。 那么就应该在对应的OrderItem基础上,调整数量
                oi.setNumber(oi.getNumber()+number);
                orderItemService.update(oi);
                found = true;
                break;
            }
        }
        //如果不存在对应的OrderItem,那么就新增一个订单项OrderItem
        if(!found){
            OrderItem oi = new OrderItem();
            oi.setCstid(customer.getId());
            oi.setNumber(number);
            oi.setPid(pid);
            orderItemService.save(oi);
        }

        return "success";
    }

    /**
     * 查看购物车购物车
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/forecart")
    public String cart( Model model,HttpSession session) {
        Customer customer =(Customer)  session.getAttribute("cst");
        //cstid等于当前登陆用户id 并且oid为null的订单项
        List<OrderItem> ois = orderItemService.listByCustomer(customer.getId());
        //购物车没有商品
        if(ois==null || ois.size()==0){
            return "forepage/cart_noPro";
        }
        int totalProductNumber = 0;
        for (OrderItem oi:ois){
            totalProductNumber += oi.getNumber();
        }
        model.addAttribute("ois", ois);
        model.addAttribute("size", totalProductNumber);

        return "forepage/foreCart";
    }

    /**
     * 删除订单项
     * @param oiid 订单项id
     * @param session
     * @return
     */
    @RequestMapping("/foreDelOrderItem")
    @ResponseBody
    public String foreDelOrderItem(int oiid,HttpSession session){
        Customer customer = (Customer) session.getAttribute("cst");
        if(customer==null){
            return "noSuccess";
        }
        orderItemService.del(oiid);
        return "success";
    }

    /*
      点击提交订单
    1. 从session中获取cst对象
    2. 通过参数Order接受收货人
    3. 根据当前时间加上一个4位随机数生成订单号
    4. 根据上述参数,创建订单对象
    5. 把订单状态设置为未支付
    6. 从session中获取订单项集合 ( 在结算功能的ForeController.buy() 13行,订单项集合被放到了session中 )
    7. 把订单加入到数据库,并且遍历订单项集合,设置每个订单项的order,更新到数据库
    8. 统计本次订单的总金额
    9. 客户端跳转到确认支付页forePayed,并带上订单id和总金额
     */
    @RequestMapping("/foreCreateOrder")
    public String createOrder( Model model,String address,HttpSession session){
        /*
          提交订单后,设置code,客户id,支付状态,地址
         */
        Order order = new Order();
        Customer customer =(Customer)  session.getAttribute("cst");
        String orderCode = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomUtils.nextInt(10000);
        order.setCode(orderCode);
        order.setAddress(address);
        order.setCstid(customer.getId());
        order.setStatus(0);//未支付

        List<OrderItem> ois= (List<OrderItem>)  session.getAttribute("ois");
        //给每个订单项设置订单id  并且算出订单总价
        float total =orderService.add(order,ois);
        return "redirect:forePayed?oid="+order.getId() +"&total="+total;
    }

    /**
     * 支付成功跳转
     * @param oid 订单id
     * @param total 总价
     * @param model
     * @return
     */
    @RequestMapping("/forePayed")
    public String payed(int oid, float total, Model model) {
        Order order = orderService.get(oid);
        order.setStatus(1);
        orderService.update(order);
        model.addAttribute("total", total);

        return "forepage/forePayed";
    }

    /**
     * 我的订单  根据session查看当前用户的订单
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/forebought")
    public String forebought(Model model,HttpSession session){
        Customer customer = (Customer) session.getAttribute("cst");
        List<Order> os= orderService.list(customer.getId());

        //给每个订单的订单项设置属性值,如orderitem、product
        orderItemService.fill(os);

        model.addAttribute("os", os);
        return "forepage/foreBought";
    }

    /**
     * 搜索商品
     * @param model
     * @param pName
     * @return
     */
    @RequestMapping("/foreNameLike")
    public String foreNameLike(Model model, String pName,Page page){
        PageHelper.offsetPage(page.getStart(),page.getCount());//分页查询
        if(pName!=null) PNAME = pName;
        List<Product> products = productService.findByName(PNAME);
        int total = (int) new PageInfo<>(products).getTotal();//总条数
        page.setTotal(total);

        model.addAttribute("products",products);
        model.addAttribute("total",total);
        model.addAttribute("page", page);

        model.addAttribute("proSize",products.size());

        return "forepage/proSeach";
    }

    /**
     * 显示分类下的商品
     * @param model
     * @param cid
     * @return
     */
    @RequestMapping("/foreFindCategory")
    public String foreFindCategory(Model model,@RequestParam(value = "id") int cid){
        List<Product> ps = productService.findByCid(cid);
        Category category = categoryService.get(cid);
        if(ps.size()>8){
            List<Product> ps1 = new ArrayList<>();
            for(int i=0;i<8;i++){
                ps1.add(ps.get(i));
            }
            model.addAttribute("products",ps1);
            model.addAttribute("category",category);
            return "forepage/proCategorySeach";
        }
        model.addAttribute("products",ps);
        model.addAttribute("proSize",ps.size());
        model.addAttribute("category",category);

        return "forepage/proCategorySeach";
    }

    @RequestMapping("/faq")
    public String faq(){
        return "forepage/faq";
    }

    /**
     * 商品评价
     * @param pid
     * @param model
     * @return
     */
    @RequestMapping("/forePingjia")
    public String forePingjia(int pid,Model model){

        return "forePage/pingjia";
    }

    /**
     * 商品评论
     * @param session
     * @param pid
     * @param content
     * @return
     */
    @RequestMapping("/cstPinglun")
    @ResponseBody
    public String cstPinglun(HttpSession session,int pid,String content){
        Customer cst = (Customer) session.getAttribute("cst");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        //string转date
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = sdf.parse(format, pos);

        Review review = new Review();
        review.setCstid(cst.getId());
        review.setCustomer(cst);
        review.setPid(pid);
        review.setProduct(productService.get(pid));
        review.setContent(content);
        review.setCreatetime(strtodate);

        reviewService.save(review);

        return "success";
    }

    /**
     * 已审核的资讯
     * @param model
     * @return
     */
    @RequestMapping("/foreZixuns")
    public String zixun(Model model){
        List<ZiXun> list = ziXunService.list();
        model.addAttribute("list",list);
        return "forepage/foreZixun";
    }

    @RequestMapping("/foreZixunadd")
    @ResponseBody
    public String zixunadd(String content,HttpSession session){
        Customer c = (Customer) session.getAttribute("cst");
        ZiXun z = new ZiXun();
        z.setCstid(c.getId());
        z.setContent(content);
        z.setFabudate(new Date());
        z.setStatus(0);
        ziXunService.save(z);
        return "success";
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
购物车系统是一个常见的电子商务功能,能够为用户提供方便快捷的购物体验。在JavaWeb开发中,我们可以利用Servlet、MySQL、JSP和Tomcat来实现购物车系统。 首先,我们需要使用MySQL数据库来存储商品信息、用户信息和订单信息等。可以创建相应的数据库表来存储这些数据,并通过Java程序连接数据库进行增删改查操作。 接下来,在Servlet中编写相应的逻辑来处理用户的购物车操作。当用户点击添加商品到购物车按钮时,可以将商品的信息存储到Session中,这样就可以在用户的不同请求之间保持购物车的信息不丢失。同时,可以编写其他Servlet来处理购物车中的商品数量修改、商品删除等操作。 在JSP中,可以使用JSTL标签库来展示用户的购物车信息。通过循环遍历Session中的商品列表,可以将商品名称、价格、数量等信息展示出来,并提供修改和删除商品的链接。 最后,利用Tomcat作为服务器来部署我们的Web应用程序。将编写好的Servlet和JSP文件打包成一个war包,然后将其部署到Tomcat服务器上即可。用户可以通过浏览器访问我们的网站,添加商品到购物车,并进行相关的操作。 除了基本的购物车功能,我们还可以进一步扩展购物车系统,例如实现用户登录注册功能、生成订单、进行支付等。通过不断改进和迭代,可以让购物车系统变得更加完善和实用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值