Hibernate 数据访问层

MovieDao

package dao;

import java.util.List;

import org.hibernate.HibernateException;

import bean.Movie;
import bean.MovieCondition;

public interface MovieDao {
	
	//查找全部
	public List<Movie>  getAll() throws HibernateException;
	//根据条件动态查找
	public List<Movie>  searchByCondition(MovieCondition  condition) throws HibernateException;
	//添加
	public void save(Movie movie) throws HibernateException;
	//根据条件动态查找,因为有价格的范围
	public List<Movie> search(String actor,String director,String titile,double priceBegin,double priceEnd,int typeid) throws HibernateException;
}

 

 

MovieDaoImpl

package dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import util.HibernateSessionFactory;

import bean.Movie;
import bean.MovieCondition;
import dao.MovieDao;

public class MovieDaoImpl implements MovieDao {
	// 查找全部
	public List<Movie> getAll() throws HibernateException {
		Session session = HibernateSessionFactory.getSession();
		Criteria criteria = session.createCriteria(Movie.class);
		List<Movie> movies = criteria.list();
		return movies;
	}
	// 添加
	public void save(Movie movie) throws HibernateException {

		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		session.save(movie);
		tx.commit();
		HibernateSessionFactory.closeSession();

	}
	// 根据条件动态查找
	public List<Movie> searchByCondition(MovieCondition condition)
			throws HibernateException {
		StringBuffer hql = new StringBuffer("from Movie where 1=1");
		if (null != condition.getTitile()) {
			hql.append(" AND titile like :titile");
		}
		if (null != condition.getActor()) {
			hql.append(" AND actor like :actor");
		}
		if (null != condition.getDirector()) {
			hql.append(" AND director like :director");
		}
		if (null != condition.getType()) {
			hql.append(" AND type=:type");
		}
		if (null != condition.getMinPrice()) {
			hql.append(" AND price>=:minPrice");
		}
		if (null != condition.getMinPrice()) {
			hql.append(" AND price<=:maxPrice");
		}
		// 根据条件动态拼接hql
		Session session = HibernateSessionFactory.getSession();
		Query query = session.createQuery(hql.toString());
		// 整个参数放入
		query.setProperties(condition);
		List<Movie> movies = query.list();
		return movies;
	}
	// 根据条件动态查找,因为有价格的范围
	public List<Movie> search(String actor, String director, String titile,
			double priceBegin, double priceEnd, int typeid)
			throws HibernateException {
		DetachedCriteria dc = DetachedCriteria.forClass(Movie.class);
		if (actor != null) {
			dc.add(Restrictions.eq("actor", actor));
		}
		if (director != null) {
			dc.add(Restrictions.eq("director", director));
		}
		if (titile != null) {
			dc.add(Restrictions.eq("titile", titile));
		}
		Criteria critera = dc.getExecutableCriteria(HibernateSessionFactory
				.getSession());
		List<Movie> movies = critera.list();
		return movies;
	}
}

 

TypeDao

package dao;

import java.util.List;

import org.hibernate.HibernateException;

import bean.Type;

public interface TypeDao {

	// 保存和查询需要加装所有的类型

	// 查找全部
	public List<Type> getAll() throws HibernateException;

	// 根据id返回type
	public Type getTypeById(int id) throws HibernateException;

}

 

TypeDaoImpl

package dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import util.HibernateSessionFactory;
import bean.Type;
import dao.TypeDao;

public class TypeDaoImpl implements TypeDao{

	public List<Type> getAll() throws HibernateException {
		List<Type> types=null;
		String hql="from Type";
		Session session=HibernateSessionFactory.getSession();
		Query query=session.createQuery(hql);
		types=query.list();
		HibernateSessionFactory.closeSession();
		return types;
	}

	// 根据id返回type
	public Type getTypeById(int id) throws HibernateException{
		Session session = HibernateSessionFactory.getSession();
		String hql = "from Type where id=:id";
		Query query= session.createQuery(hql);
		query.setInteger("id", id);
		Type type = (Type) query.uniqueResult(); 
		return type;
	}

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值