JavaWEB(MVC购物车02)

哈喽大家好~~~今天又又又给大家分享啦~~~~今天还是购物车 ~~~~

 

 来欣赏一下我们的代码吧~~~~

这是购物车dao方法 

public class CartDao implements ICartDao {

	@Override
	public int addCart(Cart c) {
		Connection con=null;
		PreparedStatement ps=null;
		int n=0;
		try {
			con=DBHelper.getCon();
			String sql="insert into cart " + "select nvl(max(cid),0)+1,?,?,?,? from cart";
			ps=con.prepareStatement(sql);
			ps.setInt(1, c.getGoods().getGid());
			ps.setInt(2, c.getUsers().getUuid());
			ps.setInt(3, c.getCcount());
			ps.setInt(4, c.getCtotal());
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myClose(con, ps,null);
		}
		return n;
	}

	@Override
	public List<Cart> getCartByUsersid(int uuid) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs = null;
		Cart c= null;
		List<Cart> list = new ArrayList<Cart>();
		try {
			con=DBHelper.getCon();
			String sql="select * from cart where userid="+uuid;
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()) {
				c=new Cart();
				c.setCid(rs.getInt(1));
				c.setGoods(new GoodsDao().getGoodsByGid(rs.getInt(2)));
				//更新
				c.setUsers(new Users(rs.getInt(3), "zkingzz", "123"));
				c.setCcount(rs.getInt(4));
				c.setCtotal();
				list.add(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myClose(con, ps,rs);
		}
		return list;
	}

	@Override
	public int editCartByAndUserid(Cart cart) {
		Connection con=null;
		PreparedStatement ps=null;
		int n=0;
		try {
			con=DBHelper.getCon();
			String sql="update cart set ccount=?,ctotal=? where cid=? and userid=?";
			ps=con.prepareStatement(sql);
			ps.setInt(1, cart.getCcount());
			ps.setInt(2, cart.getCtotal());
			ps.setInt(3, cart.getCid());
			ps.setInt(4, cart.getUsers().getUuid());
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myClose(con, ps,null);
		}
		return n;
	}

	@Override
	public int delcart(int cid) {
		Connection con = null;
		PreparedStatement ps = null;
		int n=0;
		try {
			con=DBHelper.getCon();
			String sql="delete cart where cid="+cid;
			ps=con.prepareStatement(sql);
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myClose(con, ps);
		}
		return n;
	}

	@Override
	public int deleteCartGoodsByUsersid(int userid) {
		Connection con = null;
		PreparedStatement ps = null;
		int n=0;
		try {
			con=DBHelper.getCon();
			String sql="delete from cart where userid="+userid;
			ps=con.prepareStatement(sql);
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.myClose(con, ps);
		}
		return n;
	}

}

 

public interface ICartDao {

	/**
	 * 增加
	 * @param c 商品
	 * @return 影响行数
	 */
	int addCart(Cart c);
	
	/**
	 * 根据指定的登录用户id查找对应的购物车
	 * @param uuid 用户id
	 * @return
	 */
	List<Cart> getCartByUsersid(int uuid);
	
	/**
	 * 根据用户id和购物车编号修改商品数量
	 * @param cart
	 * @return
	 */
	int editCartByAndUserid(Cart cart);
	
	/**
	 * 删除订单
	 * @param cid 订单编号
	 * @return 影响行数
	 */
	int delcart(int cid);
	/**
	 * 删除购物车
	 * @param userid
	 * @return
	 */
	int deleteCartGoodsByUsersid(int userid);
}

 数据库逻辑层


public interface ICartBiz {

	/**
	 * 增加
	 * @param c 商品
	 * @return 影响行数
	 */
	int addCart(Cart c);
	
	/**
	 * 根据指定的登录用户id查找对应的购物车
	 * @param uuid 用户id
	 * @return
	 */
	List<Cart> getCartByUsersid(int uuid);
	
	/**
	 * 根据用户id和购物车编号修改商品数量
	 * @param cart
	 * @return
	 */
	int editCartByAndUserid(Cart cart);
	
	/**
	 * 删除订单
	 * @param cid 订单编号
	 * @return 影响行数
	 */
	int delcart(int cid);
	
	/**
	 * 删除购物车
	 * @param userid
	 * @return
	 */
	int deleteCartGoodsByUsersid(int userid);
}

 AddCartServlet包~~~


/**
 * Servlet implementation class AddCartServlet
 */
@WebServlet("/AddCartServlet")
public class AddCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//jrebel 热部署启动软件
		
		//设置字符编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String id=request.getParameter("gid");
		int gid=0;
		if(null!=id) {
			gid=Integer.valueOf(id);
		}
		
		//数据封装
		Cart c = new Cart();
		IGoodsBiz igb = new GoodsBiz();
		Goods goods = igb.getGoodsByGid(gid);
		
		HttpSession session= request.getSession();
		Users users =(Users)session.getAttribute("u");
		
		c.setGoods(goods);
		c.setUsers(users);
		c.setCcount(1);
		c.setCtotal();
		ICartBiz icb = new CartBiz();
		List<Cart> listCarts = icb.getCartByUsersid(users.getUuid());
		//如果购物车为空  
		boolean flag=true;
		if(null==listCarts) {
			listCarts=new ArrayList<Cart>();
		}else {
			for(Cart cart :listCarts) {
				if(cart.getGoods().getGid()==c.getGoods().getGid()) {
					cart.setCcount(cart.getCcount()+1);
					cart.setCtotal();
					icb.editCartByAndUserid(cart);
					flag=false;
					break;
				}
			}
		}
		//listCarts.add(c);
		if(flag) {
			icb.addCart(c);
		}
		//加入后数据要继续更新
		listCarts = icb.getCartByUsersid(users.getUuid());
		
		session.setAttribute("listCarts", listCarts);
		response.sendRedirect("cart.jsp");
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值