⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userdaoimpl.java

📁 java学习的必要的资料,servlet的说明很好
💻 JAVA
字号:
package com.estore.struts.daoimpl;import java.util.Collection;import org.hibernate.Session;import org.hibernate.Transaction;import com.estore.struts.dao.UserDao;import com.estore.struts.entity.Order;import com.estore.struts.entity.OrderItem;import com.estore.struts.entity.User;import com.estore.struts.entity.UserItem;import com.estore.struts.utils.HibernateSessionFactory;import com.estore.struts.utils.StoreException;public class UserDaoImpl implements UserDao {		public UserDaoImpl() {		super();		// TODO Auto-generated constructor stub	}	public void addUser(User user) throws StoreException {		Transaction tx = null;		Session session = null;		try {			session = HibernateSessionFactory.getSession();			tx = session.beginTransaction();			session.save(user);			tx.commit();		} catch (Exception ex) {			tx.rollback();			throw new StoreException(ex);		}finally {			session.close();		}	}	public void deleteUser(User user) throws StoreException {		Transaction tx = null;		Session session = null;		try {			session = HibernateSessionFactory.getSession();			tx = session.beginTransaction();			session.delete(user);			tx.commit();		} catch (Exception ex) {			tx.rollback();			throw new StoreException(ex);		}finally {			session.close();		}	}	public void modify(User user) throws StoreException {		Transaction tx = null;		Session session = null;		try {			session = HibernateSessionFactory.getSession();			tx = session.beginTransaction();			session.update(user);			tx.commit();		} catch (Exception ex) {			tx.rollback();			throw new StoreException(ex);		}finally {			session.close();		}	}	public boolean isUserExist(String username) throws StoreException {		Session session = null;		try {			session = HibernateSessionFactory.getSession();			User user = (User) session.createQuery(					"from User u where u.username = '" + username + "'")					.uniqueResult();			if (user == null) {				return false;			} else {				return true;			}		} catch (Exception ex) {			throw new StoreException(ex);		}finally {			session.close();		}	}	public User getUserByName(String name) throws StoreException {		Session session = null;		try {			session = HibernateSessionFactory.getSession();			User user = (User) session.createQuery(					"from User u where u.username = '" + name + "'")					.uniqueResult();			return user;		} catch (Exception ex) {			throw new StoreException(ex);		}finally {			session.close();		}	}		public User getUserById(int userId) throws StoreException {		Session session = null;		try {			session = HibernateSessionFactory.getSession();			User user = (User) session.createQuery(					"from User u where u.id = '" + userId + "'")					.uniqueResult();			return user;		} catch (Exception ex) {			throw new StoreException(ex);		}finally {			session.close();		}	}	public Collection<UserItem> getUserItem(User user) throws StoreException {		Session session = null;		try {			session = HibernateSessionFactory.getSession();			Collection<UserItem> useritems = session.createQuery(					"from UserItem o  where o.user =:user").setEntity("user",					user).list();			return useritems;		} catch (Exception ex) {			throw new StoreException(ex);		} finally {			session.close();		}	}	/*	 * (non-Javadoc)	 * 	 * @see com.estore.struts.dao.UserDao#addUserItem(com.estore.struts.entity.UserItem)	 *      将新增加UserItem保存数据库中,其中保存了用户的信息:id	 */	public void addUserItem(UserItem useritem) throws StoreException {		Transaction tx = null;		Session session = null;		try {			session = HibernateSessionFactory.getSession();			tx = session.beginTransaction();			session.save(useritem);			tx.commit();		} catch (Exception ex) {			tx.rollback();			throw new StoreException(ex);		}finally {			session.close();		}	}	public UserItem getUserItemById(Integer useritemid) throws StoreException {		Session session = null;		try {			session = HibernateSessionFactory.getSession();			UserItem useritem = (UserItem) session.get(UserItem.class,useritemid);			return useritem;		} catch (Exception ex) {			throw new StoreException(ex);		}finally {			session.close();		}	}	public void deleteUserItem(Integer id) throws StoreException {		Session session = null;		Transaction tx = null;		try {			session = HibernateSessionFactory.getSession();			tx = session.beginTransaction();			UserItem useritem = getUserItemById(id);			session.delete(useritem);			tx.commit();		} catch (Exception ex) {			tx.rollback();			throw new StoreException(ex);		}finally {			session.close();		}	}	public Collection<User> alluser() {		Session session = null;		try {			session = HibernateSessionFactory.getSession();			Collection<User> users = session.createQuery("from User u left join fetch u.orders").list();			return users;		} catch (Exception ex) {			throw new StoreException(ex);		} finally {			session.close();		}	}	//	public static void main(String[] args){//		UserDaoImpl ud=new UserDaoImpl();//		Collection users=ud.alluser();//		for(Object o:users){//			User u=(User)o;//			Collection orders=u.getOrders();//			for(Object o1:orders){//				Order order=(Order)o1;//				System.out.println(order.getOrderItems().size());//			}//		}//	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -