ssm框架+MySQL批量添加和删除

1.批量添加

jsp

生成订单
  图书名称 单价 数量 小计
${cartitems.book.bname} ¥${cartitems.book.currPrice} ${cartitems.quantity } ¥${cartitems.subtotal }
总计:¥
收货地址
提交订单

Controller层获取页面提交的信息

//添加订单
    @RequestMapping("/addOrder")
    public String addOrder(HttpServletRequest request,Cartitem cartitem){

        //获取字符串
        String s=request.getParameter("cartItemIds");
        System.out.println(s);
        //截取字符串
        int i2=s.length();
        String s2=s.substring(1, i2-1);
        System.out.println(s2);
        //将字符串切割为数组
        String[] str=s2.split(", ");
        System.out.println("------------------");
        //将切割好的数组赋给cartItemIds
        cartitem.setCartItemIds(str);
        System.out.println(Arrays.toString(cartitem.getCartItemIds()));

        List cartitems=cartitemService.findCartitemByCartItemId(cartitem);

        Order order=new Order();
        //Order
        order.setOid(String.valueOf(UUID.randomUUID()));
        order.setOrdertime(String.format("%tF % orderItems=new ArrayList();
        for (Cartitem c:cartitems) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderItemId(String.valueOf(UUID.randomUUID()));
            orderItem.setQuantity(c.getQuantity());
            orderItem.setSubtotal(Double.valueOf(request.getParameter("total")));
            orderItem.setBook(c.getBook());
            orderItem.setOrder(order);
            orderItems.add(orderItem);
        }
        order.setOrderItemList(orderItems);
		//调用service
        orderService.addOrder(order);
        request.setAttribute("order",order);

        return "jsps/order/ordersucc";
    }

service层

//接口
//添加Order
        public void addOrder(Order order);

//实现类
    /**
     * 添加Order
     * */
    @Transactional
    @Override
    public void addOrder(Order order) {
        //添加Order
        orderMapper.addOrder(order);
        //批量添加OrderItem
        orderMapper.addOrderitem(order);
    }

dao层

//mapper接口
//批量添加
    public void addOrderitem(Order order);

//mapper.xml

    
        INSERT INTO t_order (oid,ordertime,total,status,address,uid)
            VALUES ('${oid}','${ordertime}',${total},${status},'${address}','${owner.uid}')
    
    
    
        INSERT INTO t_orderitem (orderItemId,quantity,subtotal,bid,bname,currPrice,image_b,oid)
        VALUES
            
            (
                '${orderItemList.orderItemId}',
                ${orderItemList.quantity},
                ${orderItemList.subtotal},
                '${orderItemList.book.bid}',
                '${orderItemList.book.bname}',
                ${orderItemList.book.currPrice},
                '${orderItemList.book.image_b}',
                '${orderItemList.order.oid}'
            )
            
    

2.批量删除

jsp提交数组

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>



  
    cartlist.jsp
    
	
	
	    
	
	
	
	
	
	
	

  
  
	<%--根据查询的数据显示不同的页面--%>
		<%--不存在商品条系--%>
			
您的购物车中暂时没有商品
<%--存在商品条系--%>
商品名称 单价 数量 小计 操作
${cartitems.book.bname} ¥${cartitems.book.currPrice} <%--虽然在Cartitem类中没有subtotal属性,但在Cartitem类中提供了subtotal的get方法,所以在subtotal中可以.到--%> ¥${cartitems.subtotal } 删除
批量删除

总计:¥

Controller接受条件(我们用id的数组为例)

/**
     * 批量删除商品条系
     * */
    @RequestMapping("/deletesCartitemByCartItemId")
    public String deletesCartitemByCartItemId(Cartitem cartitem,HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("sessionUser");
        cartitem.setUser(user);
        cartitemService.deletesCartitemByCartItemId(cartitem,request);
        List cartitems=cartitemService.findCartitemByUser(cartitem);
        request.setAttribute("cartitems",cartitems);//保存到request域
        return "jsps/cart/list";
    }

select层

//接口
//批量删除商品条系
        public void deletesCartitemByCartItemId(Cartitem cartitem, HttpServletRequest request);

//实现类
/**
     * 批量删除商品条系
     * */
    @Override
    public void deletesCartitemByCartItemId(Cartitem cartitem,HttpServletRequest request) {
        Map error=new HashMap();
        try {
            cartitemMapper.deletesCartitemByCartItemId(cartitem);
        }catch (Exception e){
            error.put("cartitem","请勾选您要删除的商品");
            request.setAttribute("error", error);
        }

    }

dao层

mapper接口
//批量删除商品条系
    public void deletesCartitemByCartItemId(Cartitem cartitem);

//mapper.xml

    
        DELETE FROM t_cartitem
        WHERE cartItemId IN
        
            '${item}'
        
    

感谢大家的支持,希望能够帮助到您!
每天进步一点点!

你可能感兴趣的:(java,javaWeb,ssm框架)