导入taotao-order
表结构
订单表:
订单商品表:
疑问:为什么要冗余存储商品的基本信息?
订单物流表(收货人信息):
开放平台
http://open.taobao.com/doc2/apiDetail?spm=0.0.0.0.MiNiKt&apiId=47&docType=:
配置tomcat插件
配置nginx
配置hosts
接口文档
接口
创建订单接口
使用:
如何生成订单号
订单号需求是什么?
1、 唯一
2、 可读性高
a) 纯数字
3、 长度
a) 不长于20位
如何生成:
1、 时间戳
a) 可能重复
2、 手机号
3、 时间戳 + 随机数
a) 可能重复
4、 时间戳 + 自增id
a) 可行 (使用Redis的INCR命令完成)
5、 用户id + 时间戳
6、 用户id + 店铺id + 时间戳
a) 可行,适用于C2C、B2C平台
具体实现
Controller: 接收提交的json数据
Service:将json反序列化为Order对象,对Order对象做校验,生成订单ID:
OrderDAO是一个接口的实现类,后期如果对订单数据的存储做改造时,保存订单就会有多套实现:
通过配置将OrderDAO加入到Spring容器:
使用配置文件配置bean,后期的修改更加灵活。
Mapper.xml:
一个statement执行多条SQL语句,默认情况下支持吗? – 不支持的。
在连接字符串中设置:
是在同一个事务中吗? – 是的。
但是,现在的代码中存在一个bug,事务不会回滚,原因:Service中的异常被捕获:
根据订单ID查询订单
测试
Controller
Service
OrderDAO
OrderMapper
OrderMapper.xml
通过延迟加载实现订单商品和订单物流数据的加载:
开启延迟加载:
根据用户名分页查询订单
测试
Controller
Service
OrderDAO
Mapp.xml:(使用延迟加载)
分页插件的配置
修改订单状态
测试
Controller
Service
将json反序列化成Order对象:
OrderDAO
设置更新时间:
OrderMapper.xml
这是一个通用的更新实现
<update id="update">
UPDATE <include refid="tableName"/>
<set>
<if test="payment !=null and payment != ''">
payment = #{payment},
</if>
<if test="postFee !=null and postFee != ''">
post_fee = #{postFee},
</if>
<if test="status !=null and status != ''">
status = #{status},
</if>
<if test="updateTime !=null and updateTime != ''">
update_time = #{updateTime},
</if>
<if test="paymentTime !=null and paymentTime != ''">
payment_time = #{paymentTime},
</if>
<if test="consignTime !=null and consignTime != ''">
consign_time = #{consignTime},
</if>
<if test="endTime !=null and endTime != ''">
end_time = #{endTime},
</if>
<if test="closeTime !=null and closeTime != ''">
close_time = #{closeTime},
</if>
<if test="shippingName !=null and shippingName != ''">
shipping_name = #{shippingName},
</if>
<if test="shippingCode !=null and shippingCode != ''">
shipping_code = #{shippingCode},
</if>
<if test="buyerMessage !=null and buyerMessage != ''">
buyer_message = #{buyerMessage},
</if>
<if test="buyerRate !=null and buyerRate != ''">
buyer_rate = #{buyerRate},
</if>
</set>
WHERE order_id = #{orderId};
</update>