SpringBootマルチデータソースのJPAの構成

3222 ワード

package com.labelwall.flash.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.orm.jpa.DefaultJpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * @EnableJpaRepositories   Jpa   
 * @Author sunlin
 * @Date 2019/4/18 17:17
 * @Describe
 */
@Slf4j
@Configuration
@EnableJpaRepositories(
        basePackages = "com.labelwall.flash.mysql.dao", entityManagerFactoryRef = "mysqlFactoryBean", transactionManagerRef = "mysqlTransaciton"
)
public class MysqlDatasourceConfig {

    @Qualifier("mysqlDataSource")
    @Autowired
    private DataSource dataSource;


    /**
     *        !
     */
    @Bean(name = "mysqlDataSource")
    @ConfigurationProperties(prefix = "mysql.datasource")
    public DataSource manDataSource() {
        return new DruidDataSource();
    }

    /**
     *         !
     */
    @Bean(name = "mysqlTransaciton")
    public PlatformTransactionManager manTransactionManager() {
        //4
        EntityManagerFactory factory = manEntityManagerFactory().getObject();
        return new JpaTransactionManager(factory);
    }

    @Bean(name = "mysqlFactoryBean")
    public LocalContainerEntityManagerFactoryBean manEntityManagerFactory() {
        //5
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setDataSource(dataSource);
        //         
        factory.setPackagesToScan("com.labelwall.flash.mysql.pojo");
        //      
        factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.hbm2ddl.auto", "none");
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        jpaProperties.put("hibernate.show-sql", true);
        factory.setJpaProperties(jpaProperties);
        return factory;
    }

}