Spring纯注解事务管理

Spring纯注解事务管理

纯注解的事务管理只需要用一个配置类就好了,配置类需要几个注解
@Configuration
@EnableTransactionManagement这个是开启事务管理的,这个注解类似与tx:advice
@@ComponentScan(basePackages = “haoren”)扫描注解
在xml文件的事务管理中我们是需要创建三个对象的DataSource,JdbcTemplate还有DataSourceTransactionManager
我们可以使用@Bean在注解类里面表明这是需要创建的对象,类似于bean标签。
最后我们还需要在需要事务管理的类或者方法上面加上注解@Transactional表明这个需要事务管理。
两个事务管理标签就像是aop:config标签里面的
aop:advisor相似,把原本的代码增强加上事务管理

代码(配置类)

package haoren.xmlProxy;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "haoren")
public class XMLproxy1 {
//    获取连接池
    @Bean
    public DataSource getDateSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setUrl("jdbc:mysql:///t_test");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return dataSource;

    }
//    获取JdbcTemplate
@Bean(value = "jdbcTemplate")
public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    return jdbcTemplate;
}

    //    创建事务管理器
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
        return dataSourceTransactionManager;
    }

}

代码(service)

package haoren.service;

import haoren.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

//@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class UserService {
    @Autowired
    @Qualifier(value = "userDaoImpl")
    private UserDao userDao;

    public void countMoney(){
        userDao.reduceMoney();
        int i=10/0;
        userDao.addMoney();
    }
}

代码(userdao)

package haoren.dao;

public interface UserDao {

    void reduceMoney();

    void addMoney();
}

代码(userdaoimpl)

package haoren.dao;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    @Qualifier(value = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;



    @Override
    public void reduceMoney() {
        String sql="update t_account set money=money-100 where username = ?";
        jdbcTemplate.update(sql,"lucy");
    }

    @Override
    public void addMoney() {
        String sql="update t_account set money=money+100 where username = ?";
        jdbcTemplate.update(sql,"mom");
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值