# 基于Jsp和MySQL的网上宠物商城系统
# 1 系统简介
## 1.1 系统开发介绍
随着人们日常生活的丰富多彩,人们对新事物的不断追求,使得各种各样的个性宠物不断出现。宠物市场上还出现了,例如蜥蜴、变色龙、蛇、蜘蛛、蜈蚣、老鼠等与我们日常生活中或惯常思维所理解的宠物不一样的“另类宠物”,不断满足着人们喂宠物的需要。当然,随着这一发展驱趋势,提供给人们各个种类宠物的宠物店则如雨后春笋般涌现。
本宠物店网上商城系统为顾客和卖家提供了一个电子商务交易平台,普通用户购买下单,管理员可进行后台管理。
宠物店网上商城在设计过程中利用JavaWeb技术,数据库技术运行需求、价格等发布与收集,方便宠物店销售管理,增进顾客关系,改善顾客服务,开拓销售渠道等,最终方便管理以及扩大销售。
## 1.2 系统具体功能
本系统用户分为普通用户和管理员两类,下面是具体功能需求:
- **普通用户**
- 登录、注册功能
- 浏览宠物功能
- 搜索宠物功能
- 添加宠物到购物车
- 购买功能(在商品详情页单独购买或在购物车批量购买,购买后自动生成相应订单)
- 查看订单状态(可以查看物流信息)
- 付款功能(线上支付)
- 确认收货功能
- 评价已购买商品功能(确认收货后方可评价)
- **管理员**
- 登陆功能
- 添加、查看、修改、删除用户信息功能
- 添加、查看、修改、删除商品信息功能
- 确认、查看、修改、删除、添加订单
- 发货功能(发布已发货信息)
# 2 功能描述和模块设计
## 2.1 功能描述
### 2.1.1 用户部分
用户可以注册一个账号,填写自己相关的信息,然后进入登录页面,进行登录,在主页可以浏览并选择自己喜欢的商品,也可以通过具体分区浏览宠物或宠物用品,还可以在导航栏搜索自己心仪的商品。在选择好自己想要的商品时,可以进入商品详情页浏览商品的具体情况,通过下方的加入购物车按钮加入购物车,在购物车勾选自己想要的宠物或商品后,可以购买,等商品收到之后可以点击确认收货按钮进行收货确认。
### 2.1.2 管理员部分
管理员可以管理用户信息,修改或删除用户信息,维护商品或宠物信息,并进行订单的管理和发货的确认。
## 2.2 模块设计
![](http://www.writebug.com/myres/static/uploads/2021/10/19/8436bcea48e159791926907bff3914c6.writebug)
# 3 主要代码清单
## 3.1 主界面
![](http://www.writebug.com/myres/static/uploads/2021/10/19/b3fdee52f94c8fb127fa0e0188c10927.writebug)
**主要代码**
Controller:取得所有宠物和商品信息。
```java
@RequestMapping(value = "/getAllCommodity")
@ResponseBody
public Map<String,Object> getAllCommodity(){
List commodityList=commodityService.getAllCommodity();
//alibaba fastjson把jsonArray转成string
String all= JSONArray.toJSONString(commodityList);
Map<String,Object> resultMap=new HashMap<String ,Object>();
resultMap.put("all",all);
return resultMap;
}
```
Service:从数据库获取所有宠物和用品的信息并被controller调用。
```java
@Override
public List getAllCommodity() {
List allCommodity=new ArrayList();
List petList=petService.getAllPets();
List productList=productService.getAllProduct();
allCommodity.add(petList);
allCommodity.add(productList);
return allCommodity;
}
```
## 3.2 登录界面
![](http://www.writebug.com/myres/static/uploads/2021/10/19/df55b082bb6fe34eb499247630dbd0e7.writebug)
**主要代码**
```java
@RequestMapping(value = "/doLogin",method= RequestMethod.POST)
@ResponseBody
public Map<String,Object> doLogin(String email, String password, HttpSession httpSession) {
System.out.println("我接收到了登录请求" + email + " " + password);
String result = "fail";
User user = userService.getUserByEmail(email);
if (user == null)
result = "unexist";
else {
if (user.getPassword().equals(password)) {
result = "success";
httpSession.setAttribute("currentUser", user);
} else
result = "wrong";
}
Map<String, Object> resultMap = new HashMap<String, Object >();
// System.out.println("result from controller:"+result);
resultMap.put("result", result);
return resultMap;
}
```
## 3.3 注册界面
![](http://www.writebug.com/myres/static/uploads/2021/10/19/96799b21db0e6ecc75300bc054a19a9c.writebug)
**主要代码**
```java
User user=userService.getUserByEmail(email);
if(user!=null) result="emailExist";
else{
User u=new User();
u.setName(name);
u.setEmail(email);
u.setPassword(password);
u.setPhone(phone);
u.setRole(0);
u.setSex(gender);
Date date=new Date();
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
u.setCreateTime(sf.format(date));
userService.addUser(u);
result="success";
}
```
## 3.4 搜索界面
![](http://www.writebug.com/myres/static/uploads/2021/10/19/071a1598e242bd666a1b53591bc8755b.writebug)
**主要代码**
在petMapper里定义的方法:
```java
List<Pet> selectByKeyWord(String keyword);
```
对应的mapper.xml定义响应的模糊查询语句:
```xml
<select id="selectByKeyWord" parameterType="java.lang.String" resultMap="BaseResultMap">
select <include refid="Base_Column_List" /> from pet
<where>
<if test="_parameter != null">
AND key_word LIKE concat(concat('%',#{key_word}),'%')
</if>
</where>
</select>
```
在service里调用以上方法:
```java
@Override
public List<Pet> getPetsByKeyWord(String searchKeyWord) {
return petMapper.selectByKeyWord(searchKeyWord);
}
```
## 3.5 宠物详情页
![](http://www.writebug.com/myres/static/uploads/2021/10/19/719f5667d23e463ba6a490d070d4f7a5.writebug)
**主要代码**
Controller拦截getPetDetail路径,并设置当前商品的信息到session:
```java
@RequestMapping(value = "/getPetDetail")
@ResponseBody
public Map<String,Object> getPetDetail(Integer id,HttpSession httpSession){
System.out.println("拦截到petDetail请求 id:"+id);
Map<String,Object> resultMap=new HashMap<String, Object>();
Pet pet =petService.getPet(id);
petService.updatePet(pet);
System.out.println("pet:"+pet.getName());
httpSession.setAttribute("currentCommodity",pet);
resultMap.put("result","success");
return resultMap;
}
```
Service在数据库查找相应数据:
```java
@Override
public Pet getPet(int id) {
return petMapper.selectByPrimaryKey(id);
}
```
## 3.6 宠物展示页
![](http://www.writebug.com/myres/static/uploads/2021/10/19/3793301086c7e10f1175debadada1ea5.writebug)
**主要代码**
service:
```java
@Override
public List<Pet> getAllPets() {
PetExample pe=new PetExample();
PetExample.Criteria pec=pe.createCriteria();
pec.andIdIsNotNull();
pec.andNameIsNotNull();
pe.setOrderByClause("id asc");
return petMapper.selectByExample(pe);
}
```
controller:
```java
@RequestMapping(value = "/getAllPets")
@ResponseBody
public Map<String,Object> getAllPets(){
List pet_list=petService.getAllPets();
String all_pet=JSONArray.toJSONString(pet_list);
Map<String,Object> resultMap=new HashMap<String ,Object>();
resultMap.put("all_pet",all_pet);
return resultMap;
}
```
## 3.7 购物车页
![](http://www.writebug.com/myres/static/uploads/2021/10/19/4911d92a318d45e2d80679fdbd920e8d.writebug)
**主要代码**
```java
@Override
public ShoppingCar getShoppingCar(int userId, int productId) {
ShoppingCarKey key=new ShoppingCarKey();
key.setUserId(userId);
key.setProductId(productId);
return shoppingCarMapper.selectByPrimaryKey(key);
}
@Ove