e3mall项目:订单系统
准备工作:创建子工程:e3-order,并在其下创建e3-order-interface、e3-order-service。创建子工程:e3-order-web,并导入相关静态资源。(需要书写相关配置,参考之前的项目进行配置)
包结构如下:
![](https://i-blog.csdnimg.cn/blog_migrate/ba1df7fbdb26d14db01c2c4bee87f549.png)
一、服务层相关代码(e3-order-interface以及e3-order-service)
package cn.e3mall.order.service;
import cn.e3mall.order.entity.OrderInfo;
/**
* 订单系统 service层
* Author: xushuai
* Date: 2018/5/31
* Time: 14:45
* Description:
*/
public interface OrderService {
/**
* 创建订单
* @auther: xushuai
* @date: 2018/5/31 14:45
* @return:
* @throws:
*/
Long create(OrderInfo orderInfo);
}
package cn.e3mall.order.service.impl;
import cn.e3mall.common.redis.JedisClient;
import cn.e3mall.dao.TbOrderItemMapper;
import cn.e3mall.dao.TbOrderMapper;
import cn.e3mall.dao.TbOrderShippingMapper;
import cn.e3mall.entity.TbOrderItem;
import cn.e3mall.entity.TbOrderShipping;
import cn.e3mall.order.entity.OrderInfo;
import cn.e3mall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* 订单系统 service层实现
* Author: xushuai
* Date: 2018/5/31
* Time: 14:46
* Description:
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private JedisClient jedisClient;
@Autowired
private TbOrderMapper orderMapper;
@Autowired
private TbOrderItemMapper orderItemMapper;
@Autowired
private TbOrderShippingMapper orderShippingMapper;
//orderId的key
@Value("${redis.orderId}")
private String ORDER_ID;
//orderItemId的key
@Value("${redis.orderItemId}")
private String ORDER_ITEM_ID;
//orderId的初始值
@Value("${redis.orderStart}")
private String ORDER_START;
@Override
public Long create(OrderInfo orderInfo) {
//生成订单ID
if(!jedisClient.exists(ORDER_ID)){
//如果订单ID不存在,添加订单ID的值
jedisClient.set(ORDER_ID,ORDER_START);
}
//存在,进行自增
Long orderId = jedisClient.incr(ORDER_ID);
//补全订单信息
orderInfo.setCreateTime(new Date());
orderInfo.setUpdateTime(new Date());
orderInfo.setOrderId(String.valueOf(orderId));
//设置订单状态:1、未付款,2、已付款,3、未发货,4、已发货,5、交易成功,6、交易关闭
orderInfo.setStatus(1);
//保存订单表数据
orderMapper.insert(orderInfo);
//生成并保存订单明细数据
addOrderItem(orderInfo.getOrderItems(),orderId.toString());
//生成并保存物流信息
addOrderShipping(orderInfo.getOrderShipping(),orderId.toString());
//返回订单号
return orderId;
}
/*
* 生成并保存物流信息
*/
private void addOrderShipping(TbOrderShipping orderShipping, String orderId) {
//补全数据
orderShipping.setCreated(new Date());
orderShipping.setUpdated(new Date());
orderShipping.setOrderId(orderId);
//执行保存
orderShippingMapper.insert(orderShipping);
}
/*
* 生成并保存订单明细
*/
private void addOrderItem(List<TbOrderItem> orderItems,String orderId) {
//遍历列表
for(TbOrderItem orderItem : orderItems){
//补全orderItem信息
orderItem.setId(jedisClient.incr(ORDER_ITEM_ID).toString());
orderItem.setOrderId(orderId);
//执行保存
orderItemMapper.insert(orderItem);
}
}
}
resource.properties配置
## redis单机版连接信息
redis.host=192.168.25.130
redis.port=6379
## redis中订单ID的key
redis.orderId=ORDER_ID
## redis中订单ID的key
redis.orderItemId=ORDER_ITEM_ID
## redis中订单ID 初始值
redis.orderStart=1234143
二、表现层相关代码(e3-order-web)
OrderController
package cn.e3mall.order.controller;
import cn.e3mall.cart.service.CartService;
import cn.e3mall.entity.TbItem;
import cn.e3mall.entity.TbUser;
import cn.e3mall.order.entity.OrderInfo;
import cn.e3mall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* 页面跳转 web层
* Author: xushuai
* Date: 2018/5/31
* Time: 12:16
* Description:
*/
@Controller
public class OrderController {
@Autowired
private CartService cartService;
@Autowired
private OrderService orderService;
/**
* 跳转到订单结算页面
* @auther: xushuai
* @date: 2018/5/31 12:29
* @return: String 订单结算页面
*/
@RequestMapping("/order/order-cart")
public String toPay(HttpServletRequest request) {
//获取用户信息
TbUser user = (TbUser) request.getAttribute("user");
//获取用户购物车商品列表
List<TbItem> itemList = cartService.getItemListFromRedis(user.getId());
//保存商品列表
request.setAttribute("cartList",itemList);
return "order-cart";
}
/**
*
* @auther: xushuai
* @date: 2018/5/31 15:12
* @return:
* @throws:
*/
@RequestMapping(value = "/order/create",method = RequestMethod.POST)
public String createOrder(OrderInfo orderInfo,HttpServletRequest request){
//获取当前登录用户信息
TbUser user = (TbUser) request.getAttribute("user");
//补全部分数据
orderInfo.setBuyerNick(user.getUsername());
//调用服务保存订单信息
Long orderId = orderService.create(orderInfo);
//订单提交成功,清空购物车
cartService.clear(user.getId());
//将相关回显数据保存
request.setAttribute("orderId",orderId);
request.setAttribute("payment",orderInfo.getPayment());
//跳转到成功页面
return "success";
}
}
LoginInterceptor
package cn.e3mall.order.interceptor;
import cn.e3mall.cart.service.CartService;
import cn.e3mall.common.entity.E3Result;
import cn.e3mall.common.utils.CookieUtils;
import cn.e3mall.common.utils.JsonUtils;
import cn.e3mall.entity.TbItem;
import cn.e3mall.entity.TbUser;
import cn.e3mall.sso.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 登录校验拦截器
* Author: xushuai
* Date: 2018/5/31
* Time: 12:42
* Description:
*/
public class LoginInterceptor implements HandlerInterceptor {
@Value("${redirect.loginURL}")
private String loginURL;
@Autowired
private UserService userService;
@Autowired
private CartService cartService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//登录判断
//从cookie中取出token
String token = CookieUtils.getCookieValue(request, "token");
if(StringUtils.isBlank(token)){
//token为空,重定向到登录页面
response.sendRedirect(loginURL + "?r=" + request.getRequestURL());
return false;
}
//使用token获取当前用户信息
E3Result e3Result = userService.getLoginUser(token);
//判断是否为登录状态
if(e3Result.getStatus() != 200){
//token为空,重定向到登录页面
response.sendRedirect(loginURL + "r=" + request.getRequestURL());
return false;
}
//获取user对象
TbUser user = (TbUser) e3Result.getData();
//获取cookie中的商品数据
String cart = CookieUtils.getCookieValue(request, "CART", true);
if(StringUtils.isNotBlank(cart)){
cartService.addProductFromCookie(JsonUtils.jsonToList(cart, TbItem.class),user.getId());
}
//将user放入request中
request.setAttribute("user",user);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
}
}
注意:记得发布服务,引用服务以及配置拦截器。