五、SSH整合之HibernateTemplate的增删改查

package blog.csdn.net.mchenys.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import blog.csdn.net.mchenys.dao.CustomerDao;
import blog.csdn.net.mchenys.domain.Customer;

//dao层,继承HibernateDaoSupport
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

	/**
	 * 保存客户
	 */
	@Override
	public void save(Customer c) {
		//使用hibernate模板操作数据库
		this.getHibernateTemplate().save(c);
	}


	/**
	 * 修改客户
	 */
	public void update(Customer customer) {
		this.getHibernateTemplate().update(customer);
	}
	
	/**
	 * 通过主键,查询
	 */
	public Customer getById(Long id) {
		return this.getHibernateTemplate().get(Customer.class, id);
	}

	/**
	 * 查询所有
	 */
	public List<Customer> findAll() {
		//HQL语句
		List<Customer> list = (List<Customer>) this.getHibernateTemplate().find("from Customer");
		return list;
	}
	
	/**
	 * 离线条件查询,查询所有的数据,使用QBC的查询
	 */
	public List<Customer> findAllByQBC() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
		// 设置查询条件
		List<Customer> list = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria);
		return list;
	}

	/**
	 * 延迟加载
	 */
	public Customer loadById(long id) {
		return this.getHibernateTemplate().load(Customer.class, id);
	}
}

你可能感兴趣的:(SSH)