From 2f21a195b2bcde69cbc716eea3690557378add6d Mon Sep 17 00:00:00 2001 From: dell <2842410839@qq.com> Date: Fri, 3 Jan 2020 14:30:13 +0800 Subject: [PATCH] first commit --- src/Connect/Connect.java | 97 ++++++++++++++------------- src/JaveBean/Book.java | 86 ++++++++++++++++++++++++ src/JaveBean/Order.java | 64 ++++++++++++++++++ src/JaveBean/Shopcar.java | 62 ++++++++++++++++++ src/JaveBean/User.java | 75 +++++++++++++++++++++ src/ShopCar/ShopCartServlet.java | 108 +++++++++++++++++++++++++++++++ 6 files changed, 443 insertions(+), 49 deletions(-) create mode 100644 src/JaveBean/Book.java create mode 100644 src/JaveBean/Order.java create mode 100644 src/JaveBean/Shopcar.java create mode 100644 src/JaveBean/User.java create mode 100644 src/ShopCar/ShopCartServlet.java diff --git a/src/Connect/Connect.java b/src/Connect/Connect.java index afe22ef..b20479b 100644 --- a/src/Connect/Connect.java +++ b/src/Connect/Connect.java @@ -7,55 +7,54 @@ import org.hibernate.cfg.Configuration; import javax.swing.*; import java.sql.*; public class Connect { - private Statement sql=null; - private ResultSet result; - // private String url="jdbc:mysql://localhost:3306/userinfo?serverTimezone=GMT%2B8"; + private Statement sql=null; + private ResultSet result; +// private String url="jdbc:mysql://localhost:3306/userinfo?serverTimezone=GMT%2B8"; // private String user="root"; // private String passWord ="1996315211cjr"; - private Connection con=null; - private static Connect connect=new Connect("jdbc:mysql://localhost:3306/userinfo?serverTimezone=GMT%2B8","root","123456"); - // private Connect con=new Connect(url,user,passWord); - public static Connect getCon(){ - return connect; - } - private Connect(String url, String user, String passWord){ - try{ - Class.forName("com.mysql.jdbc.Driver"); - } - catch(Exception e){ - System.out.print(e); - } - try{ - con = DriverManager.getConnection(url,user,passWord); - } - catch(SQLException e){ - System.out.println(e); - } - } - public int getUpdate(String str){ - int insert=0; - try{ - sql=con.createStatement(); - insert=sql.executeUpdate(str); - }catch(SQLException e){ - System.out.println(e); - } - return insert; - } - public ResultSet getResult(String str){ - try{ - sql=con.createStatement(); - result=sql.executeQuery(str); - }catch(SQLException e){ - System.out.println(e); - } - return result; - } - public static Session getConfig(){ - Configuration configuration = new Configuration();//加载配置信息 - configuration.configure(); - SessionFactory sessionFactory=configuration.buildSessionFactory(); - return sessionFactory.openSession(); - } + private Connection con=null; + private static Connect connect=new Connect("jdbc:mysql://localhost:3306/userinfo?serverTimezone=GMT%2B8","root","1996315211cjr"); +// private Connect con=new Connect(url,user,passWord); + public static Connect getCon(){ + return connect; + } + private Connect(String url, String user, String passWord){ + try{ + Class.forName("com.mysql.jdbc.Driver"); + } + catch(Exception e){ + System.out.print(e); + } + try{ + con = DriverManager.getConnection(url,user,passWord); + } + catch(SQLException e){ + System.out.println(e); + } + } + public int getUpdate(String str){ + int insert=0; + try{ + sql=con.createStatement(); + insert=sql.executeUpdate(str); + }catch(SQLException e){ + System.out.println(e); + } + return insert; + } + public ResultSet getResult(String str){ + try{ + sql=con.createStatement(); + result=sql.executeQuery(str); + }catch(SQLException e){ + System.out.println(e); + } + return result; + } + public static Session getConfig(){ + Configuration configuration = new Configuration();//加载配置信息 + configuration.configure(); + SessionFactory sessionFactory=configuration.buildSessionFactory(); + return sessionFactory.openSession(); + } } - diff --git a/src/JaveBean/Book.java b/src/JaveBean/Book.java new file mode 100644 index 0000000..a320dee --- /dev/null +++ b/src/JaveBean/Book.java @@ -0,0 +1,86 @@ +package JaveBean; + +public class Book { + private int bookId; + private String bookname; + private Double price; + private String category; + private String imageurl; + private Integer sellerId; + + public int getBookId() { + return bookId; + } + + public void setBookId(int bookId) { + this.bookId = bookId; + } + + public String getBookname() { + return bookname; + } + + public void setBookname(String bookname) { + this.bookname = bookname; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getImageurl() { + return imageurl; + } + + public void setImageurl(String imageurl) { + this.imageurl = imageurl; + } + + public Integer getSellerId() { + return sellerId; + } + + public void setSellerId(Integer sellerId) { + this.sellerId = sellerId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Book book = (Book) o; + + if (bookId != book.bookId) return false; + if (bookname != null ? !bookname.equals(book.bookname) : book.bookname != null) return false; + if (price != null ? !price.equals(book.price) : book.price != null) return false; + if (category != null ? !category.equals(book.category) : book.category != null) return false; + if (imageurl != null ? !imageurl.equals(book.imageurl) : book.imageurl != null) return false; + if (sellerId != null ? !sellerId.equals(book.sellerId) : book.sellerId != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = bookId; + result = 31 * result + (bookname != null ? bookname.hashCode() : 0); + result = 31 * result + (price != null ? price.hashCode() : 0); + result = 31 * result + (category != null ? category.hashCode() : 0); + result = 31 * result + (imageurl != null ? imageurl.hashCode() : 0); + result = 31 * result + (sellerId != null ? sellerId.hashCode() : 0); + return result; + } +} diff --git a/src/JaveBean/Order.java b/src/JaveBean/Order.java new file mode 100644 index 0000000..a602edf --- /dev/null +++ b/src/JaveBean/Order.java @@ -0,0 +1,64 @@ +package JaveBean; + +public class Order { + private int orderId; + private Integer buyerId; + private Integer sellerId; + private int bookId; + + public int getOrderId() { + return orderId; + } + + public void setOrderId(int orderId) { + this.orderId = orderId; + } + + public Integer getBuyerId() { + return buyerId; + } + + public void setBuyerId(Integer buyerId) { + this.buyerId = buyerId; + } + + public Integer getSellerId() { + return sellerId; + } + + public void setSellerId(Integer sellerId) { + this.sellerId = sellerId; + } + + public int getBookId() { + return bookId; + } + + public void setBookId(int bookId) { + this.bookId = bookId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Order order = (Order) o; + + if (orderId != order.orderId) return false; + if (bookId != order.bookId) return false; + if (buyerId != null ? !buyerId.equals(order.buyerId) : order.buyerId != null) return false; + if (sellerId != null ? !sellerId.equals(order.sellerId) : order.sellerId != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = orderId; + result = 31 * result + (buyerId != null ? buyerId.hashCode() : 0); + result = 31 * result + (sellerId != null ? sellerId.hashCode() : 0); + result = 31 * result + bookId; + return result; + } +} diff --git a/src/JaveBean/Shopcar.java b/src/JaveBean/Shopcar.java new file mode 100644 index 0000000..01ab980 --- /dev/null +++ b/src/JaveBean/Shopcar.java @@ -0,0 +1,62 @@ +package JaveBean; + +public class Shopcar { + private int userId; + private Integer bookid; + private Integer number; + private int itemid; + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public Integer getBookid() { + return bookid; + } + + public void setBookid(Integer bookid) { + this.bookid = bookid; + } + + public Integer getNumber() { + return number; + } + + public void setNumber(Integer number) { + this.number = number; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Shopcar shopcar = (Shopcar) o; + + if (userId != shopcar.userId) return false; + if (bookid != null ? !bookid.equals(shopcar.bookid) : shopcar.bookid != null) return false; + if (number != null ? !number.equals(shopcar.number) : shopcar.number != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = userId; + result = 31 * result + (bookid != null ? bookid.hashCode() : 0); + result = 31 * result + (number != null ? number.hashCode() : 0); + return result; + } + + public int getItemid() { + return itemid; + } + + public void setItemid(int itemid) { + this.itemid = itemid; + } +} diff --git a/src/JaveBean/User.java b/src/JaveBean/User.java new file mode 100644 index 0000000..7c96f86 --- /dev/null +++ b/src/JaveBean/User.java @@ -0,0 +1,75 @@ +package JaveBean; + +public class User { + private String username; + private String password; + private String school; + private String sex; + private int userId; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getSchool() { + return school; + } + + public void setSchool(String school) { + this.school = school; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + User user = (User) o; + + if (userId != user.userId) return false; + if (username != null ? !username.equals(user.username) : user.username != null) return false; + if (password != null ? !password.equals(user.password) : user.password != null) return false; + if (school != null ? !school.equals(user.school) : user.school != null) return false; + if (sex != null ? !sex.equals(user.sex) : user.sex != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = username != null ? username.hashCode() : 0; + result = 31 * result + (password != null ? password.hashCode() : 0); + result = 31 * result + (school != null ? school.hashCode() : 0); + result = 31 * result + (sex != null ? sex.hashCode() : 0); + result = 31 * result + userId; + return result; + } +} diff --git a/src/ShopCar/ShopCartServlet.java b/src/ShopCar/ShopCartServlet.java new file mode 100644 index 0000000..824a12e --- /dev/null +++ b/src/ShopCar/ShopCartServlet.java @@ -0,0 +1,108 @@ +package ShopCar; + +import Connect.Connect; +import JaveBean.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 { + @Override + 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 { + 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("无法删除"); + } + } + } +}