2021年最后几天,使用SSM实现网上购物商城系统

Posted 编程指南针

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021年最后几天,使用SSM实现网上购物商城系统相关的知识,希望对你有一定的参考价值。

项目编号:BS-SC-007

本项目基于SSM框架( spring+springmvc+mybatis)进行开发实现,数据库采用mysql,开发工具为IDEA或ECLIPSE均可。系统包含商城前端功能和后端管理功能,前端主要实现了用户注册、登陆、商品查看、添加购物车、生成订单、模拟支付等功能。后台管理功能主要包含用户管理、产品类型管理、产品管理、订单管理、销量统计等功能。功能完整,运行无误,适合做毕业设计或课程设计使用。

部分功能展示如下:

前台首页:

商品详情:

购物车:

订单支付

我的订单

后台管理功能界面:

商品管理

商品管理

订单管理

销量统计

以上是展示的系统的部分功能,系统源码+数据库+文档

package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.beans.*;
import com.qst.service.impl.*;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/admin")
public class AdminController 

    @Autowired
    private AdminUserServiceImpl adminUserService;

    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private CategorySecondServiceImpl secondService;

    @Autowired
    private ProductServiceImpl productService;

    @Autowired
    private OrderServiceImpl orderService;

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/adminIndex")
    public String adminHome()//到后台登录页
        return "admin/index";
    

    @RequestMapping("/adminLogin")
    public String adminLogin(@Param("username") String username, @Param("password") String password, HttpServletRequest request)
        AdminUser adminUser = new AdminUser();
        adminUser.setUsername(username);
        adminUser.setPassword(password);

        AdminUser adminBynamePwd = adminUserService.getAdminBynamePwd(adminUser);
        System.out.println(adminBynamePwd==null?"查询到的用户信息为空":adminBynamePwd);
        /*登录成功就到管理页,否则就回到登录页*/
        if (adminBynamePwd!=null) 
            request.getSession().setAttribute("enterAdmin",adminBynamePwd);
            return "/admin/home";
        
        return "/admin/index";
    

    /*------------------以下是管理员对用户的操作*/
    @RequestMapping(value = ("/adminUser_findAllByPage") )
    public String findAllUserByPage(@Param("page") Integer page,HttpServletRequest request)//查找商品并分页
        PageUtils<User> userPageUtils = new PageUtils<>();
        int limit = 5;//每页的记录数
        if (page==null)
            page = 1;
        PageHelper.startPage(page,limit);
        List<User> userList = userService.findAll();
        PageInfo<User> pageInfo = new PageInfo<>(userList);//得到分页信息
        userPageUtils.setPage(page);//当前页
        userPageUtils.setList(userList);//当前页的记录数
        userPageUtils.setTotalPage(pageInfo.getPages());//所有的页数

        request.setAttribute("userPageUtils", userPageUtils);
        return "/admin/user/list";
    
    /*跳转到某商品的编辑页面*/
    @RequestMapping("/adminUser_edit")
    public String editUser(@RequestParam("id")Integer uid,HttpServletRequest request)
        /*根据商品ID获取某商品*/
        User user = userService.findById(uid);
        request.setAttribute("user",user);


        return "/admin/user/edit";
    

    /*保存商品修改信息,并重新上传*/
    @RequestMapping("/adminUser_update")
    public String updateUser(User user,HttpServletRequest request)throws IOException 
        int save = userService.update(user);

        return "redirect: /admin/adminUser_findAllByPage";
    

    /*删除用户*/
    @RequestMapping("/adminUser_delete")
    public String deleteUser(@Param("id") Integer uid, HttpServletRequest request)
        /*删除数据库中的记录*/
        int deleteByid = userService.deleteById(uid);
        return "redirect: /admin/adminUser_findAllByPage";
    

    /*-------以下是管理员对一级分类的操作*/

    @RequestMapping("/adminGetAllCate")
    public String getAllCategory(HttpServletRequest request)/*查找所有一级分类*/
        List<Category> categoryList = categoryService.findAll();
        request.setAttribute("cList",categoryList);

        return "admin/category/list";
    

    @RequestMapping("/toAddCategory")
    public String toAddCategory()
        System.out.println("进入添加一级分类页面");
        return "admin/category/add";
    

    /*像数据库添加一级分类*/
    @RequestMapping(value="/addCategory_save" )
    public String addCategory(Category category)
        String cname = category.getCname();
        System.out.println("添加一级分类:"+cname);
        int i = categoryService.addCategory(category);
        if(i>0)
            System.out.println("添加"+category+"成功");
        
        return "redirect: /admin/adminGetAllCate";
    

    /*跳转到编辑一级分类页面*/
    @RequestMapping("/adminCategory_edit")
    public String editCategory(@Param("cid") Integer cid,HttpServletRequest request)
        /*从数据库中查找到此分类*/
        Category cByid = categoryService.findCByid(cid);
        request.setAttribute("category",cByid);
        return "admin/category/edit";
    

    /*更新某一级分类*/
    @RequestMapping("/adminCategory_update")
    public String updateCategory(Category category)
        int i = categoryService.saveCategory(category);
        System.out.println(category.getCname()+"更新成功:"+i);
        return "redirect: /admin/adminGetAllCate";
    

    /*删除一级分类*/
    @RequestMapping(value ="/adminCategory_delete")
    public String deleteCategory(@Param("cid") Integer cid)
        /*删除一级分类之前先删除其下的二级分类*/
        List<CategorySecond> categorySecondList = categoryService.findCSByCid(cid);
        for (CategorySecond cs:categorySecondList
             ) 
            System.out.println("要删除的二级分类:"+cs.getCsname());
            int i = secondService.deleteById(cs.getCsid());
            System.out.println("删除成功与否:"+i);
        
        /*最后删除一级分类*/
        int i = categoryService.deletByid(cid);
        System.out.println("一级分类删除成功与否:"+i);
        return "redirect: /admin/adminGetAllCate";
    




    /*-------以下是管理员对二级分类的操作*/
    @RequestMapping("/adminCategorySecond_findAllByPage")
    public String findCSByPage(@Param("page") Integer page,HttpServletRequest request)/*查找所有二级分类并分页*/
        PageUtils<CategorySecond> secondPageUtils = new PageUtils<>();
        //查找所有二级分类并分页
        int limit = 4;//每页分页记录数
        if (page==null)
            page = 1;
        PageHelper.startPage(page,limit);
        List<CategorySecond> allCS = secondService.findAllCS();
        PageInfo<CategorySecond> pageInfo = new PageInfo<>(allCS);//得到分页信息
        secondPageUtils.setPage(page);//当前页
        secondPageUtils.setList(allCS);//当前页的记录
        secondPageUtils.setTotalPage(pageInfo.getPages());//所有页数

        request.setAttribute("secondPageUtils",secondPageUtils);
        return "/admin/categorysecond/list";
    

    /*进入添加二级商品类目页面*/
    @RequestMapping(value = "toAddCategorySecond")
    public String toAddCategorySecond(HttpServletRequest request)
        List<Category> categoryList = categoryService.findAll();
        request.setAttribute("categoryList",categoryList);
        return "/admin/categorysecond/add";
    
    /*添加二级商品类目*/
    @RequestMapping(value = "addcategorySecond_save")
    public String addcategorySecond_save(CategorySecond categorySecond)
        System.out.println("所添加的二级目录"+categorySecond);
        int save = secondService.add(categorySecond);
        if (save>0)
            System.out.println("添加二级目录"+categorySecond+"成功");
        return "redirect: /admin/adminCategorySecond_findAllByPage";
    

    /*跳转到编辑二级分类页面*/
    @RequestMapping("/adminCategorySecond_edit")
    public String editSecondCategory(@Param("csid") Integer csid,HttpServletRequest request)
        /*从数据库中查找到此分类*/
        CategorySecond categorySecond = secondService.findCSByid(csid);

        List<Category> categoryList = categoryService.findAll();
        request.setAttribute("categoryList", categoryList);
        request.setAttribute("categorySecond",categorySecond);
        return "/admin/categorysecond/edit";
    

    /*更新某二级分类*/
    @RequestMapping("/adminSecondCategory_update")
    public String updateSecondCategory(CategorySecond category)
       secondService.update(category);
        return "redirect: /admin/adminCategorySecond_findAllByPage";
    




    /*------------------以下是管理员对商品的操作*/
    @RequestMapping(value = ("/adminProduct_findAllByPage") )
    public String findAllProByPage(@Param("page") Integer page,HttpServletRequest request)//查找商品并分页
        PageUtils<Product> productPageUtils = new PageUtils<>();
        int limit = 5;//每页的记录数
        if (page==null)
            page = 1;
        PageHelper.startPage(page,limit);
        List<Product> productList = productService.findAll();
        PageInfo<Product> pageInfo = new PageInfo<>(productList);//得到分页信息
        productPageUtils.setPage(page);//当前页
        productPageUtils.setList(productList);//当前页的记录数
        productPageUtils.setTotalPage(pageInfo.getPages());//所有的页数

        request.setAttribute("productPageUtils", productPageUtils);
        return "/admin/product/list";
    



    @RequestMapping("/toAddProduct")
    public String toAddProduct(HttpServletRequest request)//跳转到添加商品页面
        //查找所有的二级分类
        List<CategorySecond> categorySecondList = secondService.findAllCS();
        request.setAttribute("csList",categorySecondList);
        return "/admin/product/add";
    

    /*++++++添加商品++++*/
    @RequestMapping("/addProduct_save")
    public String addProduct(Product product,@RequestParam("upload") MultipartFile upload,HttpServletRequest request)throws Exception

        product.setPdate(new Date());
        product.setIs_hot(0);

        String realPath = request.getServletContext().getRealPath("/products");
        System.out.println(realPath);
        String originalFilename = upload.getOriginalFilename();
        File diskFile = new File(realPath + "//"+ originalFilename);
        System.out.println("目标文件:"+diskFile.getAbsolutePath());
        //将上传的实体文件复制到指定目录upload下
        upload.transferTo(diskFile);
        product.setImage("products/"+originalFilename);
        System.out.println("收到的商品:"+product);
        //将信息保存到数据库
        int save = productService.save(product);
        System.out.println(product.getPname()+"保存是否成功:"+save);

        return "redirect: /admin/adminProduct_findAllByPage";
    

    /*删除产品*/
    @RequestMapping("/adminProduct_delete")
    public String deleteProduct(@Param("pid") Integer pid, HttpServletRequest request)

        /* 删除上传文件图片*/
        Product product = productService.findById(pid);
        String path = product.getImage();
        if(path!=null) 
            String realPath = request.getServletContext().getRealPath("/" + path);
            File file = new File(realPath);
            file.delete();
        
        /*删除数据库中的记录*/
        int deleteByid = productService.deleteByid(pid);

        return "redirect: /admin/adminProduct_findAllByPage";
    


    /*跳转到某商品的编辑页面*/
    @RequestMapping("/adminProduct_edit")
    public String editProduct(@RequestParam("pid")Integer pid,HttpServletRequest request)
        /*根据商品ID获取某商品*/
        Product product = productService.findById(pid);
        System.out.println(product);
        request.setAttribute("product",product);
        /*所有二级分类*/
        List<CategorySecond> allCS = secondService.findAllCS();
        request.setAttribute("csList",allCS);

        return "/admin/product/edit";
    

    /*保存商品修改信息,并重新上传*/
    @RequestMapping("/adminProduct_update")
    public String updateProduct(Product product, @RequestParam("upload")MultipartFile upload,
                                HttpServletRequest request)throws IOException 
        product.setPdate(new Date());
        if(upload.getSize() >=0)
            /*删除 目录中的文件,然后重新上传
             * 以及更新数据库中的记录*/
            String path = product.getImage();
            if (path != null)
                String realPath = request.getServletContext().getRealPath("/" + path);
                File file = new File(realPath);
                file.delete();
            
            String realPath = request.getServletContext().getRealPath("/products");
            String originalFilename = upload.getOriginalFilename();
            File diskFile = new File(realPath + "//"+ originalFilename);
            //将上传的实体文件复制到指定目录upload下
            upload.transferTo(diskFile);
            product.setImage("products/"+originalFilename);
        
        System.out.println("商品:"+product);
        //将信息保存到数据库
        int save = productService.update(product);

        return "redirect: /admin/adminProduct_findAllByPage";
    







    /*---------------以下是管理员对订单的管理,只能修改订单的状态*/
    @RequestMapping("/adminOrder_findAllByPage")
    public String findAllOrderPage(@Param("page") Integer page, HttpServletRequest request)
        PageUtils<Order> orderPageUtils = new PageUtils<>();
        if (page == null)
            page = 1;
        PageHelper.startPage(page,5);
        List<Order> allOrder = orderService.getAllOrder();
        PageInfo<Order> pageInfo = new PageInfo<>(allOrder);
        orderPageUtils.setList(allOrder);
        orderPageUtils.setPage(page);
        orderPageUtils.setTotalPage(pageInfo.getPages());

        request.setAttribute("orderPageUtils",orderPageUtils);
        return "/admin/order/list";
    

    /*根据订单编号查找订单项*/
    @RequestMapping(value = "/adminOrderItem_findById")
    @ResponseBody
    public List<OrderItem> findItemsById(@RequestBody Order order, HttpServletRequest request)
        System.out.println("所需查找的订单项ID:"+order.getOid());
        List<OrderItem> orderItemSet = orderService.findByOid(order.getOid());
        for (OrderItem orderitem: orderItemSet
             ) 
            System.out.println(orderitem);
        
