②JAVA项目实战:Vue+SSM+分类+商品(前台主站页面)

本文通过一系列步骤展示了如何使用Vue.js和Axios库创建一个简单的电商网站,包括商品分类展示、商品详情页、商品管理(增删改查)、管理员登录、商品分类添加商品和用户管理等功能。涉及到的主要技术包括Vue组件、Vue实例、Vue数据绑定、路由跳转、数据库操作以及跨域请求等。此外,还提到了在IDEA中添加@CrossOrigin注解以解决跨域问题,以及使用Hbuilder创建和编辑HTML页面。
摘要由CSDN通过智能技术生成

前台主站页面

页面分析:

1.在IDEA中启动服务器

在这里插入图片描述

2.Hbuilder打开projectWeb项目

创建一个模板template.html

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app"></div>
	</body>
	<script>
		var config={
			el:"#app",
			data:{},
			methods:{
				
			},
			mounted:function(){
				console.log("mounted()");
			}
		}
		var vue=new Vue(config);
		
	</script>
</html>

3.复制模板粘贴一个模板,F2重命名为detail.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app"></div>
	</body>
	<script>
		var config={
			el:"#app",
			data:{},
			methods:{
				
			},
			mounted:function(){
				debugger;
				console.log("mounted()");
				//从首页跳转过来
				//detail.html?itemId=2
				//?itemId=2
				var parameter=location.search;
				//去掉?取后面的值
				parameter=parameter.substr(1);
				//字符串的切割
				//stringArray[0]=itemId
				//stringArray[1]=2
				var stringArray=parameter.split("=");
				//取商品的编号
				var itemId=stringArray[1]
				console.log(itemId);
			}
		}
		var vue=new Vue(config);
		
	</script>
</html>

打开浏览器测试:
在这里插入图片描述



调取商品

连接数据库,调取商品

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app">
			<img v-bind:src="item.itemImage"><br />
			手机型号:{{item.itemName}}<br>
			价格:{{item.itemPrice}}<br>
			详细:{{item.itemDesc}}<br>
			商品评论<br />
			商品推荐<br />
		</div>
	</body>
	<script>
		var config = {
			el: "#app",
			data: {
				item: null
			},
			methods: {

			},
			mounted: function() {
				debugger;
				console.log("mounted()");
				//从首页跳转过来
				//detail.html?itemId=2
				//?itemId=2
				var parameter = location.search;
				//去掉?取后面的值
				parameter = parameter.substr(1);
				//字符串的切割
				//stringArray[0]=itemId
				//stringArray[1]=2
				var stringArray = parameter.split("=");
				//取商品的编号
				var itemId = stringArray[1];
				console.log(itemId);
				//连接数据库调取商品数据 http://localhost:1314/item/findByItemId?id=1
				var serverUrl = "http://localhost:1314" +
					"/item/findByItemId?id=" + itemId;
				console.log(serverUrl);
				console.log(this);
				//mounted中的this代表的是vue,有item属性
				var vue = this;
				axios.get(serverUrl)
					.then(function(response) {
						debugger;
						var item = response.data;
						//this函数中,this代表的是window,没有 item属性
						//this.item=item;
						vue.item = item;
					})
					.catch(function(e) {
						window.alert("联网失败");
						//打印失败信息
						console.log(e);
					});
			}
		}
		var vue = new Vue(config);
	</script>
</html>

运行执行测试:
我们可以发现跨域调用了,调用失败
在这里插入图片描述

给IDEA的商品操作都加上@CrossOrigin注解

package com.tedu.mall.Controller;

import com.tedu.mall.mapper.ItemMapper;
import com.tedu.mall.pojo.Item;
import com.tedu.mall.pojo.ItemExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//商品操作
@RestController
@CrossOrigin
public class ItemController {
    @Autowired
    ItemMapper itemMapper;

    //更新商品
    
    @RequestMapping("/item/update")
    @CrossOrigin
    //http://localhost:1314/item/update?itemId=4
    //&categoryId&itemName=华硕冰锐4
    //&itemPrice=9999&itemDesc=3060Ti
    //&itemImage=9.jpg
    public String update(Item item){
         //update item set item_name=新名字
        //where item_id=4
        int count=itemMapper.updateByPrimaryKey(item);
        if (count>=1){
            return "更新成功";
        }else {
            return "更新失败";
        }
    }

