(1)添加购物车
Controller:
CartService:
实现类:
CartDetail detail=dao.queryByCdid(cid,gds.getId());
CartDao:
//获取详情对象
@Select("select * from t_cartdetail where cid=#{cid} and gid=#{gid}")
@ResultType(CartDetail.class)
public CartDetail queryByCdid(@Param("cid") int cid, @Param("gid") int gid);
return dao.insertDetail(cd)>0;
//购物车添加商品
@Insert("insert into t_cartdetail(cid,gid,num,money) values(#{cid},#{gid},#{num},#{money})")
public int insertDetail(CartDetail cd);
return dao.updateDetail(detail)>0;
//修改购物车中数量
@Update("update t_cartdetail set num=${num},money=${money} where cid=#{cid} and gid=#{gid}")
public int updateDetail(CartDetail cartdetail);
(2)查看购物车
CartService:
实现类:
CarDao
购物车详情表 cid购物车id gid商品id num 购买数量
商品表:num 商品库存量
SELECT cd.num,cd.money,cd.gid, g.name,g.price,g.num as num2,g.picture FROM t_cartdetail cd LEFT JOIN t_goods g ON cd.gid=g.id WHERE cd.cid=#{cid}
//购物车的数据
@Select("SELECT cd.num,cd.money,cd.gid,g.name,g.price,g.num as num2,g.picture FROM t_cartdetail cd LEFT JOIN t_goods g ON cd.gid=g.id WHERE cd.cid=#{cid}")
@ResultType(ViewCart.class)
public List<ViewCart> queryCart(int cid);
(3)删除购物车商品
CartService:
实现类:CarDao:
//清空购物车
@Delete("delete from t_cartdetail where cid=#{cid} ")
public int deleteDetailByCid(int cid);
//删除购物车中的商品
@Delete("delete from t_cartdetail where cid=#{cid} and gid=#{gid}")
public int deleteDetail(@Param("cid") int cid, @Param("gid") int gid);
(4)购物车+ - 按钮
CartService:
实现类:
CartDao:
//获取详情对象
@Select("select * from t_cartdetail where cid=#{cid} and gid=#{gid}")
@ResultType(CartDetail.class)
public CartDetail queryByCdid(@Param("cid") int cid, @Param("gid") int gid);
//修改购物车中数量
@Update("update t_cartdetail set num=${num},money=${money} where cid=#{cid} and gid=#{gid}")
public int updateDetail(CartDetail cartdetail);
+ :传递num:1
- :传递num:-1
前端:
这里有两个num
num2:商品库存的数量
num:购买商品的数量
<table class="table table-bordered table-striped table-hover">
<tr>
<th>图片</th>
<th>商品名称</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
<c:set value="0" var="sum"></c:set>
<c:forEach items="${carts}" var="c" varStatus="i">
<c:if test="${c.num2 <= 0 || c.num2 < c.num}">
<button class="sxsp" type="button" class="btn btn-default" onclick="clearCart2(${c.gid})">删除</button>
</c:if>
<c:if test="${c.num2 > 0 || c.num2 >= c.num}">
<tr>
<th><img style="width: 30px;height: auto" src="${pageContext.request.contextPath}/fmwimages/${c.picture}"></th>
<th>${c.name}</th>
<th>${c.price}</th>
<th width="100px">
<div class="input-group">
<span class="input-group-btn">
<!--数量-1 -->
<button class="btn btn-default" type="button"
onclick="mNum(${c.gid},${c.price},${i.count})">-
</button>
</span>
<input type="text" class="form-control" id="num_count${i.count}" value="${c.num}" readonly="readonly" style="width:50px;text-align:center;">
<c:if test="${c.num < c.num2}">
<span class="input-group-btn">
<!-- 数量+1 -->
<button class="btn btn-default" type="button" onclick="pNum(${c.gid},${c.price},${i.count})">+</button>
</span>
</c:if>
<c:if test="${c.num >= c.num2}">
<span class="input-group-btn">
<!-- 数量+1 -->
<button style="pointer-events:none" class="btn btn-default disabled" type="button" onclick="pNum(${c.gid},${c.price},${i.count})">+</button>
</span>
</c:if>
</div>
</th>
<th>${c.money}元</th>
<th>
<button type="button" class="btn btn-default" onclick="clearCart(${c.gid})">删除</button>
</th>
</tr>
<c:set var="sum" value="${sum+c.money}"></c:set>
</c:if>
</c:forEach>
</table>
js
<script type="text/javascript">
//数量+1
function pNum(gid,p,no){
var nums = $("#num_count"+no).val();
$.ajax({
url:"updateCartNum?gid="+gid+"&num=1&price="+p,
method:"get",
success:function(){
location.href = "getCart";
},
error:function(){
alert("服务器异常");
}
})
}
//数量-1 如果删除为0
function mNum(gid,p,no){
var num = -1; //数量
var nums = $("#num_count"+no).val();
//验证是否需要删除
if(Number(nums)<=1){
if(confirm("确认要删除吗?")){
/* num = 0; */
location.href="clearCart?gid="+gid;
return;
}else{
return;
}
}
//异步
$.ajax({
url:"updateCartNum?gid="+gid+"&num="+num+"&price="+p,
method:"get",
success:function(){
location.href = "getCart";
},
error:function(){
alert("服务器异常");
}
})
}
function clearCart(gid){
if(confirm("确认要删除吗")){
location.href="clearCart?gid="+gid;
}
}
function clearCart2(gid){
location.href="clearCart?gid="+gid;
}
$(function (){
$(".sxsp").click();
})
</script>
(5)获取订单
OrderController:
type=2为购物车下单
CarService
//购物车的数据
@Select("SELECT cd.num,cd.money,cd.gid,g.name,g.price,g.num as num2,g.picture FROM t_cartdetail cd LEFT JOIN t_goods g ON cd.gid=g.id WHERE cd.cid=#{cid}")
@ResultType(ViewCart.class)
public List<ViewCart> queryCart(int cid);
UserAddressService:
实现类:
UserAddressDao:
//查询地址
@Select("select * from t_useraddress where uid=#{uid} order by flag desc")
@ResultType(UserAddress.class)
public List<UserAddress> queryByUid(int uid);
(6)提交订单
res = service.insertDirect(user.getId(), oid, aid, (CartDetail) session.getAttribute("direct"));
实现类
dao.insert(order);
//新增 订单
@Insert("insert into t_order(id,uid,uaid,money,createtime,flag) values(#{id},#{uid},#{uaid},#{money},now(),1)")
public int insert(Order order);
dao.insertDetail(detail);
//新增 订单详情
@Insert("insert into t_orderdetail(oid,gid,money,num) values(#{oid},#{gid},#{money},#{num})")
public int insertDetail(OrderDetail detail);
goodsDao.updateGoodsNum(cd.getNum(),cd.getGid());
GoodDao
//修改商品数量
@Update("update t_goods set num = num - #{num} where id = #{id}")
int updateGoodsNum(@Param("num") int num,@Param("id") int id);
res = service.save(oid, user.getId(), aid);
实现类:
CartDao:
//获取用户的购物车详情
@Select("select cd.* from t_cartdetail cd left join t_cart c on cd.cid=c.id where c.uid=#{uid}")
@ResultType(CartDetail.class)
public List<CartDetail> queryByDetail(int uid);
OrderDao
//新增 订单详情
@Insert("insert into t_orderdetail(oid,gid,money,num) values(#{oid},#{gid},#{money},#{num})")
public int insertDetail(OrderDetail detail);
GoodDao:
//修改商品数量
@Update("update t_goods set num = num - #{num} where id = #{id}")
int updateGoodsNum(@Param("num") int num,@Param("id") int id);
cartDao.deleteDetailByCid(cds.get(0).getCid());
//清空购物车
@Delete("delete from t_cartdetail where cid=#{cid} ")
public int deleteDetailByCid(int cid);
cartDao.updateEmpty(cds.get(0).getCid());
//清空购物车
@Update("update t_cart set money=0 where id=#{id}")
public int updateEmpty(int id);