应用场合:使用SSH框架开发一套应用系统,因为不同的SSH版本+系统架构会导致各种的错误,总结测试了下,成功测试得出本文配置
软件版本:Sping3+Hibernate4+Maven3
主要配置文件内容如下:
1、hibernate.cfg.xml文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 指定数据库所用到的驱动 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- 指定数据库链接的url,hibernate链接的数据库名 -->
<property name="connection.url">jdbc:oracle:thin:@192.168.1.250:1521:orcl</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">你的数据库访问用户名</property>
<!-- 指定连接数据库的用户口令 -->
<property name="connection.password">你的数据库访问密码</property>
<!-- 指定连接池里的最大连接数 -->
<property name="hibernate.c3p0.maxsize">20</property>
<!-- 指定连接池里最小连接数 -->
<property name="hibernate.cp30.minsize">1</property>
<!-- 指定连接池里的超时时常 -->
<property name="hibernate.cp30.timeout">5000</property>
<!-- 指定连接池里最大缓存多少个Statement对象 -->
<property name="hibernate.cp30.max_statements">100</property>
<property name="hibernate.cp30.idle_test_period">3000</property>
<property name="hibernate.cp30.acquire_increment">2</property>
<property name="hibernate.cp30.validate">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 根据需要自动创建数据库表 -->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
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>