zoukankan      html  css  js  c++  java
  • Goods:查询某个用户的购物车条目以及添加购物车条目

    CartItemDao

     1 //查询某个用户的某本图书的购物车条目是否存在
     2     public CartItem findByUidAndBid(String uid,String bid) throws SQLException
     3     {
     4         String sql="select * from t_cartitem where uid=? and bid=?";
     5         Map<String,Object> map=qr.query(sql, new MapHandler(),uid,bid);
     6         CartItem cartItem=toCartItem(map);
     7         return cartItem;
     8     }
     9     
    10     //修改数量
    11     
    12     public void updateQuantity(String cartItemId,int quantity) throws SQLException
    13     {
    14         String sql="update t_cartitem set quantity=? where cartItemId=?";
    15         qr.update(sql,quantity,cartItemId);
    16     }
    17     
    18     //添加cartItem
    19     
    20     public void addCartItem(CartItem cartItem) throws SQLException
    21     {
    22         String sql="insert into t_cartitem(cartitemId,quantity,bid,uid)"+
    23       " values(?,?,?,?)";
    24         Object[] params={cartItem.getCartItemId(),cartItem.getQuantity(),
    25                 cartItem.getBook().getBid(),
    26                 cartItem.getUser().getUid()};
    27         qr.update(sql,params);
    28         
    29         
    30     }
    31     //把一个map映射成一个cartItem
    32     private CartItem toCartItem(Map<String,Object> map)
    33     {   
    34         //
    35         if(map==null||map.size()==0) return null;
    36         //1、把Map 
    37         CartItem cartItem=CommonUtils.toBean(map, CartItem.class);
    38         Book book=CommonUtils.toBean(map, Book.class);
    39         User user=CommonUtils.toBean(map, User.class);
    40         cartItem.setBook(book);
    41         cartItem.setUser(user);
    42         return cartItem;
    43         
    44     }
    45     
    46     //把多个map (List<Map>)映射成多个CartItem(List<CartItem>)
    47     
    48     private List<CartItem> toCartItemList(List<Map<String,Object>> mapList)
    49     {
    50         List<CartItem> cartItemList=new ArrayList<CartItem>();
    51         for(Map<String,Object> map:mapList)
    52         {
    53             CartItem cartItem=toCartItem(map);
    54             cartItemList.add(cartItem);
    55         }
    56         return cartItemList;
    57     }
    58     
    59     //通过用户查询购物车条目
    60     public List<CartItem> findByUser(String uid) throws SQLException
    61     {   
    62         //多表查询
    63         String sql="select * from t_cartitem c,t_book b where c.bid=b.bid where uid=? order by c.orderBy";
    64         List<Map<String, Object>> mapList=qr.query(sql, new MapListHandler(),uid);
    65         
    66         return toCartItemList(mapList);
    67         
    68     }

    CartItemService

     1 public List<CartItem> myCart(String uid)
     2     {
     3         try {
     4             return cartItemDao.findByUser(uid);
     5         } catch (SQLException e) {
     6             // TODO Auto-generated catch block
     7             throw new RuntimeException(e);
     8         }
     9         
    10     }
    11     
    12     //添加条目
    13     public void add(CartItem cartItem)
    14     {
    15         //使用uid bid 看条目是否存在  _cartItem 加上下划线表示从数据库传过来的数据
    16         try {
    17             CartItem _cartItem=cartItemDao.findByUidAndBid
    18                     (cartItem.getUser().getUid(), cartItem.getBook().getBid());
    19             if(_cartItem==null) //如果没有原来这个条目 添加条目
    20             {   
    21                 cartItem.setCartItemId(CommonUtils.uuid());
    22                 cartItemDao.addCartItem(cartItem);
    23             }
    24             else{ 
    25                 //如果原来有这个条目 修改数量
    26                 int quantity=cartItem.getQuantity()+_cartItem.getQuantity();
    27                 //_cartItem.setQuantity(quantity);
    28                 //修改老条目
    29                 cartItemDao.updateQuantity(_cartItem.getCartItemId(), quantity);
    30         
    31             }
    32         } catch (SQLException e) {
    33             throw new RuntimeException(e);
    34         }
    35     }

     效果图如下:

  • 相关阅读:
    ADO.NET改进版
    H面试程序(11): 判断字符串是否包含子串问题
    H面试程序(12): 输出字符串中第一个只出现一次的字母
    Hadoop CLI MiniCluster
    hdu 3061 (最大权闭合图)
    CF 338 D GCD Table(CRT)
    【Todo】Nodejs学习计划
    【转载】游戏并发编程的讨论 & Nodejs并发性讨论 & 语法糖术语
    Jedis(Java+Redis) Pool的使用
    【Todo】Apache-Commons-Pool及对象池学习
  • 原文地址:https://www.cnblogs.com/xiaoying1245970347/p/4785150.html
Copyright © 2011-2022 走看看