现在的位置: 首页 > 综合 > 正文

spring+ibatis 数据源及事务配置(项目备用)

2013年03月23日 ⁄ 综合 ⁄ 共 2522字 ⁄ 字号 评论关闭

<?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: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/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"> 
   
    <!-- 数据源配置 -->
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"
            value="net.sourceforge.jtds.jdbc.Driver">
        </property>
        <property name="url"
            value="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbName;SelectMethod=cursor">
        </property>
        <property name="username" value="sa"></property>
        <property name="password" value="sa"></property>
    </bean>
   
    <!-- Spring iBatis Template -->
    <bean id="sqlMapClient"
        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="classpath:config/SqlMapConfig.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>
   
    <!-- 事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>
   
    <!-- 声明式事务管理 -->
    <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
        <property name="transactionManager" ref="transactionManager"></property>
        <property name="transactionAttributes">
            <props>
                <prop key="add*">PROPAGATION_REQUIRED</prop>           
                <prop key="edit*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>

   <!-- 注意该DAO 一定要继承SqlMapClientDaoSupport 使用getSqlMapClientTemplate()方法,并且要抛出 throws DataAccessException 异常 spring才能捕获并回滚 -->
    <bean id="UserDAO" class="com.gong.struts.test.UserDAO">
        <property name="sqlMapClient" ref="sqlMapClient"></property>
    </bean>
   
    <bean id="UserManager" parent="baseTransactionProxy">
        <property name="target">
            <bean class="com.gong.struts.test.UserManager">
                <property name="userdao" ref="UserDAO"></property>
            </bean>
        </property>
    </bean>

</beans>

0

抱歉!评论已关闭.