一:數據源的配置
1、通過JNDI配置數據源
1.在tomcat context.xml中配置數據源
<Resource name="jdbc/ds" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:orcl" username="shuaige" password="123456"
maxActive="20" maxIdle="10" />
2.在applicationContext中引用創建datasourece bean
<!-- 通過JNDI配置DataSource -->
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/jboa</value>
</property>
</bean>
2、通過Properties文件配置數據連接信息
1.創建jdbc.properties文件
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=shuaige
jdbc.password=123456
2.加載配置文件
<!-- 第一種、加載配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 第二種、加載配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc:properties</value>
</property>
</bean>
3.在applicationContext中引用創建datasourece bean
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
二、SessionFactory
<!-- 定義SessionFactory Bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--為LocalSessionFactoryBean注入定義好的數據源-->
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<!--添加Hibernate配置參數-->
<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>
</props>
</property>
<!--添加對象關系映射文件 掃描這個包下的所有配置文件-->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/entity/</value>
</list>
</property>
</bean>
<!-- 定義事務管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 通過<tx:advice>標簽定義事務增強,並指定事務管理器 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 定義屬性,聲明事務規則 -->
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="do*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 定義切面 -->
<aop:config>
<!-- 定義切入點 -->
<aop:pointcut expression="execution(* com.biz.impl.*.*(..))" id="serviceMethod"/>
<!-- 將事務增強與切入點組合 -->
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>