应用场合:使用SSH框架开发一套应用系统,因为不同的SSH版本+系统架构会导致各种的错误,总结测试了下,成功测试得出本文配置
软件版本:Sping3+Hibernate4+Maven3
主要配置文件内容如下:
1、hibernate.cfg.xml文件内容如下
2、spring-common.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 启用spring注解支持 -->
<context:annotation-config/>
<!--读取数据库的properties文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/db.properties"/>
</bean>
<!--配数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.1.250:1521:orcl" />
<property name="username" value="您的数据库用户" />
<property name="password" value="您的数据库密码" />
</bean>
<!--配置Hibernate4的SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--配置扫描注解实体的包-->
<property name="packagesToScan" value="com.mycom.*.entity">
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置哪些类哪些方法使用哪种事务传播特性 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置Aop的切入位置 -->
<aop:config>
<!-- 对业务逻辑层实施事务 -->
<aop:pointcut id="ServiceMethod"
expression="execution(* com.mycom.*.service.*.*(..))"/>
<aop:advisor pointcut-ref="ServiceMethod" advice-ref="txadvice"/>
</aop:config>
<!--配置根据注解,自动注入Log对象-->
<bean id="logBeanPocessor" class="com.mycom.basic.annotation.LogBeanPostProcessor" lazy-init="false" />
</beans>