SpringBoot2 多数据源、多事务管理配置

SpringBoot2 多数据源、多事务管理配置

1. 首先启动事务管理:@EnableTransactionManagement

2. 添加基础配置:

server.port=8080
#master
spring.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/
spring.datasource.master.username=root
spring.datasource.master.password=root
#slave
spring.datasource.slave.jdbc-url=jdbc:mysql://localhost:3306/
spring.datasource.slave.username=root
spring.datasource.slave.password=root
#jpa
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
# implicit naming strategy fully qualified name
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

3. 添加配置:

/**
 * Created by xiehui1956(@)gmail.com on 2020/4/26
 */
@Configuration
public class DataSourceConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Primary
    @Bean(name = "masterDataSource")
    @Qualifier(value = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource getDefaultDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * clickHouse操作模板
     *
     * @return
     */
    @Bean(name = "slaveDataSource")
    @Qualifier(value = "slaveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource getServiceClickHouseDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "masterJdbcTemplate")
    public JdbcTemplate masterJdbcTemplate(@Qualifier(value = "masterDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "slaveJdbcTemplate")
    public JdbcTemplate slaveJdbcTemplate(@Qualifier(value = "slaveDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}

4. 添加事务配置:

/**
 * Created by xiehui1956(@)gmail.com on 2020/4/26
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryMaster"
        , transactionManagerRef = "transactionManagerMaster"
        , basePackages = {"com.dao"})
public class DataSourceMasterManager {

    @Autowired
    @Qualifier("masterDataSource")
    private DataSource masterDataSource;

    @Primary
    @Bean(name = "entityManagerFactoryMaster")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(masterDataSource)
                .packages("com.entity")
                .persistenceUnit("masterDataPersistenceUnit")
                .build();
    }

    @Bean("transactionManagerMaster")
    public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryBean(builder).getObject());
    }
}

 

/**
 * Created by xiehui1956(@)gmail.com on 2020/4/26
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySlave"
        , transactionManagerRef = "transactionManagerSlave"
        , basePackages = {"com.dto"})
public class DataSourceSlaveManager {

    @Autowired
    @Qualifier("slaveDataSource")
    private DataSource slaveDataSource;

    @Bean(name = "entityManagerFactorySlave")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(slaveDataSource)
                .packages("com.deo")
                .persistenceUnit("slaveDataPersistenceUnit")
                .build();
    }

    @Bean("transactionManagerSlave")
    public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryBean(builder).getObject());
    }
}

5. 使用

@Resource
@Qualifier(value = "masterJdbcTemplate")
private JdbcTemplate jdbcTemplate;

 

你可能感兴趣的:(Java,服务器,数据库)