applicationContext.xml重要配置

<!-- 加载 hibernate.properties 文件-->
<bean id="propertyConfig"   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">		
	<property name="locations">            
               <value>/WEB-INF/jdbc.properties</value>
        </property>
</bean>

 JDBC连接配置

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
	<property name="username" value="root"></property>
	<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			<prop key="hibernate.show_sql">true</prop>
		</props>
	</property>
	<property name="mappingResources">
		<list>
			<value>com/ssh/bean/Person.hbm.xml</value>
		</list>
	</property>
</bean>

 连接池配置:

<!--c3p0 连接池-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass">
			<value>${hibernate.connection.driver_class}</value>
		</property>
		<property name="jdbcUrl">
			<value>${hibernate.connection.url}</value>
		</property>
			<property name="user">
		 		<value>${hibernate.connection.username}</value>
			</property>
		<property name="password">
			<value>${hibernate.connection.password}</value>
		</property>
		
		<!-- 连接池中保留的最小连接数. -->
		<property name="minPoolSize">
			<value>5</value>
		</property>
		<!-- 连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize">
		 <value>100</value>
		</property>
		<!-- 初始化时获得的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize">
		 <value>5</value>
		</property>
		<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime">
		 <value>60</value>
		</property>
		<!-- 当连接池中的连接耗尽的时候c3p0一次同时获得的连接数。Default: 3 -->
		<property name="acquireIncrement">
		 <value>3</value>
		</property>
		
		<!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
		  属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
		  假如maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
		<property name="maxStatements">
			<value>0</value>
		</property>

		<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->		
		<property name="idleConnectionTestPeriod">
			<value>60</value>
		</property>
		
		<!-- 定义在从数据库获得新连接失败后反复尝试的次数。Default: 30 -->
		<property name="acquireRetryAttempts">
			<value>30</value>
		</property>
		
		<!-- 获得连接失败将会引起所有等待连接池来获得连接的线程抛出异常。但是数据源仍有效
		  保留,并在下次调用getConnection()的时候继续尝试获得连接。假如设为true,那么在尝试
		  获得连接失败后该数据源将申明已断开并永久关闭。Default: false-->
		<property name="breakAfterAcquireFailure">
			<value>true</value>
		</property>
		
		<!-- 因性能消耗大请只在需要的时候使用它。假如设为true那么在每个connection提交的
		  时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable		
		  等方法来提升连接测试的性能。Default: false -->
		<property name="testConnectionOnCheckout">
			<value>false</value>
		</property>
	</bean>
<!--create sessionFactory-->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>	
  <!-- config hibernate Field -->   <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> ${hibernate.dialect}</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.jdbc.fetch_size"> ${hibernate.jdbc.fetch_size}</prop> <prop key="hibernate.jdbc.batch_size"> ${hibernate.jdbc.batch_size}</prop> <prop key="hibernate.current_session_context_class">thread</prop> <prop key="hibernate.bytecode.use_reflection_optimizer">false</prop> <prop key="hibernate.query.substitutions">true</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.max_fetch_depth">3</prop> <prop key="hibernate.hibernate.use_outer_join">true</prop> <prop key="hibernate.jdbc.batch_size">50</prop> <!-- prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop --> <!-- 自动增加新表,重新启动服务时执行该命令,应用时务必注释此句(本机使用) --> <!-- prop key="hibernate.hbm2ddl.auto">update</prop --> </props>   </property>   <!--mapping hibernate model po class-->   <property name="mappingDirectoryLocations"> <list>   <value>/WEB-INF/classes/com/cms/pojo</value><!--改文件夹下面的所有的配置文件 --> </list>   </property>
</bean>

 事务配置方法一: 

<!-- 定义事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<!-- 配置事务管理器 -->
		<property name="transactionManager" ref="transactionManager"></property>
		<!-- 配置事务属性 -->
		<property name="transactionAttributes">
			<props>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>	
	</bean>
	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<list>
				<value>PersonDAOImpl</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>

 事务配置方法二:

<!--config transaction manager-->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
   
<!-- Transactional advice -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">   
        <tx:attributes>
            <tx:method name="save*" />
			<tx:method name="update*" />
			<tx:method name="delete*" />
			<tx:method name="add" />
			<tx:method name="modify*" />
			<tx:method name="remove*"/>
			<tx:method name="merge*" />
			<!-- <tx:method name="list*" /> -->
			<!-- <tx:method name="ajax*" /> -->
            <!-- other methods are set to read only -->   
            <!-- tx:method name="*" read-only="true"/ -->   
        </tx:attributes>   
    </tx:advice>   

  

 

posted @ 2014-11-19 12:00  雨中飞鹭  阅读(197)  评论(0编辑  收藏  举报