Springboot 多数据源配置

以下介绍Springboot的多数据源及其事务的配置

数据源配置

x3:
  jdbc:
    primary:
      dbtype: mysql
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/mos_fashion?useUnicode=true&characterEncoding=UTF-8&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false
      username: admin
      password: 123
    
    second:
      dbtype: mysql
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/mos_mdm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false
      username: admin
      password: 123

DataSource相关配置

/**
 * @Description 多数据源配置
 * @Date 2018年7月26日 下午4:16:27
 */
@Configuration
public class DataSourceConfig {

	/**
	* @Description 获取X3数据源
	* @return 
	* @returnType DataSource
	*/
	@Primary
	@Bean(name = "Primary")
	@Qualifier("Primary")
	@ConfigurationProperties(prefix = "x3.jdbc.primary")
	public DataSource getPrimaryDataSource() {
		return DataSourceBuilder.create().build();
	}
	
	@Bean(name = "Second")
	@Qualifier("Second")
	@ConfigurationProperties(prefix = "x3.jdbc.second")
	public DataSource getSecondDataSource() {
		return DataSourceBuilder.create().build();
	}

	/**
	* @Description 获取数据源的事务管理器
	* @return 
	* @returnType DataSourceTransactionManager
	*/
	@Primary
	@Bean(name = "fashTransactionManager")
	public DataSourceTransactionManager getFashTransactionManager() {
		return new DataSourceTransactionManager(getPrimaryDataSource());
	}
	
	@Bean(name = "mdmTransactionManager")
	public DataSourceTransactionManager getMdmTransactionManager() {
		return new DataSourceTransactionManager(getPrimaryDataSource());
	}

	/**
	* @Description 获取数据源的jdbc连接
	* @param dataSource
	* @return 
	* @returnType JdbcTemplate
	*/
	@Bean(name = "fashJdbcTemplate")
	public JdbcTemplate getFashJdbcTemplate(@Qualifier("Primary") DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}
	
	@Bean(name = "mdmJdbcTemplate")
	public JdbcTemplate getMdmJdbcTemplate(@Qualifier("Second") DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}

}

JDBC工具类:


@Component
public class JdbcUtil {

	private static JdbcUtil jdbcUtil;

	@Autowired
	@Qualifier("fashJdbcTemplate")
	private JdbcTemplate fashJdbcTemplate;

	@Autowired
	@Qualifier("mdmJdbcTemplate")
	protected JdbcTemplate mdmJdbcTemplate;

	
	@PostConstruct
	public void init() {
		jdbcUtil = this;
		jdbcUtil.fashJdbcTemplate = this.fashJdbcTemplate;
		jdbcUtil.mdmJdbcTemplate = this.mdmJdbcTemplate;
	}

	public static JdbcTemplate getFashJdbcTemplate() {
		return jdbcUtil.fashJdbcTemplate;
	}

	public static JdbcTemplate getMdmJdbcTemplate() {
		return jdbcUtil.mdmJdbcTemplate;
	}
	
}

事务配置


@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RuntimeException.class, BusinessException.class,
		Exception.class}, timeout = 60, value = "fashTransactionManager")
public @interface TransactionalFash {
	
}


@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RuntimeException.class, BusinessException.class,
		Exception.class}, timeout = 60, value = "mdmTransactionManager")
public @interface TransactionalMdm {
}

数据源使用:
Springboot 多数据源配置_第1张图片
Springboot 多数据源配置_第2张图片

你可能感兴趣的:(Springboot 多数据源配置)