配置类: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