📄 productdaoimpl.java
字号:
package com.estore.struts.daoimpl;import java.util.ArrayList;import java.util.Collection;import java.util.List;import org.hibernate.Session;import org.hibernate.Transaction;import com.estore.struts.Globals;import com.estore.struts.dao.ProductDao;import com.estore.struts.entity.Catalog;import com.estore.struts.entity.Product;import com.estore.struts.utils.HibernateSessionFactory;import com.estore.struts.utils.StoreException;public class ProductDaoImpl implements ProductDao { public Collection<Product> findProducts() throws StoreException { List<Product> products = new ArrayList<Product>(); Transaction tx = null; Session session = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); products = session.createQuery("from Product").list(); tx.commit(); return products; } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } } public Product getProductByID(Integer id) throws StoreException { Product product = new Product(); Transaction tx = null; Session session = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); product = (Product) session.get(Product.class, id); return product; } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } } //此方法是从数据库中获得当前页面要实现的结果; public Collection<Product> getProductsByPages(Integer page) throws StoreException { List<Product> products = new ArrayList<Product>(); Transaction tx = null; Session session = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); products = session.createQuery( "from Product p order by p.productid") .setFirstResult((page - 1) * Globals.PAGE_SIZE) .setMaxResults(Globals.PAGE_SIZE) .list(); tx.commit(); return products; } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } } /* * (non-Javadoc) * @see com.estore.struts.dao.ProductDao#getProudctCount() * 从数据库中查找商品的个数; */ public int getProudctCount() throws StoreException { Session session = null; try { session = HibernateSessionFactory.getSession(); return Integer.parseInt(String.valueOf(session.createQuery( "select count(*) from Product").uniqueResult())); } catch (Exception ex) { throw new StoreException(ex); }finally { session.close(); } } /* * (non-Javadoc) * * @see com.estore.struts.dao.ProductDao#searchProduct(java.lang.String, * java.lang.String) 使用模糊查询去数据库中取得用户想要的信息; 根据不同的查询规则来匹配数据库中不同的字段; */ public Collection<Product> searchProduct(String text, String rule) { List<Product> searchproducts = new ArrayList<Product>(); Transaction tx = null; Session session = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); if (rule.equalsIgnoreCase("author")) { searchproducts = session.createQuery( "from Product p where p.author like:author").setString( "author", "%" + text + "%").list(); } else if (rule.equalsIgnoreCase("bookname")) { searchproducts = session.createQuery( "from Product p where p.name like:bookname").setString( "bookname", "%" + text + "%").list(); } else if (rule.equalsIgnoreCase("printer")) { searchproducts = session.createQuery( "from Product p where p.printer like:printer") .setString("printer", "%" + text + "%").list(); } else if (rule.equalsIgnoreCase("createtime")) { searchproducts = session.createQuery( "from Product p where p.createtime like:createtime") .setString("createtime", "%" + text + "%").list(); } else if (rule.equalsIgnoreCase("catalog")) { Catalog catalog = (Catalog) session.createQuery( "from Catalog c where c.catalogid=:id") .setInteger("id", Integer.parseInt(text)).uniqueResult(); searchproducts.addAll(catalog.getProducts()); } tx.commit(); return searchproducts; } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } } public Catalog getCatalog(int id) throws StoreException { Session session = null; try { session = HibernateSessionFactory.getSession(); Catalog catalog = (Catalog) session.get(Catalog.class,id); return catalog; } catch (Exception ex) { throw new StoreException(ex); }finally { session.close(); } } public void addProduct(Product product) { Transaction tx = null; Session session = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); session.save(product); tx.commit(); } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } } public void deleteProduct(Product product) { Transaction tx = null; Session session = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); session.delete(product); tx.commit(); } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } } public void updateProduct(Product product) { Transaction tx = null; Session session = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); session.saveOrUpdate(product); tx.commit(); } catch (Exception ex) { tx.rollback(); throw new StoreException(ex); }finally { session.close(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -