shopdao.java
来自「《JSP网站开发典型模块与实例精讲》一书光盘源码」· Java 代码 · 共 222 行
JAVA
222 行
package book.example.photoprint.dao;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import book.example.photoprint.exception.DBException;
import book.example.photoprint.po.Shop;
import book.example.photoprint.po.User;
import book.example.photoprint.util.HibernateSessionFactory;
public class ShopDAO implements IShopDAO {
/* (non-Javadoc)
* @see book.example.photoprint.dao.IShopDAO#addShop(book.example.photoprint.po.Shop)
*/
public void addShop(Shop shop) throws DBException {
Session session;
Transaction tx = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.saveOrUpdate(shop);
tx.commit();
} catch (HibernateException e) {
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
e.printStackTrace();
throw new DBException("保存对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
/* (non-Javadoc)
* @see book.example.photoprint.dao.IShopDAO#updateShop(book.example.photoprint.po.Shop)
*/
public void updateShop(Shop shop) throws DBException {
Session session;
Transaction tx = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.update(shop);
tx.commit();
} catch (HibernateException e) {
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
e.printStackTrace();
throw new DBException("更新对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
/* (non-Javadoc)
* @see book.example.photoprint.dao.IShopDAO#deleteShop(java.lang.String)
*/
public void deleteShop(String shopid) throws HibernateException {
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
Shop shop = (Shop) session.load(Shop.class, shopid);
session.delete(shop);
tx.commit();
HibernateSessionFactory.closeSession();
}
/* (non-Javadoc)
* @see book.example.photoprint.dao.IShopDAO#getShop(java.lang.String)
*/
public Shop getShop(String shopid) throws DBException {
Session session;
Shop shop = null;
try {
session = HibernateSessionFactory.currentSession();
shop = (Shop) session.get(Shop.class, shopid);
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("获取对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return shop;
}
/* (non-Javadoc)
* @see book.example.photoprint.dao.IShopDAO#getShopByShopname(java.lang.String)
*/
public Shop getShopByShopname(String shopname) throws DBException {
Session session;
Shop shop = null;
try {
session = HibernateSessionFactory.currentSession();
List list = session
.createQuery(
"from book.example.photoprint.po.Shop shop where shop.shopname=:shopname")
.setString("shopname", shopname).list();
if (list != null && list.size() > 0) {
shop = (Shop) list.get(0);
}
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("获取对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return shop;
}
/*
* 获得未经审批的加盟店列表
*
* (non-Javadoc)
* @see book.example.photoprint.dao.IShopDAO#list()
*/
public List list() throws DBException {
Session session;
List list = null;
try {
session = HibernateSessionFactory.currentSession();
list = session
.createQuery(
"from book.example.photoprint.po.Shop shop where shop.status=1")
.list();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("获取对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return list;
}
/**
* 获得已经审批通过的加盟店列表
*/
public List list2() throws DBException {
Session session;
List list = null;
try {
session = HibernateSessionFactory.currentSession();
list = session
.createQuery(
"from book.example.photoprint.po.Shop shop where shop.status=2")
.list();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("获取对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return list;
}
/* (non-Javadoc)
* @see book.example.photoprint.dao.IShopDAO#getShopByLoginname(java.lang.String)
*/
public Shop getShopByLoginname(String loginname) throws DBException {
Session session;
Shop shop = null;
try {
session = HibernateSessionFactory.currentSession();
List list = session
.createQuery(
"from book.example.photoprint.po.Shop shop where shop.loginname=:loginname")
.setString("loginname", loginname).list();
if (list != null && list.size() > 0) {
shop = (Shop) list.get(0);
}
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("获取对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return shop;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?