spring访问数据库

来源:互联网 发布:网络与新媒体研究生吧 编辑:程序博客网 时间:2024/05/16 14:26


以下是配置一个完整的配置文件,有时间再详细讲解


<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">    <bean id="dataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean">        <property name="targetName" value="_dataSource" />    </bean>    <bean id="_dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">        <property name="driverClass">            <value>${jdbc.driverClassName}</value>        </property>        <property name="jdbcUrl">            <value>${jdbc.url}?autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8&readOnly=true            </value>        </property>        <property name="user">            <value>${jdbc.username}</value>        </property>        <property name="password">            <value>${jdbc.password}</value>        </property>        <property name="initialPoolSize">            <value>${jdbc.initialPoolSize}</value>        </property>        <property name="minPoolSize">            <value>${jdbc.minPoolSize}</value>        </property>        <property name="maxPoolSize">            <value>${jdbc.maxPoolSize}</value>        </property>        <property name="maxIdleTime">            <value>${jdbc.maxIdleTime}</value>        </property>        <property name="acquireIncrement">            <value>${jdbc.acquireIncrement}</value>        </property>        <property name="acquireRetryAttempts">            <value>${jdbc.acquireRetryAttempts}</value>        </property>        <property name="acquireRetryDelay">            <value>${jdbc.acquireRetryDelay}</value>        </property>        <property name="maxStatements">            <value>${jdbc.maxStatements}</value>        </property>        <property name="maxStatementsPerConnection">            <value>${jdbc.maxStatementsPerConnection}</value>        </property>        <property name="checkoutTimeout">            <value>${jdbc.checkoutTimeout}</value>        </property>        <property name="breakAfterAcquireFailure">            <value>${jdbc.breakAfterAcquireFailure}</value>        </property>    </bean><!--事务管理器,对事务进行管理-->    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean><!--让spring来管理SqlMapClient对象-->    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">        <property name="configLocation">            <value>classpath:sqlmap-config.xml</value>        </property>        <property name="dataSource" ref="dataSource" />    </bean>    <bean id="sqlMapTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">        <property name="sqlMapClient" ref="sqlMapClient" />    </bean>    <bean id="baseDao" class="com.***.***.dao.BaseDao" abstract="true">        <property name="sqlMapTemplate" ref="sqlMapTemplate" />    </bean>    <bean id="userResourceDao" class="com.***.***.dao.UserResourceDao">        <property name="sqlMapTemplate" ref="sqlMapTemplate"></property>    </bean>      <bean id="platformDao" class="com.***.***.dao.PlatformDao" parent="baseDao" />          <tx:advice id="transactionManagerAdivice" transaction-manager="txManager">        <tx:attributes>            <tx:method name="*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.RuntionException" />        </tx:attributes>    </tx:advice><!-- 配置哪些类的方法需要进行事务管理 -->    <aop:config>        <aop:pointcut expression="execution(* com.***.***.service.ApplyAppService.*(..))" id="applyAppServicePc" />        <aop:advisor advice-ref="transactionManagerAdivice" pointcut-ref="applyAppServicePc" />    </aop:config></beans>    


0 0