我的第一个博客,此博客只用于纪录自己在做项目时解决的小问题,以免自己写过的代码都忘记;
SSM项目中购物车多项下单,没有设计订单和商品中间的关联表订单条目表,只有订单表和商品表,完成多项下单。
下面是前台购物车代码
<c:forEach items="${myCartList }" var="offer" varStatus="Offer">
<tr onmouseover="this.style.backgroundColor = '#F5FAFE';" onmouseout="this.style.backgroundColor = 'white'" align="center">
<td width="10%">
<input type="checkbox" id="sid" name="sel" value="${offer.paperid }" onclick = "count(this)">
</td>
<td>
<a target="_blank" style="font-size:15px;">
<c:out value="${offer.printtype }"></c:out><br>
<c:out value="${offer.productsize }"></c:out>
<c:out value="${offer.printcolor }"></c:out><br>
<c:out value="${offer.papertype }"></c:out>
</a>
</td>
<td>
<c:out value="${offer.printnumber }"></c:out>
</td>
<td id="price">
¥<span class="subtotal" name="subtotal" id="subtotal">
<c:out value="${offer.totalprice }"></c:out>
</span>
</td>
<td>
<a href="queryOfferByPaperId.do?paperId=${offer.paperid }">详情</a>
<a href="delCartByPaperId.do?paperId=${offer.paperid }" onclick="return confirm('您是否要删除该项吗?')">删除</a>
</td>
</tr>
</c:forEach>
<a href="javascript:;" onclick="send()">
<input type="button" value="提交订单" name="submit" >
</a>
下面是js代码
function send(){
var tests = document.getElementsByName("sel");
var total = document.getElementById("myspan").text;
var total = 0;
//将选中的多个商品的id数组传到后台
$(":checkbox:checked").each(function(){
var conNum = $(this).parents("td").siblings("#price").children("span").html();
total += parseFloat(conNum);
});
var value = new Array();
for(var i = 0; i < tests.length; i++){
if(tests[i].checked)
value.push(tests[i].value);
}
location.href = "toUpdateCartMany.do?paperids=" + value+"&total="+total; //把value转化为字符串作为参数传到后台
}
**上面代码将选中的商品的id作为数组传到后台,后台接受数组,提交数据后去下单,
控制跳转到填写收货人信息页面**
//去下单
@RequestMapping(value="/toUpdateCartMany.do",produces="text/html; charset=UTF-8")
public String toUpdateCartMany(Map`<String,Object>` map,HttpServletRequest request){
HttpSession session = request.getSession();
//先接受传来的多个商品id和总价格
String value = request.getParameter("paperids");
String value1 = request.getParameter("total");
//将数据放入session
session.setAttribute("paperids", value);
session.setAttribute("total", value1);
return "order/order";
}
收货人信息填好后,点击提交自动生成一条订单号,
//生成订单
@RequestMapping(value="/addOrder.do",method=RequestMethod.POST,produces="text/html; charset=UTF-8")
public String addOrder(Order order,HttpServletRequest request){
HttpSession session = request.getSession();
orderDao.insertOrder(order);
System.out.println(order);
//将生成订单的订单号放入session中,方便更改已下单的报价单
session.setAttribute("orderno", order.getOrderno());
session.setAttribute("pricetotal", order.getTotal());
return "redirect:/updateCartMany.do";
}
**控制跳转到方法updateCartMany.do,目的是将生成的订单号批量插入到商品表中
,此时下单的多个商品拥有一个订单号**
@RequestMapping(value="/updateCartMany.do",produces="text/html; charset=UTF-8")
public String updateCartMany(Map`<String,Object>` map,HttpServletRequest request){
HttpSession session = request.getSession();
String[] values = ((String) session.getAttribute("paperids")).split(",");//拆分传来的商品id数组
int[] paperids = new int[values.length];
for (int i =0;i`<`paperids.length;i++){
paperids[i] = Integer.parseInt(values[i]);
}
//从session中获取生成的订单号,用动态sql插入到各个商品中
map.put("orderno", session.getAttribute("orderno"));
//默认此时生成的一条订单是未付款状态
map.put("style", 0);
map.put("paperIds",paperids);
orderDao.updateCartMany(map);
return "order/pay";
}
**下面是mapper.xml中的动态sql语句,然后数据库交互,
此时就把生成的一条订单号批量插入到多个商品中**
<update id="updateCartMany" parameterType="map">
update ap_offer set orderno=#{orderno},style=#{style} where paperId in
<foreach item="item" index="index" collection="paperIds" open="(" separator="," close=")">
#{item}
</foreach>
</update>