所谓spring对hibernate的集成,其实它就做了两方面的工作:
      1.提供了可以在配置文件中进行配置的完成了对数据源和SessionFactory信息进行封装的bean类。比如:对数据源进行封装的类BasicDataSource,对SessionFactory进行封装的类LocalSessionFactoryBean。
      2.提供了对hibernate的一些公共操作进行了封装的类。比如org.springframework.orm.hibernate3.HibernateTemplate。这里,HibernateTemplate并没有完成任何实质性的功能,它也只是对Hibernate中session方法的对应调用。并且,对于Hibernate中session的大多数方法,在HibernateTemplate中都有与之对应的方法。它还提供了一个通用的方法excute()来处理一般情况,但是要求传入一个HibernateCallback接口类型的对象,使程序员可以在实现方法doInHibernate(Session session)中借助spring给提供的session来自己进行Hibernate操作。总之,spring就是要省去我们手动创建hibernate中session的操作以及事务管理。
 
实体类:
public class Teacher implements Serializable {

   private static final long serialVersionUID = 1L;

   private Integer id;
   private String name;
   private String email;
   private Date birthday;
   private Double salary;
     //各属性的setter.getter方法
}
映射文件:
< hibernate-mapping package ="com.cernet.spring.hibernate.entity" >
   < class name ="Teacher" table ="s_spring_teacher" >
     < id name ="id" column ="id" >
       < generator class ="sequence" >
         < param name ="sequence" >s_spring_teacher_id_seq param >
       generator >
     id >
     < property name ="name" column ="name" type ="string" > property >
     < property name ="email" column ="email" type ="string" > property >
     < property name ="birthday" column ="birthday" type ="date" > property >
     < property name ="salary" column ="salary" type ="double" > property >
   class >
hibernate-mapping >
Dao实现类:
public class TeacherDaoImpl implements TeacherDao {
   private HibernateTemplate template = null;

   public HibernateTemplate getTemplate() {
     return template;
  }

   public void setTemplate(HibernateTemplate template) {
     this.template = template;
  }

  @Override
   public void delete(Integer id) {
    Teacher t = (Teacher) template.get(Teacher. class, id); // 到数据库中查指定id对应的记录
     if (t != null) {
      template.delete(t);
    }
  }

  @Override
   public void delete(Teacher t) {
     if (t.getId() != null) {
      template.delete(t);
    }
  }

  @Override
   public Teacher getTeacherById(Integer id) {
     return (Teacher) template.get(Teacher. class, id);
  }

  @SuppressWarnings( "unchecked")
  @Override
   public List getTeachersByName(String name) {
    String hql = "from Teacher as t where t.name=?";
    Object args[] = { name }; //对应占位符的参数值
     return (List) template.find(hql, args);
  }

  @SuppressWarnings( "unchecked")
  @Override
   public List getTeachers() {
    String hql = "from Teacher";
     return template.find(hql);
  }

  @Override
   public void insert(Teacher t) {
    template.saveOrUpdate(t);
  }

  @Override
   public void update( final Teacher t) {
    template.execute( new HibernateCallback() {
       public Object doInHibernate(Session session)
           throws HibernateException, SQLException {
        session.saveOrUpdate(t); //这里借助spring给提供的session来完成hibernate操作
         return null;
      }
    });
  }
}
配置文件:

  
  "dataSource"

     class= "org.apache.commons.dbcp.BasicDataSource">
    "driverClassName"
>
      oracle.jdbc.driver.OracleDriver
    
    "url">
      jdbc:oracle:thin:@localhost:1521:orcl10
    

    "username">
      scott
    

    "password">
      yf123
    

  
  
  "sessionFactory"
     class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    "dataSource">
      "dataSource" />
    

    "hibernateProperties">
      
        "hibernate.show_sql"> true
        "hibernate.format_sql"> true
        "hibernate.dialect">
          org.hibernate.dialect.Oracle9Dialect
        

      

    

    "mappingResources">
      
        
          com/cernet/spring/hibernate/entity/Teacher.hbm.xml
        

      

    

  

  
  "template"
     class= "org.springframework.orm.hibernate3.HibernateTemplate">
    "sessionFactory">
      "sessionFactory" />
    

  

  "dao"
     class= "com.cernet.spring.hibernate.dao.TeacherDaoImpl">
    "template">
      "template" />