    //删除
    @RequestMapping("/item/delete")
    @CrossOrigin
    //localhost:1314/item/delete?itemId=5
    public String delete(Integer itemId){
        //delete from item where item_id=5
        int count = itemMapper.deleteByPrimaryKey(itemId);
        if (count>=1){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }


    //查询所有商品
    @RequestMapping("/item/selectAll")
    @CrossOrigin
    public List<Item> selectAll(){
        return itemMapper.selectByExample(null);
    }

    //添加商品
    @RequestMapping("/item/insert")
    @CrossOrigin
    /*localhost:1314/intem/insert?categoryId=1&itemName=n1
    *&itemImage=1.jpg&itemPrice=90&itemDesc=desc
    */
    public String insert(Item item){
       int count=itemMapper.insertSelective(item);
       if(count >=1){
           return "添加成功";
       }else {
           return "添加失败";
       }
    }

    //首页显示商品,用户单击一个商品,显示商品详细
    //根据商品编号查询商品
    @RequestMapping("/item/findByItemId")
    @CrossOrigin
    //localhost:1314/item/findByItemId?id=2
    public Item findByItemId(Integer id){
        Item item = itemMapper.selectByPrimaryKey(id);
        return item;
    }


    //查询分类下的所有商品
    @RequestMapping("/item/findByCategoryId")
    @CrossOrigin
    //http://localhost:1314/item/findByCategoryId?id=1
    public List<Item> findByCategoryId(Integer id){
        //获取where条件
        ItemExample itemExample=new ItemExample();
        ItemExample.Criteria criteria=itemExample.or();
        criteria.andCategoryIdEqualTo(id);
        //得到一个集合,集合中放的是商品
        List<Item> list=itemMapper.selectByExample(itemExample);
        return list;
    }
}

重启服务器

重新测试连接

在这里插入图片描述

如果没有显示图片:
在IDEA项目文件目录下添加图片 projectService\src\main\resources\static
在这里插入图片描述

然后更改数据库的图片端口号:
在这里插入图片描述



商品分类

复制模板 F2更名为index.html(商品分类)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app">
			<table>
				<tr>
					<td v-for="category in categoryList">
						{{category.categoryName}}
					</td>
				</tr>
			</table>
		</div>
	</body>
	<script>
		var config = {
			el: "#app",
			data: {
				categoryList: []
			},
			methods: {
				findCategory: function() {
					debugger;
					var serverUrl = "http://localhost:1314/category/selectAll"
					var vue = this;
					axios.get(serverUrl)
						.then(function(response) {
							debugger;
							vue.categoryList = response.data;
							console.log(vue.categoryList);
						})
						.catch(function(e) {
							window.alert("联网失败");
							console.log(e);
						});
				}
			},
			mounted: function() {
				console.log("mounted()");
				this.findCategory();
			}
		}
		var vue = new Vue(config);
	</script>
</html>

运行:
在这里插入图片描述

商品分类

在index.html中增加商品分类

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app">
			<div>
			<table>
				<tr>
					<td v-for="category in categoryList">
						<img v-bind:src="item.itemImage" width="100" height="100"/>
						{{category.categoryName}}
					</td>
				</tr>
			</table>
			</div>
			<div>
			<table>
				<tr v-for="item in itemList"><br />
					<td>
					<img v-bind:src="item.itemImage" width="100" height="100"/>
					<br />
					{{item.itemName}}
					</td>
				</tr>
			</table>
			</div>
		</div>
	</body>
	<script>
		var config = {
			el: "#app",
			data: {
				categoryList: [],
				itemList: [],
			},
			methods: {
				findCategory: function() {
					debugger;
					var serverUrl = "http://localhost:1314/category/selectAll"
					var vue = this;
					axios.get(serverUrl)
						.then(function(response) {
							debugger;
							vue.categoryList = response.data;
							console.log(vue.categoryList);
						})
						.catch(function(e) {
							window.alert("联网失败");
							console.log(e);
						});
				},
				findItemBycategoryId:function(id){
					debugger;
					var serverUrl="http://localhost:1314/item/findByCategoryId?id="+id;
					//F12 从console窗口中拷贝地址到浏览器测试
					console.log(serverUrl);
					var vue=this;
					axios.get(serverUrl)
					.then(function(response){
						debugger;
						vue.itemList=response.data;
					})
					.catch(function(e){
						window.alert("联网失败");
						console.log(e);
					})
				}
			},
			mounted: function() {
				console.log("mounted()");
				this.findCategory();
				this.findItemBycategoryId(1);
			}
		}
		var vue = new Vue(config);
	</script>
</html>

运行:
在这里插入图片描述

页面跳转

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app">
			<a href="register.html">注册</a>
			<br>
			<a href="login.html">登录</a>
			<div>
			<table>
				<tr>
					<td v-for="category in categoryList"
					v-on:click="findItemBycategoryId(category.categoryId)"
					>
						{{category.categoryName}}
					</td>
				</tr>
			</table>
			</div>
			<div>
			<table>
				<tr v-for="item in itemList">
					<td>
						<a v-bind:href="'detail.html?id='+item.itemId">
					<img v-bind:src="item.itemImage"
					width="100" height="100"
					/>
					<br>
					{{item.itemName}}
					</a>
					</td>
				</tr>
			</table>
			</div>
		</div>
	</body>
	<script>
		var config={
			el:"#app",
			data:{
				categoryList:[],
				itemList:[]
			},
			methods:{
				findCategory:function(){
					debugger;
					var serverUrl="http://localhost:1314/category/selectAll"
					var vue=this;
					axios.get(serverUrl)
					.then(function(response){
						debugger;
						vue.categoryList=response.data;
						console.log(vue.categoryList);
					})
					.catch(function(e){
						window.alert("联网失败");
						console.log(e);
					});
				},
				findItemBycategoryId:function(id){
					debugger;
					var serverUrl="http://localhost:1314"
					+"/item/findByCategoryId?id="+id;
					//F12 从console窗口中拷贝地址到浏览器中测试一下
					console.log(serverUrl);
					var vue=this;
					axios.get(serverUrl)
					.then(function(response){
						debugger;
						vue.itemList=response.data;
					})
					.catch(function(e){
						window.alert("联网失败");
						console.log(e);
					});
					
				}
			},
			mounted:function(){
				console.log("mounted()");
				this.findCategory();
				this.findItemBycategoryId(1);
			}
		}
		var vue=new Vue(config);
	</script>
</html>

运行:
在这里插入图片描述



后台管理

新建项目projectAdmin

然后把复制这3个到中projectAdmin

在这里插入图片描述

管理员登录

新建adminLogin.html.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app">
			<h1>管理员登录</h1>
			账 号:<input v-model="adminName" /><br />
			密 码:<input v-model="adminPassword" /><br />
			<br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<button v-on:click="login">登录</button>
		</div>
	</body>
	<script>
		var config={
			el:"#app",
			data:{
				adminName:null,
				adminPassword:null
			},
			methods:{
				login:function(){
					debugger;
					var serverUrl="http://localhost:1314//admin/login?"
					+"adminName="+this.adminName
					+"&adminPassword="+this.adminPassword;
					//在浏览器中测试Url
					console.log(serverUrl);
					axios.get(serverUrl)
					.then(function(res){
						var admin=res.data;
						if(admin.adminId){
							window.alert("管理员登录成功");
							location.href="index.html";
						}
					})
					.catch(function(e){
						window.alert("连接失败");
						console.log(e);
					})
				}
			},
			mounted:function(){
			  console.log("mounted()");
			}
		}
		var vue=new Vue(config);
		
