Hibernate和Spring的一个整合
明白一个问题:Hibernate和Spring整合的时候的整合的关键点在哪儿?
Spring和Hibernate整合的时候整合的关键点在 SessionFactory上
以前咋们在做开发的时候 SessionFacory的创建 是由我们自己通过程序写的帮助类来创建的
我们学习了Spring整合Hibernate的时候那么这个对象的创建就由 Spring来完成了
1>:要整合 首先得添加jar包
Hibernate核心功能包 Spring的相关包 Spring和Hibernate整合的包 驱动的相关包 c3p0连接池的相关包
2>:在我们的web.xml中配置我们的Spring
<!--Spring的相关配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/config/bean-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3>:在我们的src/config下面添加配置文件
bean-dao.xml bean-service.xml bean-base.xml hibernate.cfg.xml
4>:最终版
<!--将Hibernate中的连接数据库的相关信息配置到Spring中来 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="acquireIncrement" value="2"></property>
<property name="maxPoolSize" value="100"></property>
<property name="minPoolSize" value="2"></property>
<property name="maxStatements" value="100"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///Spring14"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--Spring和Hibernate整合的关键点是SessionFactory的创建问题 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--将hibernate配置到这里-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!--编写映射文件 -->
<property name="mappingDirectoryLocations">
<list>
<value >classpath:com/zy/pojo</value>
</list>
</property>
</bean>
<!--配置事务 -->
<bean id="txManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="dataSource" ref="dataSource"></property>
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--配置ioc -->
<context:component-scan base-package="com.zy"></context:component-scan>
<!--配置aop -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 应用事物-->
<tx:annotation-driven transaction-manager="txManager"/>