spring事务-纯注解实现

5 篇文章 0 订阅
4 篇文章 0 订阅

配置类:SpringConfiguration.java 取代beans.xml配置文件

/*
 * Copyright (c) 2019, wenwenliuyun@163.com All Rights Reserved. 
 */

package com.liuyun.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author liuyun
 * @since 2019年9月1日下午10:27:37
 */
@Configuration
@ComponentScan("com.liuyun") // 组件扫描包设置
@PropertySource("classpath:db.properties") // 读取properties文件
@Import(Beans.class) // 引入外部类中的bean加入到容器
@EnableTransactionManagement // 开启事务管理
public class SpringConfiguration {
	@Bean(name = "transactionManager")
	public DataSourceTransactionManager transactionManager(DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}
	
	@Bean(name = "jdbcTemplate")
	public JdbcTemplate jdbcTemplate(DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}

	@Bean(name = "dataSource")
	public DriverManagerDataSource dataSource(@Value("${jdbc.driverClass}") String driverClassName,
			@Value("${jdbc.jdbcUrl}") String url, @Value("${jdbc.user}") String username,
			@Value("${jdbc.password}") String password) {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName(driverClassName);
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		return dataSource;
	}
	
	@Bean // 高版本的spring容器中会自动加入该bean(注解读取properties时,需要该bean)
	PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
}

关键代码:开始事务管理,事务管理bean加入容器
在这里插入图片描述
配合业务层:UserServiceImpl.java

/*
 * Copyright (c) 2019, wenwenliuyun@163.com All Rights Reserved. 
 */

package com.liuyun.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.liuyun.dao.IUserDao;
import com.liuyun.domain.User;
import com.liuyun.service.IUserService;

/**
 * @author liuyun
 * @version
 * @since 2019年8月29日下午7:43:54
 */
// 在类和方法上开启事务控制,并设置事务管理类; 
// 其他属性取默认值:propagation = Propagation.REQUIRED, readOnly = false;
// 方法可以根据需要进行单独设置
@Transactional(transactionManager = "transactionManager")
@Service("userService")
public class UserServiceImpl implements IUserService {

	@Resource
	private IUserDao userDao;

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public long count() throws Exception {
		System.out.println("Service的count执行");
		return userDao.count();
	}

	@Override
	public int deleteById(Integer id) throws Exception {
		return userDao.deleteById(id);
	}

	@Override
	public int insert(User record) throws Exception {
		int count = userDao.insert(record);
		int i = 1 / 0;// 验证事务回滚
		return count;
	}

	@Override
	public int update(Integer id, User record) throws Exception {
		if (id == null || record == null) {
			throw new RuntimeException("update input params cannot be null...");
		}
		record.setId(id);
		return userDao.update(record);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<User> findAll() throws Exception {
		return userDao.findAll();
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public User findById(Integer id) throws Exception {
		return userDao.findById(id);
	}
}

关键代码:设置事务管理类,在类和方法上加入事务控制
在这里插入图片描述

完整代码:https://github.com/liuyunHappy/spring-tx-annotation.git

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值