用户订单表:
订单详情表:
思路分析:首先我们需要知道当点击了确认订单按钮后,需要向后端传递哪些数据,先看用户订单表:ORDER_ID是不需要传的,这个可以在后台生成就行了;USER_ID是需要传的;ADDRESS_ID也是需要传的;ORDER_TIME可以在后台获得系统的时间,也是不需要传的;GET_TIME不需要;TOTAL总价是需要传的;NOTE备注也是需要传的。
再看订单详情表:ORDER_DETAIL_ID是自增的,不需要;ORDER_ID也是不需要的;ITEM_ID是需要的;BUY_COUNT也是需要的
1. 提交表单需要传递的数据
补充:再上一篇博客中讲述了如何选择其他地址并渲染到页面上,此时我们在这边的,需要传递ADDRESS_ID, 所以当我们选择其他地址时,同时也是需要将ADDRESS_ID也跟着改变成相应的,不然提交表单时,会传错误的ADDRESS_ID
html
<input id="formAddressId" name="addressId" type="hidden" th:value="${defaultAddress.addressId}"/>
将需要传递的数据都放入表单中,如果不需要展示,直接设为隐藏就行了
2. 后端接收数据并执行插入操作
我们需要向两个表中插入数据,所以要写两个mapper
UserOrderMapper.java
java
public interface UserOrderMapper {
// 新增订单信息
int insertUserOrder(UserOrder userOrder);
}
UserOrderMapper.xml
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.shopping.mapper.UserOrderMapper">
<resultMap id="userOrderBaseResultMap" type="UserOrder">
<id column="ORDER_ID" property="orderId"/>
<result column="USER_ID" property="userId"/>
<result column="ADDRESS_ID" property="addressId"/>
<result column="ORDER_TIME" property="orderTime"/>
<result column="GET_TIME" property="getTime"/>
<result column="TOTAL" property="total"/>
<result column="NOTE" property="note"/>
</resultMap>
<!--insertUserOrder(UserOrder userOrder);-->
<insert id="insertUserOrder" parameterType="UserOrder">
INSERT INTO userorder (ORDER_ID,USER_ID,ADDRESS_ID,ORDER_TIME,GET_TIME,TOTAL,NOTE)
VALUES (#{orderId},#{userId},#{addressId},#{orderTime},#{getTime},#{total},#{note})
</insert>
</mapper>
OrderDetailMapper.java
java
public interface OrderDetailMapper {
//插入订单详情
int insertOrderDetail(OrderDetail orderDetail);
}
OrderDetailMapper.xml
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.shopping.mapper.OrderDetailMapper">
<resultMap id="baseOrderDetailResultMap" type="Address">
<id column="ORDER_DETAIL_ID" property="orderDetailId"/>
<result column="ORDER_ID" property="orderId"/>
<result column="ITEM_ID" property="itemId"/>
<result column="BUY_COUNT" property="buyCount"/>
</resultMap>
<!--insertOrderDetail(OrderDetail orderDetail);-->
<insert id="insertOrderDetail" parameterType="OrderDetail">
INSERT INTO orderdetail (ORDER_ID,ITEM_ID,BUY_COUNT) VALUES (#{orderId},#{itemId},#{buyCount})
</insert>
</mapper>
UserOrderService.java 接口
使其的返回类型时ResultDTO
java
public interface UserOrderService {
// 直接购买确认订单功能
ResultDTO<UserOrder> confirmOrder(UserOrder userOrder, OrderDetail orderDetail);
}
UserOrderServiceImpl.java 实现类
java
public class UserOrderServiceImpl implements UserOrderService {
@Resource
UserOrderMapper userOrderMapper;
@Resource
OrderDetailMapper orderDetailMapper;
@Override
public ResultDTO<UserOrder> confirmOrder(UserOrder userOrder, OrderDetail orderDetail) {
ResultDTO<UserOrder> userOrderResultDTO = new ResultDTO<>();
// 判断用户是否登录
if (0!= userOrder.getUserId()) {
int orderId = (int)(Math.random() * 1234567890); // 生成随机的订单编号
userOrder.setOrderId(orderId);
userOrder.setOrderTime(new Timestamp(System.currentTimeMillis())); // 将当前系统的时间直接填入
int orderSubmit = userOrderMapper.insertUserOrder(userOrder);
if (orderSubmit > 0) {
// 订单插入成功,才能进行订单详情插入
orderDetail.setOrderId(orderId);
int orderDetailSubmit = orderDetailMapper.insertOrderDetail(orderDetail);
if (orderDetailSubmit > 0) {
userOrderResultDTO.setCode(200);
userOrderResultDTO.setMsg("下单成功!");
userOrderResultDTO.setData(userOrder);
} else {
userOrderResultDTO.setCode(500);
userOrderResultDTO.setMsg("下单失败!");
userOrderResultDTO.setData(null);
}
}
} else {
userOrderResultDTO.setCode(500);
userOrderResultDTO.setMsg("下单失败!");
userOrderResultDTO.setData(null);
}
return userOrderResultDTO;
}
}
UserOrderController.java
java
@RequestMapping("/confirmOrder")
@ResponseBody
// http://localhost:8082/project/order/confirmOrder
public ResultDTO confirmOrder(UserOrder userOrder, OrderDetail orderDetail) throws Exception {
// System.out.println("userOrder:" + userOrder);
// System.out.println("orderDetail:" + orderDetail);
ResultDTO<UserOrder> confirmOrderResult = userOrderService.confirmOrder(userOrder, orderDetail);
return confirmOrderResult;
}
这边是直接以ResultDTO的形式返回的,也可以使用ModelAndView进行数据的返回