?? shoppingcarthibernateimpl.java
字號:
package com.tarena.shoppingcart.dao;import java.util.Collection;import org.hibernate.*;import java.util.*;import com.tarena.shoppingcart.entity.*;import com.tarena.util.HibernateSessionFactory;public class ShoppingCartHibernateImpl implements ShoppingCartDAO { public int generateOrder(Order order) { Session session = HibernateSessionFactory.getSession(); Transaction transaction=session.beginTransaction(); try{ session.save(order); transaction.commit(); return order.getId(); }catch(Exception e){ transaction.rollback(); e.printStackTrace(); throw new RuntimeException(e.getMessage()); } } public Collection listProducts() { Collection products =null; Session session=HibernateSessionFactory.getSession(); Transaction transaction=session.beginTransaction(); try{ Query query =session.createQuery("from Product"); products =query.list(); transaction.commit(); return products; }catch(Exception e){ transaction.rollback(); e.printStackTrace(); return null; } } public User login(String username, String password) { Session session=HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); try{ User user=(User)session.createQuery("from User where name=?").setString(0, username).uniqueResult(); if(password.equals(user.getPassword())){ transaction.commit(); return user; } else { transaction.commit(); return null; } }catch(Exception e){ transaction.rollback(); e.printStackTrace(); return null; } } public User find(String username) { Session session=HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); User user=null; try{ user=(User)session.createQuery("from User where name=?").setString(0, username).uniqueResult(); transaction.commit(); }catch(Exception e){ transaction.rollback(); e.printStackTrace(); return null; } return user; } public void saveUser(User user){ Session session=HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); try{ session.save(user); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } } public void updateUser(User user){ Session session =HibernateSessionFactory.getSession(); Transaction transaction=session.beginTransaction(); try{ session.update(user); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } } public void updateProduct(Product product){ Session session =HibernateSessionFactory.getSession(); Transaction transaction=session.beginTransaction(); try{ session.update(product); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } } public ArrayList<Order> listOrders(Integer id){ ArrayList<Order> orders=new ArrayList<Order>(); Session session =HibernateSessionFactory.getSession(); Transaction transaction =session.beginTransaction(); try{ String hql="from Order where user_id=?"; orders=(ArrayList<Order>)session.createQuery(hql).setInteger(0, id).list(); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } return orders; } public ArrayList<Order> AllListOrders(){ ArrayList<Order> orders=new ArrayList<Order>(); Session session =HibernateSessionFactory.getSession(); Transaction transaction =session.beginTransaction(); try{ String hql="from Order"; orders=(ArrayList<Order>)session.createQuery(hql).list(); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } return orders; } public Order deleteOrderByOrderId(Integer id){ Session session =HibernateSessionFactory.getSession(); Transaction transaction =session.beginTransaction(); Order order=new Order(); try{ String hql="from Order where id=?"; order=(Order)session.createQuery(hql).setInteger(0, id).uniqueResult(); if(order.getStatus()==0) session.delete(order); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } return order; } public void updateOrder(Order order){ Session session =HibernateSessionFactory.getSession(); Transaction transaction=session.beginTransaction(); try{ session.update(order); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } } public void deleteProductById(Integer id){ Session session =HibernateSessionFactory.getSession(); Transaction transaction =session.beginTransaction(); Product product=new Product(); try{ String hql="from Product where id=?"; product=(Product)session.createQuery(hql).setInteger(0, id).uniqueResult(); session.delete(product); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } } public Product findProductById(Integer id){ Session session =HibernateSessionFactory.getSession(); Transaction transaction =session.beginTransaction(); Product product=new Product(); try{ String hql="from Product where id=?"; product=(Product)session.createQuery(hql).setInteger(0, id).uniqueResult(); transaction.commit(); }catch(Exception e){ product=null; transaction.rollback(); throw new RuntimeException(e.getMessage()); } return product; } public void changeStatusById(Integer id){ Session session=HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); Order order=new Order(); try{ String hql="from Order where id=?"; order=(Order)session.createQuery(hql).setInteger(0, id).uniqueResult(); order.setStatus(1); session.update(order); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } } public void saveProduct(Product product){ Session session=HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); try{ session.save(product); transaction.commit(); }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } } public Order findOrderByOrderId(Integer id){ Session session = HibernateSessionFactory.getSession(); Transaction transaction =session.beginTransaction(); Order order=new Order(); try{ String hql="from Order where id=?"; order=(Order)session.createQuery(hql).setInteger(0,id).uniqueResult(); transaction.commit(); return order; }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } } public User findUserByUserId(Integer id){ Session session = HibernateSessionFactory.getSession(); Transaction transaction =session.beginTransaction(); User user=new User(); try{ String hql="from User where id=?"; user=(User)session.createQuery(hql).setInteger(0,id).uniqueResult(); transaction.commit(); return user; }catch(Exception e){ transaction.rollback(); throw new RuntimeException(e.getMessage()); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -