spring整合hibernate

Spring整合Hibernate的方式比较灵活,比较多样。主要是在Spring提供的org.springframework.orm.hibernate3.LocalSessionFactoryBean中进行整合,这个Bean提供了多种整合的方法:

  1.可以通过<property name="hibernateProperties">标签将hibernate的配置信息以property的方式写入.

复制代码
 1     <property name="hibernateProperties">
 2             <props>
 3                 <prop key="hibernate.dialet">org.hibernate.dialect.MySQLDialect</prop>
 4                 <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
 5                 <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/book</prop>
 6                 <prop key="hibernate.connection.username">yuan</prop>
 7                 <prop key="hibernate.connection.password">hanyuan9113</prop>
 8                 <prop key="hibernate.show_sql">true</prop>
 9                 <prop key="hibernate.connection.autocommit">true</prop>
10             </props>
11         </property>
复制代码

  2.可以通过<property name="configLocation">标签直接读取hibernate.cfg.xml配置信息

1      <property name="configLocation">
2             <value>classpath:hibernate.cfg.xml</value>
3        </property>

  3.可以通过<property name="dataSource">标签指定连接池,连接池中有连接数据库的信息

1     <property name="dataSource">
2             <ref bean="myDataSource" />
3         </property>

 

Spring整合Hibernate的步骤:

一:用以上三种方法之一配置Hibernate信息,装配LocalSessionFactoryBean

复制代码
 1 <bean id="sessionFactory"
 2         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 3         
 4         <property name="hibernateProperties">
 5             <props>
 6                 <prop key="hibernate.dialet">org.hibernate.dialect.MySQLDialect</prop>
 7                 <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
 8                 <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/book</prop>
 9                 <prop key="hibernate.connection.username">yuan</prop>
10                 <prop key="hibernate.connection.password">hanyuan9113</prop>
11                 <prop key="hibernate.show_sql">true</prop>
12                 <prop key="hibernate.connection.autocommit">true</prop>
13             </props>
14         </property>
15 
16         <property name="mappingResources">
17             <list>
18                 <value>com/sunflower/entity/Sutdent.hbm.xml</value>
19             </list>
20         </property>
21 </bean>
复制代码

其中<property name="mappingResources">属性是配置映射文件的。如果有很多映射文件要配置,用这种方法就要为每个映射文件书写配置信息,这样将会非常麻烦。Spring的org.springframework.orm.hibernate3.LocalSessionFactoryBean提供了一个<property name="mappingDirectoryLocations">属性,将所有配置文件放置到一个统一的地方就能一次性进行配置。例如:

1       <property name="mappingDirectoryLocations">
2             <list>
3                 <value>classpath:com/sunflower/entity</value>
4             </list>
5         </property>

将一次性配置com.sunflower.entity包下的所有映射文件。

 

二:装配org.springframework.orm.hibernate3.HibernateTemplate

1     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
2         <property name="sessionFactory">
3             <ref bean="sessionFactory" />
4         </property>
5     </bean>

HibernateTemplate类是Spring提供给我们进行Hibernate持久层操作的类它对增删查改方法进行了封装,通过这个类我们很方便就能操作数据库<property name="sessionFactory">标签配置LocalSessionFactoryBean

三:装配自定义DAO

1   <bean id="studentDao" class="com.sunflower.daoimp.StudentDaoImp">
2         <property name="hibernateTemplate">
3             <ref bean="hibernateTemplate" />
4         </property>
5     </bean>

为每个Dao的实现类配置一个HibernateTemplate,然后在Spring配置文件中进行装配,这样就可以使用这个HibernateTemplate进行持久层的操作了。

 


StudentDaoImp.java:

复制代码
 1 public class StudentDaoImp extends StudentDaoAdapter {
 2     @Override
 3     public void saveStudent(Student student) {
 4         this.hibernateTemplate.save(student);
 5     }
 6 
 7     @Override
 8     public Student getStudent(Student student) {
 9         return this.hibernateTemplate.get(Student.class, student.getSno());
10     }
11 }
复制代码

进行测试,Test.java:

复制代码
 1 public class Test {
 2 
 3     @org.junit.Test
 4     public void saveStudent() {
 5         ApplicationContext context = new ClassPathXmlApplicationContext(
 6                 "applicationContext.xml");
 7         StudentDao studentDao = (StudentDao) context.getBean("studentDao");
 8 
 9         Student student = new Student();
10 
11         student.setCno(4);
12         student.setName("喜爱啸");
13         student.setScore(70);
14 
15         studentDao.saveStudent(student);
16 
17     }
18 
19 //    @org.junit.Test
20     public void getStudent() {
21         ApplicationContext context = new ClassPathXmlApplicationContext(
22                 "applicationContext.xml");
23         StudentDao studentDao = (StudentDao) context.getBean("studentDao");
24 
25         Student student = new Student();
26         student.setSno(15);
27         Student s = studentDao.getStudent(student);
28 
29         System.out.println("name:" + s.getName());
30     }
31 
32 }
复制代码

这里需要注意的是,因为这里没有用到事务处理,Hibernate默认是不会自动提交事务的,所以刚开始测试的时候出现数据插入不了数据库的情况。后来查了资料,知道要将事务设置为自动提交才能将数据插入到数据库中,这里只是为了测试,就改了过来。如顶上所写<prop key="hibernate.connection.autocommit">true</prop>

(转)http://www.cnblogs.com/hanyuan/archive/2012/10/06/2713047.html
原文地址:https://www.cnblogs.com/yaoxing92/p/3020557.html