userorderdao.java
来自「《JSP网站开发典型模块与实例精讲》一书光盘源码」· Java 代码 · 共 233 行
JAVA
233 行
package book.example.photoprint.dao;
import java.util.Date;
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.User;
import book.example.photoprint.po.Userorder;
import book.example.photoprint.util.HibernateSessionFactory;
public class UserorderDAO implements IUserorderDAO {
/*
* (non-Javadoc)
*
* @see book.example.photoprint.dao.IUserorderDAO#addUserorder(book.example.photoprint.po.Userorder)
*/
public void addUserorder(Userorder userorder) throws DBException {
Session session;
Transaction tx = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.save(userorder);
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.IUserorderDAO#updateUserorder(book.example.photoprint.po.Userorder)
*/
public void updateUserorder(Userorder userorder) throws DBException {
try{
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
session.update(userorder);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("更新对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
/*
* (non-Javadoc)
*
* @see book.example.photoprint.dao.IUserorderDAO#deleteUserorder(java.lang.String)
*/
public void deleteUserorder(String userorderid) throws DBException {
try {
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
Userorder userorder = (Userorder) session.load(Userorder.class,
userorderid);
session.delete(userorder);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("删除对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
/*
* (non-Javadoc)
*
* @see book.example.photoprint.dao.IUserorderDAO#getUserorder(java.lang.String)
*/
public Userorder getUserorder(String userorderid) throws DBException {
Session session;
Userorder userorder = null;
try {
session = HibernateSessionFactory.currentSession();
userorder = (Userorder) session.get(Userorder.class, userorderid);
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException("获取对象失败!");
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return userorder;
}
/*
* (non-Javadoc)
*
* @see book.example.photoprint.dao.IUserorderDAO#getOrderListByShopId(java.lang.String)
*/
public List getOrderListByShopId(String shopId) throws DBException {
List list = null;
try {
list = HibernateSessionFactory
.currentSession()
.createQuery(
"from book.example.photoprint.po.Userorder order1 where order1.shopid=?")
.setString(0, shopId).list();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException();
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return list;
}
public List getOrderList() throws DBException {
List list = null;
try {
list = HibernateSessionFactory.currentSession().createQuery(
"from book.example.photoprint.po.Userorder").list();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException();
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return list;
}
public List getOrderListByUserId(User user) throws DBException {
List list = null;
try {
list = HibernateSessionFactory
.currentSession()
.createQuery(
"from book.example.photoprint.po.Userorder order1 where order1.user=?")
.setParameter(0, user).list();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException();
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
System.out.println("*****************" + list.size());
return list;
}
public List getOrderListByAlbumId(String albumid) throws DBException {
List list = null;
try {
list = HibernateSessionFactory
.currentSession()
.createQuery(
"from book.example.photoprint.po.Userorder order1 where order1.albumid=?")
.setParameter(0, albumid).list();
} catch (HibernateException e) {
e.printStackTrace();
throw new DBException();
} finally {
try {
HibernateSessionFactory.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
System.out.println("*****************" + list.size());
return list;
}
public static void main(String[] args){
try{
UserorderDAO dao=new UserorderDAO();
Userorder order=new Userorder();
Date date = new Date();
User user=new UserDAO().getUserByUsername("test");
// 生成订单id
String id = String.valueOf(date.getTime());
order.setId(id);
order.setDescription("ddd");
order.setShopid("1");
order.setCreatetime(new Date());
order.setStatus(new Short("1"));
order.setUser(user);
dao.addUserorder(order);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?