说明:只要当一个订单生成时,处理订单的信息之外,还有订单中包含的订单商品数据以及订单物流信息,而订单的信息封装在“tb_order”表中,关于tb_order表结构如下图所示:
订单商品数据封装在“tb_order_item”表结构如图所示:
说明:tb_order表和tb_order_item表的关系是一对多的关系,因为一个订单中可以包含多个商品。订单物流数据封装“tb_order_shipping”表,表结构如图所示:
说明:tb_order表和tb_order_shipping表示一对一的关系。因为一个订单只会对应一条物流信息。
第一步:创建jt-order系统
01.创建jt-order系统
02.编辑jt-order项目的pom.xml文件
从jt-sso中的pom.xml文件中复制一个插件
org.apache.maven.plugins
maven-surefire-plugin
true
org.springframework.boot
spring-boot-maven-plugin
03.导入资源
说明:将课程资料中的day18—src导入到自己创建的jt-order项目中。
04.创建pojo对象
order:
package com.jt.pojo;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@TableName("tb_order")
@Data
@Accessors(chain=true)
public class Order extends BasePojo{
@TableField(exist=false) //入库操作忽略该字段
private OrderShipping orderShipping;
//封装订单商品信息 一对多
@TableField(exist=false) //入库操作忽略该字段
private List orderItems;
@TableId
private String orderId;
private String payment;
private Integer paymentType;
private String postFee;
private Integer status;
private Date paymentTime;
private Date consignTime;
private Date endTime;
private Date closeTime;
private String shippingName;
private String shippingCode;
private Long userId;
private String buyerMessage;
private String buyerNick;
private Integer buyerRate;
}
OrderItem:
package com.jt.pojo;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@TableName("tb_order_item")
@Data
@Accessors(chain=true)
public class OrderItem extends BasePojo{
@TableId
private String itemId;
@TableId
private String orderId;
private Integer num;
private String title;
private Long price;
private Long totalFee;
private String picPath;
}
ordershipping
package com.jt.pojo;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@TableName("tb_order_shipping")
@Data
@Accessors(chain=true)
public class OrderShipping extends BasePojo{
@TableId
private String orderId;
private String receiverName;
private String receiverPhone;
private String receiverMobile;
private String receiverState;
private String receiverCity;
private String receiverDistrict;
private String receiverAddress;
private String receiverZip;
}
05.创建独立接口DubboOrderService
说明:在jt-common项目下的com.jt.service包下创建DubboOrderService接口
代码如下:
package com.jt.service;
public interface DubboOrderService {
}
06.修改DubboOrderServiceImpl实现类,代码如下:
@Service
public class DubboOrderServiceImpl implements DubboOrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderShippingMapper orderShippingMapper;
@Autowired
private OrderItemMapper orderItemMapper;
}
将jt-order项目下的redis.properties文件删掉,如图所示:
07.修改jt-order项目下的application.yml文件
server:
port: 8095
servlet:
context-path: /
spring:
datasource:
#引入druid数据源
#type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jtdb?
serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true
&allowMultiQueries=true
username: root
password: root
#mybatis-plush配置
mybatis-plus:
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mybatis/mappers/*.xml
configuration:
map-underscore-to-camel-case: true
#引入日志信息
logging:
level:
com.jt.mapper: debug
#dubbo配置
dubbo:
scan:
basePackages: com.jt
application:
name: provider-order
registry:
address: zookeeper://192.168.126.129:2181?
backup=192.168.126.129:2182,192.168.126.129:2183
protocol:
name: dubbo
port: 20882
第二步:订单提交业务
01.页面url分析
02.页面js(order.js)分析
function submit_Order() {
$("#submit_message").hide();
jQuery.ajax( {
type : "POST",
dataType : "json",
url : "/order/submit",
//获取整个表单数据
data : $("#orderForm").serialize(),
cache : false,
success : function(result) {
if(result.status == 200){
location.href = "/order/success.html?id="+result.data;
}else{
$("#submit_message").html("订单提交失败,请稍后重试...").show();
}
},
error : function(error) {
$("#submit_message").html("亲爱的用户请不要频繁点击, 请稍后重
试...").show();
}
});
}
05.订单入库业务实现
第一步:编辑jt-web的OrderController
@Controller
@RequestMapping("/order")
public class OrderController {
@Reference(check = false)
private DubboCartService cartService;
@Reference(check = false)
private DubboOrderService orderService;
/**
* 实现订单入库功能
* url:http://www.jt.com/order/submit
* 参数:表单数据,使用对象接
* 返回值:SysResult对象包含orderId(后期需要基于orderId实现订单信息的回显)
*/
@RequestMapping("/submit")
@ResponseBody
public SysResult saveOrder(Order order,HttpServletRequest request) {
//动态获取userId
User user = (User)request.getAttribute("JT_USER");
Long userId=user.getId();
order.setUserId(userId);
String orderId = orderService.saveOrder(order);
return SysResult.success(orderId);
}
}
第二步:编辑DubboOrderService独立接口
public interface DubboOrderService {
String saveOrder(Order order);
}
第三步:编辑DubboOrderServiceImpl
/**
* 业务分析:
* 表单数据提交
* 表单数据包含订单信息,订单商品信息以及订单物流信息
* 所以这三部分数据需要同时在三张表中实现入库
* orderId=用户登录id+当前时间戳
*/
@Override
public String saveOrder(Order order) {
//1.动态生成orderId
String orderId=""+order.getUserId()+System.currentTimeMillis();
//2.完成订单信息,订单商品信息以及订单物流信息数据的入库
//2.1订单入库
Date date=new Date();
order.setOrderId(orderId).setStatus(1).setCreated(date).setUpdated(date);
orderMapper.insert(order);
System.out.println("订单入库成功!");
//2.2订单商品信息的入库
List orderItems = order.getOrderItems();
for (OrderItem orderItem : orderItems) {
orderItem.setOrderId(orderId).setCreated(date).setUpdated(date);
orderItemMapper.insert(orderItem);
}
System.out.println("订单商品信息入库成功!");
//2.3订单物流信息的入库
OrderShipping orderShipping = order.getOrderShipping();
orderShipping.setOrderId(orderId).setCreated(date).setUpdated(date);
orderShippingMapper.insert(orderShipping);
System.out.println("订单物流信息入库成功");
return orderId;
}
06.订单回显业务实现
第一步:页面url分析
第二步:页面JS分析
-
订单号:${order.orderId}
货到付款:${order.payment}元
京淘快递 送货时间: 预计 ${date} 送达
您的订单已经在处理中,发货后订单内容会显示承运人联系方式,
如有必要您可以联系对方
第三步:编辑jt-web的OrderController
@Controller
@RequestMapping("/order")
public class OrderController {
@Reference(check = false)
private DubboCartService cartService;
@Reference(check = false)
private DubboOrderService orderService;
/**
* 需求:根据orderId查询订单信息 3张表查询
* url:http://www.jt.com/order/success.html?id=121622786609949
* 参数:orderId
* 返回值:具体页面信息
*/
@RequestMapping("/success")
public String success(@RequestParam("id")String orderId,Model model) {
//1.根据orderId查询order对象信息
Order order=orderService.findOrderById(orderId);
model.addAttribute("order", order);
return "success";
}
}
第四步:编辑DubboOrderService接口
public interface DubboOrderService {
Order findOrderById(String orderId);
}
第五步:
编辑DubboOrderServiceImpl实现类
@Override
public Order findOrderById(String orderId) {
//1.根据orderId查询订单信息
Order order = orderMapper.selectById(orderId);
//对订单的创建时间进行重新的封装(+3天),这个是预测货单到达时间
Calendar calender=Calendar.getInstance();
calender.setTime(order.getCreated());
calender.add(Calendar.DAY_OF_MONTH, 3);//+3天
order.setCreated(calender.getTime());
//2.根据订单id查询订单物流信息
OrderShipping shipping = orderShippingMapper.selectById(orderId);
//3.根据订单id查询订单商品信息
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.eq("order_id", orderId);
List items = orderItemMapper.selectList(queryWrapper);
//4.将商品信息和物流信息封装到订单对象中
order.setOrderItems(items).setOrderShipping(shipping);
return order;
}