農農  
1.使用事务工厂实现
1.1创建Dao层接口
public interface TransationDao {
    //加钱方法
    public void addMoney(Double dou);


    //减钱方法
    public void subMoney(Double dou);
}

 

 

1.2创建Dao层接口实现类
@Repository
public class TransationDaoImpl implements TransationDao {

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public void addMoney(Double dou) {

        String sql="update accounts set balance=balance+? where accountid=1";

        /*this.getJdbcTemplate().update(sql, dou);*/

        jdbcTemplate.update(sql,dou);
    }

    @Override
    public void subMoney(Double dou) {
        String sql="update accounts set balance=balance-? where accountid=6";
        /*this.getJdbcTemplate().update(sql,dou);*/
        jdbcTemplate.update(sql,dou);
    }
}

 

 

 

1.3创建Service接口
public interface TransationService{

    //转账方法
    public void transferMoney(Double dou);
}

 

 

1.4创建Service接口实现类

 

@Service("transationService")
public class TransationServiceImpl implements TransationService {
    @Resource
    private TransationDao transationDao;
    
    @Override
    public void transferMoney(Double dou) {
        transationDao.subMoney(dou);
        Integer result=5/0;
        transationDao.addMoney(dou);
    }

    public TransationDao getTransationDao() {
        return transationDao;
    }

    public void setTransationDao(TransationDao transationDao) {
        this.transationDao = transationDao;
    }
}

 

 

 

1.5编写applicationContext.xml文件
<!--扫描注解:包扫描器-->
<context:component-scan base-package="com.spring"/>
<!--识别到配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<!--spring内置的数据源:提供连接的,不负责管理,使用连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!--构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!--事务,事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>


<!--配置Spring事务的代理工厂-->
<bean id="transactionFactory" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <!--事务管理器-->
    <property name="transactionManager" ref="transactionManager"></property>
    <!--目标对象-->
    <property name="target" ref="transationService"></property>
    <!--设置方法-->
    <property name="transactionAttributes">
        <props>
            <!--方法对应的隔离级别和传播行为-->
            <prop key="transferMoney">ISOLATION_READ_COMMITTED</prop>
        </props>
    </property>
</bean>

 

 

1.6编写测试类
@Test
public void transationTest(){
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext01.xml");
    TransationService transationService = (TransationService)context.getBean("transactionFactory");
    transationService.transferMoney(Double.valueOf(1000));
    System.out.println("转账成功");
}

 

 

 

1. 使用AOP方式实现
2.1创建Dao层接口
public interface TransationDao {
    //加钱方法
    public void addMoney(Double dou);


    //减钱方法
    public void subMoney(Double dou);
}

 

 

 

2.2创建Dao层接口实现类
@Repository
public class TransationDaoImpl implements TransationDao {

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public void addMoney(Double dou) {

        String sql="update accounts set balance=balance+? where accountid=1";

        /*this.getJdbcTemplate().update(sql, dou);*/

        jdbcTemplate.update(sql,dou);
    }

    @Override
    public void subMoney(Double dou) {
        String sql="update accounts set balance=balance-? where accountid=6";
        /*this.getJdbcTemplate().update(sql,dou);*/
        jdbcTemplate.update(sql,dou);
    }
}

 

 

 

2.3创建Service接口
public interface TransationService{

    //转账方法
    public void transferMoney(Double dou);
}

 

 

2.4创建Service接口实现类

 

@Service("transationService")
public class TransationServiceImpl implements TransationService {
    @Resource
    private TransationDao transationDao;
    
    @Override
    public void transferMoney(Double dou) {
        transationDao.subMoney(dou);
        Integer result=5/0;
        transationDao.addMoney(dou);
    }

    public TransationDao getTransationDao() {
        return transationDao;
    }

    public void setTransationDao(TransationDao transationDao) {
        this.transationDao = transationDao;
    }
}

 

 

 

2.5编写applicationContext.xml文件

<!--扫描注解:包扫描器-->
<context:component-scan base-package="com.spring"/>
<!--识别到配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<!--spring内置的数据源:提供连接的,不负责管理,使用连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!--构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>


 </bean>-->

<!--事务,事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!--AOP方式-->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transferMoney" propagation="REQUIRED" isolation="READ_COMMITTED"></tx:method>
</tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="pointcut" expression="execution(* *..service.impl.*.*(..))"/>
    <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>

 

 

2.6编写测试类

@Test
public void transationTest(){
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext01.xml");
    TransationService transationService = (TransationService)context.getBean("transationService");
    transationService.transferMoney(Double.valueOf(1000));
    System.out.println("转账成功");
}

 

 

2. 使用注解方式实现
3.1创建Dao层接口
public interface TransationDao {
    //加钱方法
    public void addMoney(Double dou);


    //减钱方法
    public void subMoney(Double dou);
}

 

 

 

3.2创建Dao层接口实现类
@Repository
public class TransationDaoImpl implements TransationDao {

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public void addMoney(Double dou) {

        String sql="update accounts set balance=balance+? where accountid=1";

        /*this.getJdbcTemplate().update(sql, dou);*/

        jdbcTemplate.update(sql,dou);
    }

    @Override
    public void subMoney(Double dou) {
        String sql="update accounts set balance=balance-? where accountid=6";
        /*this.getJdbcTemplate().update(sql,dou);*/
        jdbcTemplate.update(sql,dou);
    }
}

 

 

 

3.3创建Service接口
public interface TransationService{

    //转账方法
    public void transferMoney(Double dou);
}

 

 

 

3.4创建Service接口实现类

 

@Service("transationService")
public class TransationServiceImpl implements TransationService {
    @Resource
    private TransationDao transationDao;
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public void transferMoney(Double dou) {
        transationDao.subMoney(dou);
        Integer result=5/0;
        transationDao.addMoney(dou);
    }

    public TransationDao getTransationDao() {
        return transationDao;
    }

    public void setTransationDao(TransationDao transationDao) {
        this.transationDao = transationDao;
    }
}

 

 

 

 

3.5编写applicationContext.xml文件

<!--扫描注解:包扫描器-->
    <context:component-scan base-package="com.spring"/>
    <!--识别到配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源-->
    <!--spring内置的数据源:提供连接的,不负责管理,使用连接池-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--构建jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--事务,事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

<!--Spring支持注解式配置-->
<tx:annotation-driven/>

 

 

3.6编写测试类

@Test
public void transationTest(){
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext01.xml");
    TransationService transationService = (TransationService)context.getBean("transationService");
    transationService.transferMoney(Double.valueOf(1000));
    System.out.println("转账成功");
}

 

posted on 2019-11-04 14:12  Baekhyunne  阅读(616)  评论(0编辑  收藏  举报