	</script>
</html>

执行:
在这里插入图片描述

商品分类 添加商品

复制模板粘贴,F2更改为item.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app">
			分类: <select v-model="selectId">
				<option v-for="category in categoryList" v-bind:value="category.categoryId">
					{{category.categoryName}}
				</option>
			</select>
			<br>
			名称: <input v-model="itemName" /><br>
			价格: <input v-model="itemPrice" /><br>
			描述: <input v-model="itemDesc" /><br>
			图片: <input v-model="itemImage" /><br>
			<br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<button v-on:click="insert">添加</button>
		</div>
	</body>
	<script>
		var config = {
			el: "#app",
			data: {
				selectId: null,
				categoryList: [],
				itemName: "",
				itemPrice: "",
				itemDesc: "",
				itemImage: ""
			},
			methods: {
				insert: function() {
					debugger;
					var serverurl = "http://localhost:1314/item/insert?" 
					+"categoryId=" + this.selectId
					+"&itemName=" + this.itemName
					+"&itemPrice=" + this.itemPrice
					+"&itemDesc=" + this.itemDesc
					+"&itemImage=" + this.itemImage;
					//输入参数,点添加,从console中拷贝url到浏览器
					console.log(serverurl);
					axios.get(serverurl)
					.then(function(res){
						window.alert(res.data);
					})
					.catch(function(e){
						window.alert("联网失败");
						console.log(e);
					})
				},
				findCategory: function() {
					debugger;
					var serverUrl = "http://localhost:1314/category/selectAll";
					var vue = this;
					axios.get(serverUrl)
						.then(function(res) {
							debugger;
							vue.categoryList = res.data;
						})
						.catch(function(e) {
							window.alert("联网失败");
							console.log(e);
						});
				}
			},
			mounted: function() {
				console.log("mounted()");
				this.findCategory();
			}
		}
		var vue = new Vue(config);
	</script>
</html>

运行:
在这里插入图片描述

查看数据库

在这里插入图片描述



实现增删改查

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app">
			分类: <select v-model="selectId">
				<option v-for="category in categoryList" v-bind:value="category.categoryId">
					{{category.categoryName}}
				</option>
			</select>
			<br>
			名称: <input v-model="itemName" /><br>
			价格: <input v-model="itemPrice" /><br>
			描述: <input v-model="itemDesc" /><br>
			图片: <input v-model="itemImage" /><br>
			<br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<button v-on:click="insert">添加</button>
			<br />

