spring-hibernate
原则上尽量要使用spring下载包里lib目录里的jar文件,否则造成sping依赖的jar文件版本不一致,可能产生一些莫名奇妙的异常
spring下载包里lib的commons-attributes-compiler.jar如果被拷贝到项目lib目录下也许会导致无法启动tomcat,那么删除该jar文件或者修改jar文件里的META-INF/MANIFEST.MF文件
尽量要使用spring下载包里lib目录里的hibernate3.jar和ehcache.jar文件(Hibernate第三方二级缓存实现)
如果报 java.lang.NoClassDefFoundError: antlr/ANTLRException异常,则在工程lib目录下加入antlr.jar
如果在项目里使用了spring的动态代理,则在工程lib目录下加入cglib.jar,Hibernate使用延迟加载也会使用该jar文件
如果需要DBCP连接池的话,则在工程lib目录下加入commons-dbcp.jar、commons-pool.jar和commons-collections.jar
如果使用JTA事务,则在工程lib目录下加入jta.jar
其他可能会用到jar文件:dom4j.jar、log4j.jar、数据库驱动等
如果在spring的配置文件中配置了事务,那么就不需要显示的close session
参考:
applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/blog?characterEncoding=GBK</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
<property name="maxActive" value="40" />
<property name="maxIdle" value="20" />
<property name="maxWait" value="300" />
<property name="defaultAutoCommit" value="false"/>
<!-- 回收被遗弃的(一般是忘了释放的)数据库连接到连接池中 -->
<property name="removeAbandoned" value="true"/>
<!-- 数据库连接过多长时间不用将被视为被遗弃而收回连接池中 -->
<property name="removeAbandonedTimeout" value="300"/>
</bean>
<!-- 整合Hibernate3 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>hibernate/Users.hbm.xml</value>
</list>
</property>
</bean>
<!-- 事务管理类 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 事务拦截器 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 事务代理创建器 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Business,*Service</value>
<!--
<list>
<ref local="service1"/>
<ref bean="service2"/>
</list>
-->
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<import resource="crazymustang.xml"/>
</beans>