规划SQL语句
//更新t_cart表num值(加入购物车功能已有)
//判断商品信息是否存在
select * from t_cart where cid=#{cid}
设计接口和抽象方法
Cart findCartByCid(Integer cid);
编写xml映射文件
设计接口和抽象方法
Integer addNum(Integer cid,Integer uid,String username);
实现接口和抽象方法
@Override
public Integer addNum(Integer cid, Integer uid, String username) {
Cart cart = cartMapper.findCartByCid(cid);
if (cart == null){
throw new CartNotFoundException("购物车商品信息不存在");
}
Integer num = cart.getNum()+1;
Integer integer = cartMapper.updateCart(cid, num, username, new Date());
if (integer != 1){
throw new UpdateException("增加购物车商品数量时发生异常");
}
return num;
}
设计请求
/url/{cid}/num/add
post
Integer cid,HttpSession session
JsonResult<Integer>
实现请求
@RequestMapping("{cid}/add/num")
public JsonResult addNum(Integer cid,HttpSession session){
Integer data = cartService.addNum(cid, getUidFromSession(session), getUsernameFromSession(session));
return new JsonResult<>(OK,data);
}
规划SQL语句
select
cid,uid,pid,
t_cart.price,t_cart.num,
t_product.title,t_product.image,
t_product.price as realPrice
from
t_cart left join t_product on t_cart.pid=t_product.id
where
cid in (?,?,?)
order by
t_cart.craeted_time desc
设计接口和抽象方法
List findOVByCid(Integer[] cid);
编写xml映射
设计接口和抽象方法
List getOVByCid(Integer uid,Integer[] cid);
实现接口和方法
@Override
public List getOVByCid(Integer uid, Integer[] cid) {
List list = cartMapper.findOVByCid(cid);
Iterator it = list.iterator();
while (it.hasNext()){
CartVO cartVO = it.next();
if (!cartVO.getUid().equals(uid)){
list.remove(cartVO);
}
}
return list;
}
设计请求
/carts/list
POST
Integer[] cid,HttpSession session
JsonResult>
实现请求
@RequestMapping("list")
public JsonResult> getCartList(Integer[] cid, HttpSession session){
List data = cartService.getOVByCid(getUidFromSession(session), cid);
return new JsonResult<>(OK,data);
}
//orderConfirm.html
<script type="text/javascript">
$(document).ready(function () {
getCartOV();
});
function getCartOV(){
$("#cart-list").empty();
console.log(location.search.substr(1));
$.ajax({
url: "/carts/list",
//截取url地址信息,1表示?之后,0表示?之前
data: location.search.substr(1),
type: "GET",
dataType: "JSON",
success: function (json) {
if (json.state == 200){
let list = json.data;
let allCount = 0;
let allPrice = 0;
for (let i = 0; i < list.length; i++) {
let tr = '\n' +
' \n' +
'#{title} \n' +
'¥#{price} \n' +
'#{num} \n' +
'#{totalprice} \n' +
' ';
tr = tr.replace(/#{image}/g,list[i].image);
tr = tr.replace(/#{title}/g,list[i].title);
tr = tr.replace(/#{price}/g,list[i].price);
tr = tr.replace(/#{num}/g,list[i].num);
tr = tr.replace(/#{totalPrice}/g,list[i].num*list[i].price);
$("#cart-list").append(tr);
allCount += list[i].num;
allPrice += list[i].num*list[i].price;
}
$("#all-count").html(allCount);
$("#all-price").html(allPrice);
}
},
error: function (xhr) {
alert("增加购物车商品数量时发生异常:"+xhr.status)
}
});
}
</script>
通过用户id选择收货地址列表,在收货地址列表展示已存在后端不需要编写
在orderConfirm.html编写前端页面
function showAddress(){
$("#address-list").empty();
$.ajax({
url: "/address/",
type: "GET",
dataType: "JSON",
success: function (json) {
if (json.state == 200){
let list = json.data;
for (let i = 0; i < list.length; i++) {
let opt = '';
opt = opt.replace(/#{aid}/g,list[i].aid);
opt = opt.replace(/#{tag}/g,list[i].tag);
opt = opt.replace(/#{name}/g,list[i].name);
opt = opt.replace(/#{provinceName}/g,list[i].provinceName);
opt = opt.replace(/#{cityName}/g,list[i].cityName);
opt = opt.replace(/#{areaName}/g,list[i].areaName);
opt = opt.replace(/#{address}/g,list[i].address);
opt = opt.replace(/#{phone}/g,list[i].phone);
$("#address-list").append(opt);
}
}
},
error: function (xhr) {
alert("购物车收货地址展示时发生异常:"+xhr.status)
}
});
}
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;
@Data
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;
}
@Data
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;
}
规划SQl语句
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}
)
设计接口和抽象方法
/**
* 插入订单数据
* @param order
* @return
*/
Integer insertOrder(Order order);
/**
* 插入订单项数据
* @param orderItem
* @return
*/
Integer insertOrderItem(OrderItem orderItem);
编写映射文件
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}
)
设计接口和抽象方法
Order creatOrder(Integer aid, Integer uid, String username, Integer[] cids);
实现接口和抽象方法
@Service
public class OrderServiceImpl implements IOrderService {
@Autowired
private AddressMapper addressMapper;
@Autowired
private OrderMapper orderMapper;
@Autowired
private CartMapper cartMapper;
@Override
public Order creatOrder(Integer aid, Integer uid, String username, Integer[] cids) {
List list = cartMapper.findOVByCid(cids);
//计算总金额
Long totalPrice = 0L;
for (CartVO cartVO : list) {
totalPrice += cartVO.getRealPrice()*cartVO.getNum();
}
Address add = addressMapper.getAddressByAid(aid);
Order order = new Order();
order.setUid(uid);
order.setRecvName(username);
order.setRecvPhone(add.getPhone());
order.setRecvProvince(add.getProvinceName());
order.setRecvCity(add.getCityName());
order.setRecvArea(add.getAreaName());
order.setRecvAddress(add.getAddress());
order.setTotalPrice(totalPrice);
order.setStatus(0);
order.setOrderTime(new Date());
order.setCreatedUser(username);
order.setCreatedTime(new Date());
Integer integer = orderMapper.insertOrder(order);
if (integer != 1){
throw new InsertException("插入时异常");
}
for (CartVO cartVO : list) {
OrderItem orderItem = new OrderItem();
orderItem.setOid(order.getOid());
orderItem.setPid(cartVO.getPid());
orderItem.setTitle(cartVO.getTitle());
orderItem.setImage(cartVO.getImage());
orderItem.setPrice(cartVO.getRealPrice());
orderItem.setNum(cartVO.getNum());
orderItem.setCreatedUser(username);
orderItem.setCreatedTime(new Date());
Integer integer1 = orderMapper.insertOrderItem(orderItem);
if (integer1 != 1){
throw new InsertException("插入订单项数据失败");
}
}
return order;
}
}
设计请求
/orders/creat/
PoST
Integer aid,Integer[] cids,HttpSession session
JsonResult
实现请求
@RestController
@RequestMapping("orders")
public class OrderController extends BaseController{
@Autowired
private IOrderService orderService;
@RequestMapping("creat")
public JsonResult creatOrder(Integer aid, Integer[] cids, HttpSession session){
Order data = orderService.creatOrder(aid, getUidFromSession(session),
getUsernameFromSession(session), cids);
return new JsonResult<>(OK,data);
}
}
//在orderConfirm.html
$("#btn-create-order").click(function () {
let aid = $("#address-list").val();
let cids = location.search.substr(1);
$.ajax({
url: "/orders/creat",
data: "aid="+aid+"&"+cids,
type: "GET",
dataType: "JSON",
success: function (json) {
if (json.state == 200){
location.href = "payment.html";
alert("订房创建成功");
console.log(json.data);
}else {
alert("订单创建失败");
}
},
error: function (xhr) {
alert("购物车收货地址展示时发生异常:"+xhr.status)
}
});
});
/** 获取功能耗费时间
* @author 38058
*/
/**将当前对象创建使用维护交给spring容器*/
@Component
/**将当前类标记为切面类*/
@Aspect
public class TimerAspect {
/**将当前方法映射到某个页面*/
@Around("execution(* com.cy.store.service.impl.*.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Long start = System.currentTimeMillis();
Object proceed = pjp.proceed();
Long end = System.currentTimeMillis();
Long time = end - start;
System.out.println("耗时:"+time);
return proceed;
}
}