first commit

master
dell 5 years ago
parent 4439ec4e17
commit 2f21a195b2

@ -9,12 +9,12 @@ import java.sql.*;
public class Connect { public class Connect {
private Statement sql=null; private Statement sql=null;
private ResultSet result; private ResultSet result;
// private String url="jdbc:mysql://localhost:3306/userinfo?serverTimezone=GMT%2B8"; // private String url="jdbc:mysql://localhost:3306/userinfo?serverTimezone=GMT%2B8";
// private String user="root"; // private String user="root";
// private String passWord ="1996315211cjr"; // private String passWord ="1996315211cjr";
private Connection con=null; private Connection con=null;
private static Connect connect=new Connect("jdbc:mysql://localhost:3306/userinfo?serverTimezone=GMT%2B8","root","123456"); private static Connect connect=new Connect("jdbc:mysql://localhost:3306/userinfo?serverTimezone=GMT%2B8","root","1996315211cjr");
// private Connect con=new Connect(url,user,passWord); // private Connect con=new Connect(url,user,passWord);
public static Connect getCon(){ public static Connect getCon(){
return connect; return connect;
} }
@ -58,4 +58,3 @@ public class Connect {
return sessionFactory.openSession(); return sessionFactory.openSession();
} }
} }

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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("无法删除");
}
}
}
}
Loading…
Cancel
Save