zoukankan      html  css  js  c++  java
  • Spring事务管理器

    1、创建实体和接口

    public class Bank {
        private Integer id;
        private String name;
        private String manay;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getManay() {
            return manay;
        }
        public void setManay(String manay) {
            this.manay = manay;
        }
    }

    2、接口中新建方法

    public interface IBankDao {
        //转出
        public int RolloutMoney(String card_code,double money);
    
        //转入
        public int RollinMoney(String card_code,double money);
    }

    3、IBankDaoImpl类

    @Repository
    public class IBankDaoImpl implements IBankDao {
        //植入JDBC模板
        @Resource
        private JdbcTemplate jdbcTemplate;
        @Override
        public int RolloutMoney(String card_code, double money) {
            int outconut = jdbcTemplate.update("update bank set card_balance=card_balance-? where card_code=?",money,card_code);
            return outconut;
        }
    
        @Override
        public int RollinMoney(String card_code, double money) {
            int incount = jdbcTemplate.update("update bank set card_balance=card_balance+? where card_code=?",money,card_code);
            return incount;
        }
    }

    4、IBankService类

    public interface IBankService {
        //转账的方法
        public int transfer(String out_card_code,String in_card_code,double money) throws Exception;
    }

    5、IBankServiceImpl类

    @Service("iBankService")
    public class IBankServiceImpl implements IBankService {
        //植入Dao层对象
        @Resource
        private IBankDao iBankDao;
    
        @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
        @Override
        public int transfer(String out_card_code, String in_card_code, double money) throws Exception {
            //转出
            int out = iBankDao.RolloutMoney(out_card_code,money);
    
            //模拟异常
            //Spring事务管理会不会对所有异常都会回滚,只能对运行时异常eollback
            if (true){
                throw new RuntimeException("=============异常=============");
            }
            //转入
            int in=iBankDao.RollinMoney(in_card_code,money);
            return out+in;
        }
    }

    6、applicationContext.xml

    <!--加载配置文件-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        <!--Datasource供模板调用
            DriverManagerDataSource:spring提供管理数据源的
            c3p0数据源             dbcp数据源-->
        <bean id="dataSourse" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
        <!--扫描注解:包扫描器-->
        <context:component-scan base-package="com.Spring"/>
    
        <!--植入JdbcTemplate-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSourse"/>
        </bean>
    
        <!--事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!--管理数据源-->
            <property name="dataSource" ref="dataSourse"/>
        </bean>
    
        <!--AOP事务-->
        <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="trans*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>
    
        <aop:config>
            <aop:pointcut id="pointCut" expression="execution(* *..service.impl.*.*(..))"/>
            <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointCut"/>
        </aop:config>
    
        <!--Spring支持注解式事务配置-->
        <tx:annotation-driven/>
    
        <!--事务代理工厂Bean-->
        <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <!-- 事务管理器-->
            <property name="transactionManager" ref="transactionManager"></property>
            <!--目标对象-->
            <property name="target" ref="iBankService"></property>
            <!--设置方法-->
            <property name="transactionAttributes">
                <props>
                    <!--方法对应的隔离级别和传播行为-->
                    <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
                </props>
            </property>
        </bean>

    7、TransferTest类

    public class TransferTest {
        public static void main(String[] args) {
            ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
            IBankService iBankService = (IBankService)ctx.getBean("transactionProxy");
    
            try {
                iBankService.transfer("45354754","63285978",100);
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("转账成功");
        }
    }
  • 相关阅读:
    Linux基础知识整理
    小白学习之路,基础四(函数的进阶)
    关于高通量数据格式
    数据库管理系统
    Linux 基本操作
    生信研究内容
    redis6 多线程特性
    Centos8配置NFS4
    关于Mybatis将查询结果中添加常量列并返回
    关于swagger文档的使用方法
  • 原文地址:https://www.cnblogs.com/dabrk/p/11791676.html
Copyright © 2011-2022 走看看