//        request.setAttribute("orderItems",orderItemSet);
//        return "/admin/order/orderItem";
        return orderItemSet;
    

    @RequestMapping("/export_product_info")
    public void exportProductInfo(HttpServletResponse response)throws IOException
        response.setCharacterEncoding("UTF-8");
        List<Product> productList = productService.findAll();
        System.out.println(productList);
        /*创建excel文件*/
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("商品信息表");
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("商品名称");
        titleRow.createCell(1).setCellValue("平台价格");
        titleRow.createCell(2).setCellValue("市场价格");
        titleRow.createCell(3).setCellValue("图片目录");
        titleRow.createCell(4).setCellValue("商品广告词");
        titleRow.createCell(5).setCellValue("销售量");
        titleRow.createCell(6).setCellValue("添加日期");
        titleRow.createCell(7).setCellValue("所属目录");

        for(Product product : productList)
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
            dataRow.createCell(0).setCellValue(product.getPname());
            dataRow.createCell(1).setCellValue(product.getShop_price());
            dataRow.createCell(2).setCellValue(product.getMarket_price());
            dataRow.createCell(3).setCellValue(product.getImage());
            dataRow.createCell(4).setCellValue(product.getIs_hot());
            dataRow.createCell(5).setCellValue(product.getPdesc());
            dataRow.createCell(6).setCellValue(product.getPdate());
            dataRow.createCell(7).setCellValue(product.getCsid());
        
        // 设置下载时客户端Excel的名称
        String filename =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + filename);
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    




