Hibernate的常用模板的使用

  • 保存操作
    save(Object obj);

  • 修改操作
    update(Object obj);

  • 删除操作
    delete(Object obj);

  • 查询操作

    • 查询一个
      get(Class c,Serializable id);
      load(Class c,Serializable id);

    • 查询多个
      List find(String hql,Object… args);
      List findByCriteria(DetachedCriteria dc);
      List findByCriteria(DetachedCriteria dc,int firstResult,int maxResults);
      List findByNamedQuery(String name,Object… args);

实际应用,按照之前SSH整合的基本环境来:

/*
* 客户管理的Dao层的实现
* */
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
    @Override
    public void save(Customer customer) {
        System.out.println("dao中的save方法被调用了...");
        this.getHibernateTemplate().save(customer);
    }

    @Override
    public void update(Customer customer) {
        this.getHibernateTemplate().update(customer);
    }

    @Override
    public void delete(Customer customer) {
        this.getHibernateTemplate().delete(customer);
    }

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

    @Override
    public List fingAllByHQL() {
        List list = (List) this.getHibernateTemplate().find("from Customer");
        return list;
    }

    @Override
    public List fingAllByQBC() {
        //做分页的时候尽量使用QBC,比较方便
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
        List list = (List) this.getHibernateTemplate().findByCriteria(detachedCriteria);
        return list;
    }
}

你可能感兴趣的:(Java)