SpringBoot多数据源之JPA的配置

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;
    }

}

 

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