SSH框架整合 - 05 Hibernate的模板的常用的方法

比较简单直接贴代码了

注意使用延时加载的时候会有问题可以参考:https://blog.csdn.net/alexzt/article/details/84568111

package dao;

import java.util.List;

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

import domain.Customer;

/**
 * 继承HibernateDaoSupport 简化开发操作
 * @author Administrator
 *
 */
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

	public void add(Customer cust) {
		System.out.println("dao add");
		//从父类获得hibernate模板保存customer对象
		this.getHibernateTemplate().save(cust);
	}

	public void delete(Customer cust) {

		this.getHibernateTemplate().delete(cust);

	}

	@Override
	public Customer findById(long id) {
		return this.getHibernateTemplate().get(Customer.class, id);
	}

	@Override
	public List findByName(Customer cust) {
		
		List list = (List) this.getHibernateTemplate().find("from Customer where cust_name like ?", "%"+cust.getCust_name()+"%");
		
		return list;
	}
	
	/*
	 * 获得所有客户
	 * (non-Javadoc)
	 * @see dao.CustomerDao#findAll()
	 */
	@Override
	public List findAll() {
		
		List list = (List) this.getHibernateTemplate().find("from Customer");
		
		return list;
	}

	/*
	 * 更新客户的方法
	 * (non-Javadoc)
	 * @see dao.CustomerDao#update(domain.Customer)
	 */
	public void update(Customer cust) {
		this.getHibernateTemplate().update(cust);
	}

	/*
	 *	演示延迟加载
	 * (non-Javadoc)
	 * @see dao.CustomerDao#load(domain.Customer)
	 */
	@Override
	public Customer load(long l) {
		Customer load = this.getHibernateTemplate().load(Customer.class, l);
		return load;
	}
	
}

 

你可能感兴趣的:(JAVA,SSH,练习,JAVA_WEB)