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

📄 shoppingcartdaohibernateimpl.java

📁 项目是实现网络购物功能
💻 JAVA
字号:
package com.tarena.shoppingcart.dao.impl;

import java.util.Collection;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.tarena.shoppingcart.dao.ShoppingCartDao;
import com.tarena.shoppingcart.exception.DataAccessException;
import com.tarena.shoppingcart.model.Order;
import com.tarena.shoppingcart.model.User;
import com.tarena.shoppingcart.util.HibernateUtil;

public class ShoppingCartDaoHibernateImpl implements ShoppingCartDao {

	public Collection listProducts() throws DataAccessException {
		try {
			Session s = HibernateUtil.currentSession();
			return s.createQuery("from Product").list();
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DataAccessException(e);
		}
	}

	public User login(String name, String password) throws DataAccessException {
		try {
			Session s = HibernateUtil.currentSession();
			return (User) s.createQuery("from User u " +
					"where u.name=:name and " +
					"password=:password")
					.setString("name", name)
					.setString("password", password)
					.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DataAccessException(e);
		}
	}

	public int generateOrder(Order order) throws DataAccessException {
		try {
			Session s = HibernateUtil.currentSession();
			s.save(order);
			return order.getId();
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DataAccessException(e);
		}
	}

}

⌨️ 快捷键说明

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