spring随笔(事务配置)

spring中的jdbc

spring中的jdbc与传统的jdbc有什么区别?Spring的jdbc:节省代码,不管连接(Connection),不管事务、不管异常、不管关闭(con.close() ps.close )由JdbcTemplate实现crud

spring代码式管理事务

流程:
1.在xml中配置数据源(连接池)dataSource
2.配置事务管理器,注入数据源 DataSourceTransactionManager
3.配置事务管理模板transactionTemplate,注入事务管理器
4.配置JDBC模板JdbcTemplate,注入dao
5.配置service,注入dao和transactionTemplate
(如果我们使xxDao继承JdbcDaoSupport 他会在底层为我们直接new一个JdbcTemplate 无需注入,直接向注入数据源)

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 1.创建链接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl"
            value="jdbc:mysql://localhost:3306/springdemo?useUnicode=true&amp;characterEncoding=UTF8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="admin"></property>

    </bean>
    <!-- 2.配置事务管理器 事务需要从Connection 中获得,链接从连接池中获取 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 3.创建事务管理模板 -->
    <bean id="transactionTemplate"
        class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="txManager"></property>
    </bean>
    <bean id="accountDao" class="com.spring.transaction.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="AccountService" class="com.spring.transaction.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
        <property name="transactionTemplate" ref="transactionTemplate"></property>
    </bean>


</beans>
package com.spring.transaction;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    @Override
    public void AccountIn(String inname, int money) {
        String sql = "update account set money=money+? where username=? ";
        this.getJdbcTemplate().update(sql, money, inname);
    }

    @Override
    public void AccountOut(String outname, int money) {
        String sql = "update account set money=money-? where username=? ";
        this.getJdbcTemplate().update(sql, money, outname);
    }

}
package com.spring.transaction;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    private TransactionTemplate transactionTemplate;

    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }

    // 注入
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
   //没有返回值类型 使用TransactionCallbackWithoutResult()  如果有TransactionCallback
    @Override
    public void AccountIn(String inname, String outname, int money) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                accountDao.AccountIn(inname, money);

                accountDao.AccountOut(outname, money);

            }
        });

    }

}

声明式事务管理

半自动

半自动事务流程:
1.在xml中配置数据源(连接池)dataSource
2.配置事务管理器,注入数据源 DataSourceTransactionManager
3.配置JDBC模板JdbcTemplate,注入dao
4.配置service,注入dao
5.配置TransactionProxyFactoryBean生成代理对象

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 1创建链接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl"
            value="jdbc:mysql://localhost:3306/springdemo?useUnicode=true&amp;characterEncoding=UTF8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="admin"></property>
    </bean>
    <!-- 2配置事务管理器 事务需要从Connection 中获得,链接从连接池中获取 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="accountService" class="com.spring.semiauto_transaction.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <bean id="accountDao" class="com.spring.semiauto_transaction.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 工厂bean代理 -->
    <!-- 
    proxyInterfaces         代理对象的接口
    target                  目标类
    transactionManager      事务管理器
    transactionAttributes   事务属性 
       prop.key             确定哪些方法使用当前事务配置  
       prop.text            用于配置事务详情    
                                格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
                    传播行为        隔离级别    是否只读        异常回滚        异常提交            
     -->
    <bean id="proxyService"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="proxyInterfaces"
            value="com.spring.semiauto_transaction.AccountService"></property>
        <property name="target" ref="accountService"></property>
        <property name="transactionManager" ref="txManager"></property>
        <property name="transactionAttributes">
            <props>
                <prop key="AccountIn">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
            </props>
        </property>
    </bean>






</beans>
package com.spring.semiauto_transaction;

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;

    // 注入
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }


    @Override
    public void AccountIn(String inname, String outname, int money) {
        accountDao.AccountIn(inname, money);
        //int x=1/0;
        accountDao.AccountOut(outname, money);

    }

}

基于aop自动管理

自动事务流程:
1.在xml中配置数据源(连接池)dataSource
2.配置事务管理器,注入数据源 DataSourceTransactionManager
3.配置JDBC模板JdbcTemplate,注入dao
4.配置service,注入dao
5.配置事务详情 TransactionDefintion 标签
6.aop编程

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 创建链接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl"
            value="jdbc:mysql://localhost:3306/springdemo?useUnicode=true&amp;characterEncoding=UTF8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="admin"></property>
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 事务详情 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="AccountIn" isolation="DEFAULT" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>
    <!-- aop编程 -->
    <aop:config>
    <aop:pointcut expression="execution(* com.spring.auto_transaction..*.*(..))" id="myPointcut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
    </aop:config>

    <bean id="accountService" class="com.spring.auto_transaction.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>

    </bean>
    <bean id="accountDao" class="com.spring.auto_transaction.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

注解配置

package com.spring.transaction_anno;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

@Service("AccountService")
@Transactional(isolation=Isolation.DEFAULT,propagation= Propagation.REQUIRED)
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    private TransactionTemplate transactionTemplate;

    @Autowired
    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }
    // 注入
    @Autowired
    @Qualifier("accountDao")
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    @Override
    public void AccountIn(String inname, String outname, int money) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                accountDao.AccountIn(inname, money);
                // int x=1/0;
                accountDao.AccountOut(outname, money);

            }
        });

    }

}
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 创建链接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl"
            value="jdbc:mysql://localhost:3306/springdemo?useUnicode=true&amp;characterEncoding=UTF8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="admin"></property>
    </bean>
    <!-- 配置事务管理器 事务需要从Connection 中获得,链接从连接池中获取 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="accountDao" class="com.spring.transaction_anno.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 组件扫描 -->

    <context:component-scan base-package="com.spring.transaction_anno"></context:component-scan>
    <!-- 将管理器交予spring 
        * transaction-manager 配置事务管理器
        * proxy-target-class
        true : 底层强制使用cglib 代理 -->

    <tx:annotation-driven transaction-manager="txManager" />
</beans>
发布了57 篇原创文章 · 获赞 29 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章