資料庫配置相關檔案
阿新 • • 發佈:2018-11-02
applicationConfig.properties 資料庫配置
#application all config
#jdbc c3p0
jdbc.driver =com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url = jdbc:sqlserver://192.168.1.133:1433;DatabaseName=MyWeb
jdbc.username = mingqi
jdbc.password = dc-8899
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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <!--引入配置屬性檔案 --> <context:property-placeholder location="classpath:/dbconfig/applicationConfig.properties" /> <!-- c3p0資料來源配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 請求超時時間 --> <property name="checkoutTimeout" value="30000" /> <!-- 每60秒檢查所有連線池中的空閒連線。預設值: 0,不檢查 --> <property name="idleConnectionTestPeriod" value="30" /> <!-- 連線資料庫連線池最大空閒時間 --> <property name="maxIdleTime" value="30" /> <!-- 連線池初始化連線數 --> <property name="initialPoolSize" value="5" /> <property name="minPoolSize" value="5" /> <property name="maxPoolSize" value="20" /> <!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。預設值: 3 --> <property name="acquireIncrement" value="5" /> </bean> <!-- spring jdbc --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <!-- 配置事務管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 宣告式事務,事務攔截器 --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager" /> <!-- 配置事務屬性 --> <property name="transactionAttributes"> <!--下面定義事務傳播屬性 --> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="del*">PROPAGATION_REQUIRED</prop> <prop key="update">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- Bean後處理器BeanNameAutoProxyCreator,根據List配置建立事務代理 --> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!-- 下面是所有需要自動建立事務代理的bean --> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <!-- 下面定義BeanNameAutoProxyCreator所需的事務攔截器 --> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> </beans>