Spring --16.Spring基于注解的声明事务控制

版权声明:转载请注明原始链接 https://blog.csdn.net/sswqzx/article/details/84111650

1、创建子工程、引入依赖 (同上xml方式)

2、编写相关类、(同上xml方式)

3、开启Spring对注解的支持、配置事务管理、开启Spring对注解的支持

applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!--开启事务注解的支持 transaction-manager;写事务管理器的id-->
        <tx:annotation-driven transaction-manager="transactionManager"/>

        <!--开启Spring注解扫描-->
        <context:component-scan base-package="com.day04_tx_anno"></context:component-scan>
        <!--导入jdbc-->
        <context:property-placeholder location="classpath:jdbc.properties"/>

            <!--采用注解一定要用JdbcTemplate.因为在dao中要注入JdbcTemplate-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driverClass}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
         </bean>


        <!--配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!--注入数据源-->
            <property name="dataSource" ref="dataSource"></property>
        </bean>

</beans>

4、在相关类上加注解

AccountDaoImpl.java

package com.day04_tx_anno.dao.Impl;

import com.day04_tx.dao.AccountDao;
import com.day04_tx.domain.Account;
import com.day04_tx.rowmapper.AccountRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 9:38 2018/11/15
 */
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据id查询用户对象
     * @param id
     * @return
     */
    @Override
    public Account findById(Long id) {
        Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new AccountRowMapper(), id);
        return account;
    }

    /**
     * 更新用户对象
     * @param account
     */
    @Override
    public void update(Account account) {
        jdbcTemplate.update("update account set name = ?, money = ? where id =? ",account.getName(),account.getMoney(),account.getId());
    }
}

AccountServiceImpl.java

package com.day04_tx_anno.service.Imlp;

import com.day04_tx.dao.AccountDao;
import com.day04_tx.domain.Account;
import com.day04_tx.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 9:53 2018/11/15
 */
@Service("accountService")
@Transactional//该类中所有的方法都加可读写的事务
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public void transfer(Long fromId, Long toId, Double money) {
        //查询转出账户
        Account fromAccount = accountDao.findById(fromId);
        //查询转入帐户
        Account toAccount = accountDao.findById(toId);
        //转出帐户捡钱
        fromAccount.setMoney(fromAccount.getMoney()-money);
        //转入帐户加钱
        toAccount.setMoney(toAccount.getMoney()+money);
        //更新转出账户
        accountDao.update(fromAccount);
         //int i = 1/0;
        //更新转入账户
        accountDao.update(toAccount);
    }

    /**
     * 查询功能、现在做了修改操作、但不允许的。
     * @param id 用户id
     * @return
     */
    @Override
    public Account findById(Long id) {
        Account account = accountDao.findById(id);
        account.setMoney(100000d);
        accountDao.update(account);
        return account;
    }


}

5、在业务层中find开头的方法加只读事务@Transactional(readOnly=true)

AccountServiceImpl.java

package com.day04_tx_anno.service.Imlp;

import com.day04_tx.dao.AccountDao;
import com.day04_tx.domain.Account;
import com.day04_tx.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 9:53 2018/11/15
 */
@Service("accountService")
@Transactional//该类中所有的方法都加可读写的事务
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public void transfer(Long fromId, Long toId, Double money) {
        //查询转出账户
        Account fromAccount = accountDao.findById(fromId);
        //查询转入帐户
        Account toAccount = accountDao.findById(toId);
        //转出帐户捡钱
        fromAccount.setMoney(fromAccount.getMoney()-money);
        //转入帐户加钱
        toAccount.setMoney(toAccount.getMoney()+money);
        //更新转出账户
        accountDao.update(fromAccount);
         //int i = 1/0;
        //更新转入账户
        accountDao.update(toAccount);
    }

    /**
     * 查询功能、现在做了修改操作、但不允许的。
     * @param id 用户id
     * @return
     */
    @Override
    @Transactional(readOnly = true)//只读事务
    public Account findById(Long id) {
        Account account = accountDao.findById(id);
        account.setMoney(100000d);
        accountDao.update(account);
        return account;
    }


}

提示:@Transactional注解也可以加在方法上,如果类上和方法上都有@Transactional,则以方法上的为准。

测试注解事务是否成功

运行单元测试类TestTx中的test1方法,控制台报“被零除”异常,account表中的数据也没有发生变化,表示注解事务配置成功啦!

运行Test2方法、发现会出现

注:实际开发中。事务只用xml配置、注解牵涉到第三方类就不能用了。

猜你喜欢

转载自blog.csdn.net/sswqzx/article/details/84111650
今日推荐