spring_spring+JDBC整合開發

spring+JDBC整合開發, 步驟如下:

(1)配置數據源,如:

      <context:property-placeholder location="classpath:jdbc.properties"/>
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	      	<property name="driverClassName" value="${driverClassName}"/>
	      	<property name="url" value="${url}"/>
	      	<property name="username" value="${username}"/>
	      	<property name="password" value="${password}"/>
	      	<!-- 連接池啓動時的初始值 -->
	      	<property name="initialSize" value="${initialSize}"/>
	      	<!-- 連接池的最大值 -->
	      	<property name="maxActive" value="${maxActive}"/>
	      	<!-- 最大空閒值,當經過一個高峯時間後,連接池可以慢慢將已經用不到的鏈接釋放一部分,一直減少到maxIdle 爲止 -->
	      	<property name="maxIdle" value="${maxIdle}"/>
	      	<!-- 最小空閒值,當空閒的連接數少於閾值時,連接池就會預申請一些連接,以免洪峯來時來不及申請 -->
	      	<property name="minIdle" value="${minIdle}"/>
      </bean>
(2)配置事務, 如:

      <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      		<property name="dataSource" ref="dataSource"/>
      </bean>
      <tx:annotation-driven transaction-manager="txManager"/>
其中, tx 命名空間的引入如下:

       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop"      
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
注意: serviceBean加這個註解: @Transactional   使用spring 的事務管理功能




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章