spring整合hibernate的步骤:
1.创建持久化类和映射文件;
2.创建hibernate的配置文件;
3.写spring的配置文件,引入sessionFactory
4.测试sessionFactory;
5.写dao和service层的接口和类。
因为sessionFactory要注入dataSource,而hibernate提供的SessionFactoryImpl没有提供符合spring注入的方式(构造器注入和setter注入)。所以,hibernate的SessionFactoryImpl类不能用,需使用spring提供的类。
引入sessionFactory的两种常用方式:
方式一:
<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
方式二:
<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--
映射文件所在的路径
-->
<property name="mappingDirectoryLocations">
<list>
<!--
spring容器会去该包及子包下搜索所有的映射文件
-->
<value>com/itheima12/spring/hibernate/transaction/domain</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>