1.项目介绍
实现一个网上商城,商品信息展示,购物车,订单管理,个人中心,商品评价,商品搜索,地址管理,聊天,后台管理(商品增删改查),分类管理,活动管理,客服聊天回复
2.开发环境
- 开发环境:IDEA/eclipse、Tomcat8.5+
- 数据库:MySql
- 前端主要使用Bootstrap以及JQuery,后端基于SpringMVC、Spring、MyBatis进行开发,使用Maven构建
- activeMQ
相关代码
@Controller
public class OrderController {
/*@Value("#{addressService}")*/
@Autowired
private AddressService addressService;
@Autowired
private ShopCartService shopCartService;
@Autowired
private GoodsService goodsService;
@Autowired
private OrderService orderService;
@Autowired
private ActivityService activityService;
@RequestMapping("/order")
public String showOrder(HttpSession session, Model model) {
User user = (User) session.getAttribute("user");
if (user == null) {
return "redirect:/login";
}
//查询当前用户的收货地址
AddressExample addressExample = new AddressExample();
addressExample.or().andUseridEqualTo(user.getUserid());
List<Address> addressList = addressService.getAllAddressByExample(addressExample);
model.addAttribute("address", addressList);
//订单信息
//获取当前用户的购物车信息
ShopCartExample shopCartExample = new ShopCartExample();
shopCartExample.or().andUseridEqualTo(user.getUserid());
List<ShopCart> shopCart = shopCartService.selectByExample(shopCartExample);
//获取购物车中的商品信息
List<Goods> goodsAndImage = new ArrayList<>();
Float totalPrice = new Float(0);
Integer oldTotalPrice = 0;
for (ShopCart cart:shopCart) {
Goods goods = goodsService.selectById(cart.getGoodsid());
List<ImagePath> imagePathList = goodsService.findImagePath(goods.getGoodsid());
goods.setImagePaths(imagePathList);
goods.setNum(cart.getGoodsnum());
//活动信息
Activity activity = activityService.selectByKey(goods.getActivityid());
goods.setActivity(activity);
if(activity.getDiscount() != 1) {
goods.setNewPrice(goods.getPrice()*goods.getNum()* activity.getDiscount());
} else if(activity.getFullnum() != null) {
if (goods.getNum() >= activity.getFullnum()) {
goods.setNewPrice((float) (goods.getPrice()*(goods.getNum()-activity.getReducenum())));
} else {
goods.setNewPrice((float) (goods.getPrice()*goods.getNum()));
}
} else {
goods.setNewPrice((float) (goods.getPrice()*goods.getNum()));
}
totalPrice = totalPrice + goods.getNewPrice();
oldTotalPrice = oldTotalPrice + goods.getNum() * goods.getPrice();
goodsAndImage.add(goods);
}
model.addAttribute("totalPrice", totalPrice);
model.addAttribute("oldTotalPrice", oldTotalPrice);
model.addAttribute("goodsAndImage", goodsAndImage);
return "orderConfirm";
}
@RequestMapping("/orderFinish")
@ResponseBody
public Msg orderFinish(Float oldPrice, Float newPrice, Boolean isPay, Integer addressid,HttpSession session) {
User user = (User) session.getAttribute("user");
//获取订单信息
ShopCartExample shopCartExample = new ShopCartExample();
shopCartExample.or().andUseridEqualTo(user.getUserid());
List<ShopCart> shopCart = shopCartService.selectByExample(shopCartExample);
//删除购物车
for (ShopCart cart : shopCart) {
shopCartService.deleteByKey(new ShopCartKey(cart.getUserid(),cart.getGoodsid()));
}
//把订单信息写入数据库
Order order = new Order(null, user.getUserid(), new Date(), oldPrice, newPrice, isPay, false, false, false, addressid,null,null);
orderService.insertOrder(order);
//插入的订单号
Integer orderId = order.getOrderid();
//把订单项写入orderitem表中
for (ShopCart cart : shopCart) {
orderService.insertOrderItem(new OrderItem(null, orderId, cart.getGoodsid(), cart.getGoodsnum()));
}
return Msg.success("购买成功");
}
}
@Controller
public class MainController {
@Autowired
private CateService cateService;
@Autowired
private GoodsService goodsService;
@RequestMapping("/main")
public String showAllGoods(Model model, HttpSession session) {
Integer userid;
User user = (User) session.getAttribute("user");
if (user == null) {
userid = null;
} else {
userid = user.getUserid();
}
//数码分类
List<Goods> digGoods = getCateGoods("数码", userid);
model.addAttribute("digGoods", digGoods);
//家电
List<Goods> houseGoods = getCateGoods("家电", userid);
model.addAttribute("houseGoods", houseGoods);
//服饰
List<Goods> colGoods = getCateGoods("服饰", userid);
model.addAttribute("colGoods", colGoods);
//书籍
List<Goods> bookGoods = getCateGoods("书籍", userid);
model.addAttribute("bookGoods", bookGoods);
return "main";
}
public List<Goods> getCateGoods(String cate, Integer userid) {
//查询分类
CategoryExample digCategoryExample = new CategoryExample();
digCategoryExample.or().andCatenameLike(cate);
List<Category> digCategoryList = cateService.selectByExample(digCategoryExample);
if (digCategoryList.size() == 0) {
return null;
}
//查询属于刚查到的分类的商品
GoodsExample digGoodsExample = new GoodsExample();
List<Integer> digCateId = new ArrayList<Integer>();
for (Category tmp:digCategoryList) {
digCateId.add(tmp.getCateid());
}
digGoodsExample.or().andCategoryIn(digCateId);
List<Goods> goodsList = goodsService.selectByExampleLimit(digGoodsExample);
List<Goods> goodsAndImage = new ArrayList<>();
//获取每个商品的图片
for (Goods goods:goodsList) {
//判断是否为登录状态
if (userid == null) {
goods.setFav(false);
} else {
Favorite favorite = goodsService.selectFavByKey(new FavoriteKey(userid, goods.getGoodsid()));
if (favorite == null) {
goods.setFav(false);
} else {
goods.setFav(true);
}
}
List<ImagePath> imagePathList = goodsService.findImagePath(goods.getGoodsid());
goods.setImagePaths(imagePathList);
goodsAndImage.add(goods);
}
return goodsAndImage;
}
}