			<table border="2">
				<tr>
					<td>商品编号</td>
					<td>名称</td>
					<td>价格</td>
					<td>删除</td>
					<td>修改</td>
				</tr>
				<tr v-for="item in itemList">
					<td>{{item.itemId}}</td>
					<td>{{item.itemName}}</td>
					<td>{{item.itemPrice}}</td>
					<td v-on:click="deleteItem(item.itemId)">删除</td>
					<td><a v-bind:href="'update.html?itemId='+item.itemId">修改</a></td>
				</tr>
			</table>

		</div>
	</body>
	<script>
		var config = {
			el: "#app",
			data: {
				selectId: null,
				categoryList: [],
				itemName: "",
				itemPrice: "",
				itemDesc: "",
				itemImage: "",
				itemList: []
			},
			methods: {
				deleteItem: function(itemId) {
					var result = window.confirm("你确定要删除吗?");
					//window.alert(result);
					if (result) {
						var serverUrl = "http://localhost:1314/item/delete?itemId=" + itemId;
						console.log(serverUrl);
						var vue=this;
						axios.get(serverUrl)
							.then(function(res) {
								//sqlyog中查看是否删除
								window.alert(res.data);
								//重新查询
								vue.findItem();
							})
							.catch(function(e) {
								window.alert("删除失败");
								console.log(e);
							});
					}
				},
				findItem: function() {
					var serverUrl = "http://localhost:1314/item/selectAll";
					var vue = this;
					axios.get(serverUrl)
						.then(function(res) {
							debugger;
							vue.itemList = res.data;
						})
						.catch(function(e) {
							window.alert("联网失败");
							console.log(e);
						});
				},

				insert: function() {
					debugger;
					var serverurl = "http://localhost:1314/item/insert?"
					+"categoryId=" + this.selectId 
					+"&itemName=" + this.itemName 
					+"&itemPrice=" + this.itemPrice 
					+"&itemDesc=" + this.itemDesc 
					+"&itemImage=" + this.itemImage;
					//输入参数,点添加,从console中拷贝url到浏览器
					console.log(serverurl);
					axios.get(serverurl)
						.then(function(res) {
							window.alert(res.data);
						})
						.catch(function(e) {
							window.alert("联网失败");
							console.log(e);
						});
				},
				findCategory: function() {
					debugger;
					var serverUrl = "http://localhost:1314/category/selectAll";
					var vue = this;
					axios.get(serverUrl)
						.then(function(res) {
							debugger;
							vue.categoryList = res.data;
						})
						.catch(function(e) {
							window.alert("联网失败");
							console.log(e);
						});
				}
			},
			mounted: function() {
				console.log("mounted()");
				this.findCategory();
				this.findItem();
			}
		}
		var vue = new Vue(config);
	</script>
</html>

运行:
在这里插入图片描述

管理员修改功能

复制模板 F2更名为update.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>商品修改</title>
		<script src="vue.js" type="text/javascript"></script>
		<script src="axios.min.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="app">
			分类:
			<select v-model="itemCategoryId">
				<option v-for="category in categoryList" v-bind:value="category.categoryId">{{category.categoryName}}</option>
			</select>
			<br />
			名称: <input v-model="item.itemName" /><br />
			价格: <input v-model="item.itemPrice" /><br />
			描述: <input v-model="item.itemDesc" /><br />
			图片: <input v-model="item.itemImage" /><br />
			<br />
			<button v-on:click="update">修改</button>
		</div>
	</body>
	<script>
		var config = {
			el: "#app",
			data: {
				categoryList: [],
				itemCategoryId: null,
				item: null
			},
			methods: {
				update: function() {
					var serverUrl = "http://localhost:1314/item/update?" +
						"itemId=" + this.item.itemId +
						"&categoryId=" + this.itemCategoryId +
						"&itemName=" + this.item.itemName +
						"&itemPrice=" + this.item.itemPrice +
						"&itemDesc=" + this.item.itemDesc +
						"&itemImage=" + this.item.itemImage;
					//update.htm?itemId=2
					//console拷贝url到浏览器测试
					console.log(serverUrl);
					axios.get(serverUrl)
						.then(function(res) {
							window.alert(res.data);
							//让浏览器访问item.html
							location.href="item.html";
						})
						.catch();
				},
				findItemById: function(itemId) {
					var serverUrl = "http://localhost:1314/item/findByItemId?id=" + itemId;
					console.log(serverUrl);
					var vue = this;
					axios.get(serverUrl)
						.then(function(res) {
							debugger;
							vue.item = res.data;
							//修改data中商品的分类id
							vue.itemCategoryId = vue.item.categoryId;
						})
						.catch()
				},

				findCategory: function() {
					debugger;
					var serverUrl = "http://localhost:1314/category/selectAll";
					var vue = this;
					axios.get(serverUrl)
						.then(function(res) {
							debugger;
							vue.categoryList = res.data;
						})
						.catch(function(e) {
							window.alert("联网失败");
							console.log(e);
						});
				}
			},
			mounted: function() {
				console.log("mounted()");
				this.findCategory();
				//获取传过来的商品编号
				//?itemId=2
				var p = location.search;
				//itemId=2
				p = p.substr(1);
				//对字符串进行切割
				var stringArray = p.split("=");
				var itemId = stringArray[1];
				console.log(itemId);
				this.findItemById(itemId);
			}
		}
		var vue = new Vue(config);
	</script>
</html>

运行:
在这里插入图片描述

用户管理

新建一个index.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>用户管理界面</title>
	</head>
	<body>
		<a href="item.html">商品管理</a>
		<a href="user.html">用户管理</a>
	</body>
</html>

新建一个user.html

<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		用户管理
	</body>
</html>

运行:
在这里插入图片描述

到此所有的步骤都完成了,页面比较简陋,功能实现后可以自己进行调试



本系统是一个基于SSM+Vue.js的网上家电商城系统。本系统的服务对象是家电企业或家电专营店商家。家电商家目前大部分依赖第三方购物平台售货。创建本系统的目的是让这些家电企业有属于自己的网上售货渠道。网上家电商城为家电商家规范了业务流程,简化了业务处理工作量。对于客户来说,有些客户只希望在特定品牌或门店购买。本系统也为这些客户提供了新的购买渠道。 网上家电商城系统分别实现了商家和客户两个模块。客户模块由前台商城系统实现,分为浏览商品、注册登录、下单、退款等子模块。商家模块由后台管理系统实现,分为商品管理、商品类型管理、订单管理、客户管理等子模块。网上家电商城系统完整实现了实际生产生活中客户选购、客户下单、卖家处理订单以及退款等业务流程。 系统开发采用B/S架构,以Java作为开发语言。整个系统的后端基于如今企业开发常用的SSM框架,搭载在Tomcat服务器上。系统的前端分为两个模块。客户模块前端使用EasyUI框架。商家模块前端使用Vue.js框架进行开发,采用前后端分离系统,使用Nginx服务器反向代理。系统所用数据库基于中小型的MySQL数据库管理系统实现。基于以上技术,本系统经过数据库设计、系统功能设计、系统实现等开发流程,完成了软件开发的相关工作,满足实际生产生活中网上家电商城的功能需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值