Hibernate与Spring整合的过程中,
我们让dao实现类继承HibernateDaoSupport的方法去获取hibernate模板,
dao实现类要访问数据库需要Hibernate的 sessionFactory
dao层获取sessionFactory有两种方法:
1.第一种:
在Spring配置文件中配置dao实现类的bean,然后在该bean中引用sessionFactory,例如:
<!-- 配置dao层的类 -->
<bean id="employeeDao" class="com.ssh.dao.impl.EmployeeDaoImpl">
<!-- 注入sessionFactory,dao需要先继承HibernateDaoSupport才会有sessionFactory属性 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置 hibernate 的sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 配置数据源属性 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate 的基本属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载 hibernate 映射文件 -->
<property name="mappingResources">
<list>
<value>com/ssh/entity/Department.hbm.xml</value>
<value>com/s