订单表设计
创建订单项目
创建项目
添加继承依赖
com.jt
jt-common
1.0-SNAPSHOT
org.springframework.boot
spring-boot-maven-plugin
添加POJO
构建order项目
订单页面跳转
url分析
编辑OrderController
package com.jt.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.pojo.Order;
import com.jt.service.DubboCartService;
import com.jt.service.DubboOrderService;
import com.jt.theardLocal.UserTheardLocal;
import com.jt.vo.SysResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/order")
public class OrderController {
@Reference(timeout = 3000,check = false)
private DubboOrderService orderService;
@Reference(timeout = 3000,check = false)
private DubboCartService cartService;
/**
* 订单页面跳转到确认页面
* url:http://www.jt.com/order/create.html
* 页面的取值:${carts}
* */ @RequestMapping("/create")
public String create(Model model){
//根据userId查询购物车信息】
Long userId= UserTheardLocal.get().getId();
List cartList = cartService.findCartListUserId(userId);
//通过model将数据带到页面中去
model.addAttribute("carts",cartList);
//跳转到订单确认页面
return "order-cart";
}
编辑OrderService
@Override
public List findCartListByUserId(Long userId) {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", userId);
return cartMapper.selectList(queryWrapper);
}
页面效果展现
关于SpringMVC参数提交问题说明
简单参数传参问题
1.页面url标识
2.Controller中的方法
public void xxxx(User user){
}
public class User{
private Integer name;
private String age;
}
使用对象的引用为参数赋值
属性提交问题....
解决思路:可采用对象引用方式为属性赋值。
Controller中的方法
public void xxx(User user){
}
public class Dog{
private String name;
private Integer age;
}
public class User{
private String name;
private Integer age;
private Dog dog;
}
关于订单提交
页面url说明
请求参数
页面JS解析
jQuery.ajax( {
type : "POST",
dataType : "json",
url : "/order/submit",
data : $("#orderForm").serialize(),
// data: {"key":"value","key2":"value2".....}
// data: id=1&name="xxx"&age=18......
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();
}
});
编辑OrderController
/**
* 订单提交
* url:http://www.jt.com/order/submit
* 参数:整个form表单
* 返回值:SysResult对象 需要携带返回值orderId
* 业务说明:
* 当订单入库之后,需要返回orderId,让用户查询订单信息和状态
*/
@RequestMapping("/submit")
@ResponseBody
public SysResult saveOrder(Order order){
//获取用户的编号
Long userId=UserTheardLocal.get().getId();
order.setUserId(userId);
String orderId=orderService.saveOrder(order);
//判断orderId是否为空
if(StringUtils.isEmpty(orderId)){
return SysResult.fail();
}else{
//如果执行成功将orderId返回给用户
return SysResult.success(orderId);
}
}
编辑OrderService
package com.jt.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.OrderItemMapper;
import com.jt.mapper.OrderMapper;
import com.jt.mapper.OrderShippingMapper;
import com.jt.pojo.Order;
import com.jt.pojo.OrderItem;
import com.jt.pojo.OrderShipping;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Service(timeout = 3000)
public class DubboOrderServiceImpl implements DubboOrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderShippingMapper orderShippingMapper;
@Autowired
private OrderItemMapper orderItemMapper;
/**
* Order 分为三大块:1、order订单本身,2、order物流信息,3、order商品信息
* 需要操作3张表完成入库操作
* 主键信息:OrderId
* @param order
* @return
*/
@Override
public String saveOrder(Order order) {
//拼接orderId
String orderId=
""+order.getUserId()+System.currentTimeMillis();
//1、完成订单入库
order.setOrderId(orderId)//将orderId存到order对象中
.setStatus(1);//设置订单提交的状态
orderMapper.insert(order);//将订单入库
//2、完成订单物流入库
OrderShipping orderShipping= order.getOrderShipping();//从order中先取出OrderShipping对象
orderShipping.setOrderId(orderId);//将查物流的orderId存到物流对象中
//直接入库
orderShippingMapper.insert(orderShipping);
//3、完成订单商品的入库
List orderItems = order.getOrderItems();
//批量入库操作的sql:
//insert into xxx(xxx,xx,xx)values (xxx,xx,x),(xxx,xx,x).....
for(OrderItem orderItem:orderItems){
orderItem.setOrderId(orderId);//将订单id存到订单信息中
orderItemMapper.insert(orderItem);//完成订单入库
}
System.out.println("订单入库成功!!!");
return orderId;
}
订单成功跳转
页面url分析
编辑OrderController
/**
* 实现订单提交成功的页面(商品的查询)
* url:http://www.jt.com/order/success.html?id=101605796286970
* 参数说明:orderId订单编号
* 返回值类型:success.html
* 页面的取值方式:${order.orderId}
*/
@RequestMapping("/success")
public String findOrderById(String id,Model model){
//根据id查询订单商品信息
Order order=orderService.findOrderById(id);
//通过model对象将数据传到页面
model.addAttribute("order",order);
return "success";
}
编辑OrderService
/**
* 实现订单提交成功的页面(商品的查询)
* url:http://www.jt.com/order/success.html?id=101605796286970
* 参数说明:orderId订单编号
* 返回值类型:success.html
* 页面的取值方式:${order.orderId}
*/@Override
public Order findOrderById(String id) {
//1.查询订单信息
Order order=orderMapper.selectById(id);
//查询订单物流信息
OrderShipping orderShipping=orderShippingMapper.selectById(id);
//查询订单商品,因为一个订单下可以有多个商品所以用selectList
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.eq("order_id", id);
List lists=orderItemMapper.selectList(queryWrapper);
return order.setOrderShipping(orderShipping).setOrderItems(lists);
}