spring访问数据库

本文详细介绍了一个使用Spring框架的完整配置文件实例。配置包括数据源设置、事务管理、IBATIS对象管理等内容,并通过AOP实现了对指定服务方法的事务控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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


<?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>
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值