package com.qst.controller;

import com.qst.beans.User;
import com.qst.service.impl.UserServiceImpl;
import com.qst.util.CreateYanZhen;
import com.qst.util.EmailUtils;
import com.qst.util.MD5Encoding;
import com.qst.util.RandomNum;
import com.sun.deploy.net.HttpResponse;
import org.apache.ibatis.annotations.Param;
//import org.apache.struts2.ServletActionContext;
//import org.apache.tools.ant.types.resources.selectors.None;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletWebRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * 用户相关请求控制器
 */
@Controller
public class UserController 
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping(value = "/user_findByName")
    @ResponseBody
    public String findByName(@Param("username") String username, HttpServletRequest request)
//        username = request.getParameter("username");
        System.out.println("名称验证:" + username);
        User existUser = userService.findByName(username);
        System.out.println("existUser:" + existUser);
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter out = response.getWriter();
        if (existUser != null ) 
            return "exist";
         else 
            return "ok";
        
    

    @RequestMapping(value = "/user_login", method = RequestMethod.POST)
    public String userLogin(User user, HttpSession session,HttpServletRequest request) 
        String randomcodekey = (String)session.getAttribute("RANDOMVALIDATECODEKEY");
        System.out.println("登录者:" + user);
        System.out.println("登录验证码:"+randomcodekey);
        if(!randomcodekey.equals(user.getCode()))
            request.setAttribute("msg","验证码错误");
            return "views/login";
        
        User enterUser = userService.findByName(user.getUsername());
        if (enterUser != null) 
            System.out.println(enterUser.toString());
            if (MD5Encoding.getMD5(user.getPassword()).equals(enterUser.getPassword())) 
                session.setAttribute("enterUser", enterUser);//将登陆者信息保存到session作用域中
                return "redirect: /user_indexPage";
            else
                request.setAttribute("msg","密码错误");
        else
            request.setAttribute("msg","账号错误");
        return "views/login";
    


    @RequestMapping("/user_regist")
    public String userRegist(User user,HttpServletRequest request,HttpSession session) 
        System.out.println("注册者:" + user);
        int i = 0;
        if (user != null) 
            String pwd = MD5Encoding.getMD5(user.getPassword());
            user.setPassword(pwd);
            i = userService.addUser(user);
        
        String registCode = "";
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie : cookies)
            if(cookie.getName().equals("registCode"))
                registCode = cookie.getValue();
            
        
        System.out.println("发送的注册码:"+registCode);
        if(registCode==null || !registCode.equals(user.getCode()))
            request.setAttribute("msg","验证码错误");
            return "views/regist";
        
        System.out.println("注册是否成功?" + i);
        if (i > 0) 
            return "views/login";
         else 
            request.setAttribute("msg","用户名已存在");
            return "views/regist";
        
    

    /**
     * 发送注册验证码到邮箱
     * @param email
     * @param request
     * @param response
     */
    @RequestMapping("/send_code_email")
    @ResponseBody
    public String  sendCodeEmail(String email, HttpServletRequest request, HttpServletResponse response)
        String registCode = RandomNum.createNumRandom(4);
        EmailUtils.sendEmail(email,"农产品推荐平台注册验证码",registCode);
        Cookie cookie = new Cookie("registCode", registCode);
        cookie.setMaxAge(3*60);
        response.addCookie(cookie);
        if(registCode!=null)
            return "1";
        
        return "0";
    


    @RequestMapping("/user_logout")
    public String logout(HttpSession session) 
        session.invalidate();
        return "redirect: /user_indexPage";
    

    /*验证码生成*/
    @RequestMapping("/checkImg")
    public void setCodeImg(HttpServletRequest request, HttpServletResponse response) throws Exception 
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        CreateYanZhen randomValidateCode = new CreateYanZhen();
        try 
            randomValidateCode.getRandcode(request, response);//输出验证码图片方法
         catch (Exception e) 
            e.printStackTrace();
        
    



以上是关于2021年最后几天,使用SSM实现网上购物商城系统的主要内容,如果未能解决你的问题,请参考以下文章

网上商城购物系统设计与实现(Java+Web+SSM+MySQL)

Java项目:网上商城系统设计和实现(java+Springboot+ssm+mysql+jsp+maven)

基于SSM框架的五金电器商城管理系统的设计与实现(附源码论文)

基于ssm+mysql+jsp实现水果蔬菜商城系统

计算机毕业设计之java+ssm尤文图斯足球俱乐部网上商城系统

基于ssm的运动产品商城的设计与实现(文末附源码论文)