?? dboperate.java
字號(hào):
package cn.com.shoppingonline;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class DbOperate {
/**
* 根據(jù)用戶名得到會(huì)員對(duì)象
*/
public Member getMember(String userId) throws HibernateException {
Session session = HibernateUtil.currentSession();
Member member = null;
Transaction tx = null;
try {
tx = session.beginTransaction();
// 創(chuàng)建查詢對(duì)象
Query query = session.createQuery("from Member where username=:userId");
query.setParameter("userId", userId);
List list = query.list();
if (!list.isEmpty())
member = (Member) list.get(0);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
session.close();
return member;
}
/**
* 根據(jù)商品ID得到商品對(duì)象
*/
public Product getProduct(int Id) throws HibernateException {
Session session = HibernateUtil.currentSession();
Product product = null;
Transaction tx = null;
try {
tx = session.beginTransaction();
// 創(chuàng)建查詢對(duì)象
Query query = session.createQuery("from Product where id="+Id);
List list = query.list();
if (!list.isEmpty())
product = (Product) list.get(0);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
session.close();
return product;
}
/**
* 得到指定數(shù)量的頂端商品對(duì)象
*/
public List getTopProducts(int type) throws HibernateException {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
List list=null;
String strSql;
int dispNUm;
try {
if (type ==1) {
strSql="from Product order by saledate desc";//創(chuàng)建一個(gè)查詢語(yǔ)句,按商品上架時(shí)間排序
dispNUm=4;
}
else{
strSql="from Product order by salecount desc";//創(chuàng)建一個(gè)查詢語(yǔ)句,按商品銷售數(shù)量排序
dispNUm=10;
}
Query query = session.createQuery(strSql); //創(chuàng)建查詢對(duì)象
query.setMaxResults(dispNUm); //記錄集最大個(gè)數(shù)
list = query.list(); //從數(shù)據(jù)庫(kù)取出數(shù)據(jù),并自動(dòng)封裝到List集合中
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
HibernateUtil.closeSession();
return list;
}
/**
* 得到所有符合條件商品對(duì)象
*/
public List getMatchProducts(int sortId,String keyword) throws HibernateException {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
List list=null;
String strSql;
try {
if (sortId ==-1)
strSql="from Product where name like '%" + keyword + "%' ";//創(chuàng)建一個(gè)查詢語(yǔ)句,按關(guān)鍵字查詢
else
strSql="from Product where sortid='" + sortId +"'";//創(chuàng)建一個(gè)查詢語(yǔ)句,查詢指定類別產(chǎn)品
Query query = session.createQuery(strSql); //創(chuàng)建查詢對(duì)象
list = query.list(); //從數(shù)據(jù)庫(kù)取出數(shù)據(jù),并自動(dòng)封裝到List集合中
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
HibernateUtil.closeSession();
return list;
}
/**
* 得到所有商品類別對(duì)象
*/
public List getSorts() throws HibernateException {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
List list=null;
try {
Query query = session.createQuery("from Sort"); //創(chuàng)建查詢對(duì)象
list = query.list();
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
HibernateUtil.closeSession();
return list;
}
/**
* 根據(jù)編號(hào)得到訂單對(duì)象
*/
public Order getOrder(String orderno) throws HibernateException {
Session session = HibernateUtil.currentSession();
Order order = null;
Transaction tx = null;
try {
tx = session.beginTransaction();
// 創(chuàng)建查詢對(duì)象
Query query = session.createQuery("from Order where orderno=:orderno");
query.setParameter("orderno", orderno);
List list = query.list();
if (!list.isEmpty())
order = (Order) list.get(0);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
session.close();
return order;
}
/**
* 得到指定用戶所有訂單對(duì)象
*/
public List getOrders(int userid) throws HibernateException {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
List list=null;
try {
Query query = session.createQuery("from Order where userid=" + userid); //創(chuàng)建查詢對(duì)象
list = query.list();
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
HibernateUtil.closeSession();
return list;
}
/**
* 插入實(shí)體對(duì)象所對(duì)應(yīng)的記錄
*/
public void save(Object obj) throws HibernateException {
Session session = HibernateUtil.currentSession();
if (obj != null) {
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(obj);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
}
session.close();
}
/**
* 修改實(shí)體對(duì)象所對(duì)應(yīng)的記錄
*/
public void update(Object obj) throws HibernateException {
Session session = HibernateUtil.currentSession();
if (obj != null) {
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(obj);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
}
session.close();
}
/**
* 刪除對(duì)象所對(duì)應(yīng)的記錄
*/
public void delete(Object obj) throws HibernateException {
Session session = HibernateUtil.currentSession();
if (obj != null) {
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete(obj);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
}
session.close();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -