CREATE TABLE t_order (
oid INT AUTO_INCREMENT COMMENT '订单id',
uid INT NOT NULL COMMENT '用户id',
recv_name VARCHAR(20) NOT NULL COMMENT '收货人姓名',
recv_phone VARCHAR(20) COMMENT '收货人电话',
recv_province VARCHAR(15) COMMENT '收货人所在省',
recv_city VARCHAR(15) COMMENT '收货人所在市',
recv_area VARCHAR(15) COMMENT '收货人所在区',
recv_address VARCHAR(50) COMMENT '收货详细地址',
total_price BIGINT COMMENT '总价',
status INT COMMENT '状态:0-未支付,1-已支付,2-已取消,3-已关闭,4-已完成',
order_time DATETIME COMMENT '下单时间',
pay_time DATETIME COMMENT '支付时间',
created_user VARCHAR(20) COMMENT '创建人',
created_time DATETIME COMMENT '创建时间',
modified_user VARCHAR(20) COMMENT '修改人',
modified_time DATETIME COMMENT '修改时间',
PRIMARY KEY (oid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE t_order_item (
id INT AUTO_INCREMENT COMMENT '订单中的商品记录的id',
oid INT NOT NULL COMMENT '所归属的订单的id',
pid INT NOT NULL COMMENT '商品的id',
title VARCHAR(100) NOT NULL COMMENT '商品标题',
image VARCHAR(500) COMMENT '商品图片',
price BIGINT COMMENT '商品价格',
num INT COMMENT '购买数量',
created_user VARCHAR(20) COMMENT '创建人',
created_time DATETIME COMMENT '创建时间',
modified_user VARCHAR(20) COMMENT '修改人',
modified_time DATETIME COMMENT '修改时间',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
public class Order extends BaseEntity implements Serializable {
private Integer oid;
private Integer uid;
private String recvName;
private String recvPhone;
private String recvProvince;
private String recvCity;
private String recvArea;
private String recvAddress;
private Long totalPrice;
private Integer status;
private Date orderTime;
private Date payTime;
public class OrderItem extends BaseEntity implements Serializable {
private Integer id;
private Integer oid;
private Integer pid;
private String title;
private String image;
private Long price;
private Integer num;
1.将数据插入到订单表中
insert into t_order(oid除外所有的字段) values(字段的值)
2.将数据插入到订单项表中
insert into t_order_item(oid除外所有的字段) values(字段的值)
创建一个OrderMapper接口,接口中添加以上两个SQL对象的抽象方法。
/** 订单的持久层接口**/
public interface OrderMapper {
/**
* 插入订单数据
* @param order 订单数据
* @return 受影响的行数
*/
Integer insertOrder(Order order);
/**
* 插入订单项的数据
* @param orderItem 订单项数据
* @return 受影响的行数
*/
Integer insertOrderItem(OrderItem orderItem)
配置OrderMapper.xml的映射文件。
insert into t_order (
uid, recv_name, recv_phone, recv_province, recv_city, recv_area, recv_address,
total_price,status, order_time, pay_time, created_user, created_time, modified_user,
modified_time
) values (
#{uid}, #{recvName}, #{recvPhone}, #{recvProvince}, #{recvCity}, #{recvArea},
#{recvAddress}, #{totalPrice}, #{status}, #{orderTime}, #{payTime}, #{createdUser},
#{createdTime}, #{modifiedUser}, #{modifiedTime}
)
insert into t_order_item (
oid, pid, title, image, price, num, created_user,
created_time, modified_user, modified_time
) values (
#{oid}, #{pid}, #{title}, #{image}, #{price}, #{num}, #{createdUser},
#{createdTime}, #{modifiedUser}, #{modifiedTime}
)
测试类
public class OrderMapperTests {
@Autowired
private OrderMapper orderMapper;
@Test
public void insertOrder() {
Order order = new Order();
order.setUid(9);
order.setRecvName("动笔");
order.setRecvPhone("45454545");
orderMapper.insertOrder(order);
}
@Test
public void insertOrderItem() {
OrderItem orderItem = new OrderItem();
orderItem.setOid(1);
orderItem.setPid(10000001);
orderItem.setTitle("广博(GuangBo)10本装40张A5牛皮纸记事本子日记本办公软抄本GBR0731");
orderMapper.insertOrderItem(orderItem);
}
1.在IAddressService接口中定义根据收货地址id获取收货地址数据。
Address getByAid(Integer aid);
2.在子类中实现该抽象方法。
@Override
public Address getByAid(Integer aid,Integer uid) {
Address address = addressMapper.findByAid(aid);
if(address==null){
throw new AddressNotFoundException("找不到地址");
}
if(address.getUid().equals(uid)){
throw new AccessDeniedException("非法数据访问");
}
address.setProvinceCode(null);
address.setCityCode(null);
address.setAreaCode(null);
address.setCreatedUser(null);
address.setCreatedTime(null);
address.setModifiedUser(null);
address.setModifiedTime(null);
return address;
}
3.在service包下创建IOrderService接口添加抽象方法用于创建订单
public interface IOrderService { Order create(Integer aid,Integer uid,String username,Integer[] cids); }
4 创建实现类OrderServiceImpl
package com.cy.store.service.impl;
import com.cy.store.entity.Address;
import com.cy.store.entity.Order;
import com.cy.store.mapper.OrderMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.ICartService;
import com.cy.store.service.IOrderService;
import com.cy.store.service.ex.InsertException;
import com.cy.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service
public class OrderServiceImpl implements IOrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private IAddressService addressService;
@Autowired
private ICartService cartService;
@Override
public Order create(Integer aid, Integer uid, String username, Integer[] cids) {
Listlist= cartService.getVoByCid(cids, uid);
//计算总价
Long totalPrice=0L;
for (CartVo cartVo : list) {
totalPrice+= cartVo.getRealPrice() * cartVo.getNum();
}
Address address = addressService.getByAid(aid, uid);
Order order = new Order();
order.setUid(uid);
order.setRecvName(address.getName());
order.setRecvPhone(address.getPhone());
order.setRecvProvince(address.getProvinceName());
order.setRecvCity(address.getCityName());
order.setRecvArea(address.getAreaName());
order.setRecvAddress(address.getAddress());
//支付,总价
order.setStatus(0);
order.setTotalPrice(totalPrice);
order.setOrderTime(new Date());
//日志
order.setCreatedUser(username);
order.setCreatedTime(new Date());
order.setModifiedUser(username);
order.setModifiedTime(new Date());
//查询数据
Integer rows = orderMapper.insertOrder(order);
if(rows !=1){
throw new InsertException("插入异常");
}
return order;
}
}
1.设计请求:
/orders/create
aid,session,cids
post
jsonresult
2 创建OrderController类,并编写请求处理方法。
@RequestMapping("orders")
@RestController
public class OrderController extends BaseController{
@Autowired
private IOrderService orderService;
@RequestMapping("create")
public JsonResult create(Integer aid, Integer[] cids, HttpSession session){
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
Order data = orderService.create(aid, uid, username, cids);
return new JsonResult<>(OK,data);
}
}
在订单确定页面中添加发送请求的处理方法。
$("#btn-create-order").click(function () {
let aid= $("#address-list").val(); //12
let cids= location.search.substr(1); //cids=7&cids=8
$.ajax(
{
url:"/orders/create",
type:"POST",
data:"aid="+aid+"&"+cids,
dataType:"JSON",
success:function (json) {
if(json.state==200){
alert("订单创建成功");
location.href="payment.html"
console.log(json.data)
}
},
error:function (xhr) {
alert("订单数据加载产生未知异常"+xhr.status)
}
}
);
});