You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
4.0 KiB
111 lines
4.0 KiB
package ShopCar;
|
|
|
|
import Connect.Connect;
|
|
import JavaBean.Shopcar;
|
|
import org.hibernate.Session;
|
|
import org.hibernate.Transaction;
|
|
import org.hibernate.query.Query;
|
|
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.http.HttpServlet;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpSession;
|
|
import java.io.IOException;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
|
|
public class ShopCartServlet extends HttpServlet {
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
if(req.getServletPath().equals("/addBook.do")){
|
|
addBook(req, resp);
|
|
}else if(req.getServletPath().equals("/deleteBook.do")){
|
|
deleteBook(req, resp);
|
|
}else if(req.getServletPath().equals("/showCart.do")){
|
|
showShopcar(req,resp);
|
|
}
|
|
}
|
|
private void showShopcar(HttpServletRequest req, HttpServletResponse resp) {
|
|
HttpSession session=req.getSession();
|
|
Object obj=session.getAttribute("userID");
|
|
int userId = 0;
|
|
if(obj!=null) {
|
|
userId = (int) obj;
|
|
System.out.println("userID是"+userId);
|
|
}
|
|
Session hSession=Connect.getConfig();
|
|
Transaction tx=hSession.beginTransaction();
|
|
String hql = "FROM Shopcar WHERE userId=:userid";
|
|
Query query = hSession.createQuery(hql);
|
|
query.setParameter("userid",userId);
|
|
tx.commit();
|
|
List results = query.list();
|
|
System.out.println("购物车中一共有"+results.size());
|
|
Iterator it=results.iterator();
|
|
while(it.hasNext()){
|
|
Shopcar shopcar= (Shopcar) it.next();
|
|
System.out.println("Bookid"+shopcar.getBookid());
|
|
System.out.println("Itemid"+shopcar.getItemid());
|
|
System.out.println("useid"+shopcar.getUserId());
|
|
}
|
|
}
|
|
private void addBook(HttpServletRequest req, HttpServletResponse resp){
|
|
HttpSession session=req.getSession();
|
|
Object obj=session.getAttribute("userID");
|
|
int bookID=10;
|
|
int userId = 0;
|
|
if(obj!=null) {
|
|
userId = (int) obj;
|
|
System.out.println("userID是"+userId);
|
|
}
|
|
Session hSession= Connect.getConfig();
|
|
if(userId!=0) {
|
|
System.out.println(" this is ");
|
|
Transaction tx=hSession.beginTransaction();
|
|
Shopcar shopCar=new Shopcar();
|
|
shopCar.setUserId(userId);
|
|
shopCar.setBookid(bookID);
|
|
shopCar.setNumber(10);
|
|
hSession.save(shopCar);
|
|
tx.commit();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
System.out.println("程序执行到这里了");
|
|
String bookid=req.getParameter("bookid");
|
|
System.out.println(bookid);
|
|
doPost(req, resp);
|
|
}
|
|
|
|
private void deleteBook(HttpServletRequest req,HttpServletResponse resp){
|
|
HttpSession session=req.getSession();
|
|
Object obj=session.getAttribute("userID");
|
|
int bookID=10;
|
|
int userId = 0;
|
|
if(obj!=null) {
|
|
userId = (int) obj;
|
|
System.out.println("userID是"+userId);
|
|
}
|
|
Session hSession= Connect.getConfig();
|
|
if(userId!=0) {
|
|
Transaction tx1=hSession.beginTransaction();
|
|
String hql = "FROM Shopcar WHERE bookid=:bookid AND userId=:userid";
|
|
Query query = hSession.createQuery(hql);
|
|
query.setParameter("bookid",bookID);
|
|
query.setParameter("userid",userId);
|
|
List results = query.list();
|
|
tx1.commit();
|
|
if(results.size()>0){
|
|
Transaction tx2=hSession.beginTransaction();
|
|
Shopcar deleteItem= (Shopcar) results.get(0);
|
|
hSession.delete(deleteItem);
|
|
tx2.commit();
|
|
}else{
|
|
System.out.println("无法删除");
|
|
}
|
|
}
|
|
}
|
|
}
|