哈喽大家好~~~今天又又又给大家分享啦~~~~今天还是购物车 ~~~~
来欣赏一下我们的代码吧~~~~
这是购物车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");
}
}