友情提示:这篇博客主要是学习方法的总结,而不是讨论搭建springMVC的过程,想要了解搭建技术的同学可以绕开了......
上一篇springmvc搭建并没有整合HIbernate或Ibatis ORM 框架。这篇博客主要关注怎么在SpringMVC里面整合HIbernate框架,并使用c3p0连接池。
今天第一次搭建整合了HIbernate的SpringMVC框架,于是参考了以前的Spring3+HIbernate3的搭建方法。但是我使用的是Spring4+HIbernate4。
然后就雪崩了.....
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<context:component-scan base-package="org.expc"/>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:expc.properties</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="org.expc.entity"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">true </prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop> -->
</props>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置 Spring 的声明式事务 -->
<!-- 1.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2.配置事务属性 , 需要事务管理器 -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
添加下面此行后改变了 事务的隔离级别
<tx:method name="purchase" propagation="REQUIRES_NEW"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
3.配置事务切点, 并把切点和事务属性联系起来
<aop:config>
<aop:pointcut expression="execution(* com.yl.spring.hibernate.service.*.*(..))"
id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config> -->
<!-- ... -->
</beans>
这次搭建过程中遇到两个问题,
1.参考spring3+hibernate3的搭建方法
2.一股气的把Hibernate的所有可选jar包全导入,出现了NoClassDefError,而且配置的C3P0连接池也出各种问题。
对于第一个问题,不同的spring版本搭建的方法不一样,后面我百度了一些spring4的搭建配置,copy过来还是出现第2个问题。然后我就怀疑他们的配置有问题,最终还是参考官方文档搭建的,应该一开始就跟着官方文档做,可以绕过许多麻烦。
对于第二个问题,有可能是jar包冲突,所以我重新导入HIbernate的jar包,第一次只导必要的(required目录下的),如果console提示ClassNotFound***,再去找对应的jar包,最终果然成功了。