SSM实现商城管理系统

学完Mybatis、Spring、SpringMVC后,我做了一个电子商品的商城管理系统。

用到的知识:

  • vue
  • Mybatis
  • Spring
  • SpringMVC
  • jsp
  • javaScript等

先来展示下大概的页面。

登录页面

商品展示页面

 购物车页面

 完成订单页面

 个人信息,充值页面

 管理员的添加新品

 下来是一些重要功能的代码

文件上传:

@Controller
public class FileController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping( "/addshop")
    public String test09(@RequestParam("file") MultipartFile multipartFile, String type, String brand, String price, HttpServletRequest request, HttpServletResponse resp) throws IOException {
        // 获取文件上传到具体文件夹的绝对路径
        String realpath = request.getSession().getServletContext().getRealPath("upload");
        // 获取上传的文件名
        String fileName = multipartFile.getOriginalFilename();
        // 根据路径构建文件对象
        // 在构建过程中一定要注意路径问题
        File uploadFile = new File(realpath, fileName);
        // 判断指定文件夹uploadfiles是否存在,不存在就创建
        if (!uploadFile.exists()) {
            uploadFile.mkdirs();
        }
        String s;
        String image = goodsService.selectGoodsByimage(fileName);
        if (image==null) {
            // 上传文件
            multipartFile.transferTo(uploadFile);
            System.out.println(type + " " + brand + " " + price);
            Goods goods = new Goods();
            goods.setBrand(brand);
            goods.setPrice(Integer.parseInt(price));
            goods.setImage(fileName);
            goods.setType(type);
            goodsService.addshop(goods);
            s = "添加成功";
        }else {
            s = "用户已存在";
        }
        resp.getWriter().write(s);
        return "adminList.jsp";
    }
}

购买操作:

    //购买操作
    @RequestMapping("/buyselect")
    public void buySelect(String goods[],String username,String price,HttpServletResponse resp) throws IOException {
        //获取操作时间
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        User user = userService.selectbalanceByusername(username);
        String a;
        if (Integer.parseInt(user.getBalance())>=Integer.parseInt(price)){
            String x = String.valueOf(Integer.parseInt(user.getBalance())-Integer.parseInt(price));
            User user1 = new User();
            user1.setBalance(x);
            user1.setUsername(username);
            userService.updatebalanceByusername(user1);
            Record record = new Record();
            record.setUsername(username);
            record.setBuytime(dateFormat.format(date));
            for (String good:goods) {
                String[] s = good.split(" ");
                record.setGoodsimage(s[0]);
                record.setBuynum(s[1]);
                System.out.println(s[0]);
                System.out.println(s[1]);
                recordService.updaterecord(record);
            }
            a="1";
        }else {
            a="0";
        }
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(a);
    }

vue实现的数据动态变化:

new Vue({
        el:"#la",
        data:{
            num:[],
            sum:0,
        },
        methods:{
            //点击全部勾选,按钮变
            allcheck(){
                for (let i=0;i<check.length;i++){
                check[i].checked = true;
                }
                this.money()
            },
            //点击取消全部勾选
            nocheck(){
                for (let i =0 ;i<check.length;i++){
                check[i].checked = false;
                }
                this.money()
            },
            //算总价功能
            money(){
                let sum = 0
                for (let i=0;i<check.length;i++){
                    if (check[i].checked===true){
                        sum+=shuliang[i].innerHTML*goodsprice[i].innerHTML
                    }
                }
                this.sum=sum
            },
            //展示购物车页面
            showshop(){
                btnNums.style.display="none"
                select.style.display="none"
                layuifooter.style.display="block"
                axios({
                    method: 'get',
                    url: "/showshoplist?username="+"${user.username}"
                }).then((resp) => {
                    let things = resp.data
                    for (let i=0;i<things.length;i++) {
                        this.num[i] = 1
                    }
                    listAll.innerHTML = `<tr>
                                     <th>勾选</th>
                                     <th>序号</th>
                                     <th>图片</th>
                                     <th>单价</th>
                                     <th>数量</th>
                                     <th>操作</th>
                                </tr>`
                    for (let j = 0; j < things.length; j++) {
                        listAll.innerHTML += `
                    <tr>
                        <td><input type="checkbox" class="check"></td>
                        <td>` + (j+1)+ `</td>
                        <td><img class="imgg" src="../upload/` + things[j].goodsimage + `" alt=""></td>
                        <td class="goodsprice">` + things[j].goodsprice + `</td>
                        <td class="shuliang" id="`+(j+1)+`" >`+this.num[j]+`</td>
                        <td>
                            <button class="nadd"> + </button>
                            <button class="nsub"> - </button>
                        </td>
                    </tr>`
                    }
                    let nadds = document.getElementsByClassName('nadd')
                    let nsubs = document.getElementsByClassName('nsub')

                    for (let i=0;i<nadds.length;i++) {
                        nadds[i].onclick = () => {
                            this.num[i]++
                            document.getElementById(i+1).innerHTML = this.num[i]
                            this.money()
                        }
                        nsubs[i].onclick = () => {
                            if(this.num[i]-1 === 0) return
                            this.num[i]--
                            document.getElementById(i+1).innerHTML = this.num[i]
                            this.money()
                        }
                        check[i].onclick = ()=>{
                            this.money()
                        }
                    }
                })
            },
            //购买操作
            buyselect(){
                let s = new Array()
                for (let i=0;i<check.length;i++) {
                    if(check[i].checked){
                        s.push(imgg[i].src.substring(29)+" "+shuliang[i].innerText)
                    }
                }
                if (s.length!==0){
                    axios({
                        method:"get",
                        url:"/buyselect?goods="+s+"&&username="+"${user.username}"+"&&price="+this.sum
                    }).then((resp)=> {
                        let x = resp.data
                        if (x===1){
                            this.showshop()
                            summoney.innerHTML = '0'
                            alert("购买成功")
                        }
                        if (x===0) {
                            alert("请检查余额是否充足")
                        }
                    })
                }else {
                    alert("请选择要购买的商品")
                }
            },
            //展示已完成订单
            showbuylist(){
                axios({
                    method: "get",
                    url:"/showbuylist?username="+"${user.username}"
                }).then(function (resp){
                    let things = resp.data
                    btnNums.style.display="none"
                    select.style.display="none"
                    layuifooter.style.display="none"
                    listAll.innerHTML = `<tr>
                                     <th>序号</th>
                                     <th>图片</th>
                                     <th>单价</th>
                                     <th>数量</th>
                                     <th>购买时间</th>
                                </tr>`
                    for (let j = 0; j < things.length; j++) {
                        listAll.innerHTML += `
                    <tr>
                        <td>` + (j+1)+ `</td>
                        <td><img class="imgg" src="../upload/` + things[j].goodsimage + `" alt=""></td>
                        <td class="goodsprice">` + things[j].goodsprice + `</td>
                        <td class="shuliang">` + things[j].buynum+ `</td>
                        <td>`+things[j].buytime+`</td>
                    </tr>`
                }
            })
        },
    }
    })

源码:GitHub - FrankZhang63/SSMContribute to FrankZhang63/SSM development by creating an account on GitHub.https://github.com/FrankZhang63/SSM.git

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值