在Springboot出现之前配置数据源以及相关的事物,缓存等内容一直是个繁琐的工作,但是Springboot出现后这些基本都可以靠默认配置搞定,就变得很轻松了。这就是现在推崇模板>配置的原因,不过话说回来,如果你想配和模板不同,该繁琐的地方仍然一样繁琐,比如今天要讲的,在Springboot项目中配多数据源。接下来逐渐讲一下。

为项目添加Druid依赖

直接去Maven仓库搜最新的Druid和log4j,阿里的Druid强依赖log4j,却没加到Maven依赖中,实际上,不装log4j的话会报运行错误。同理Spring-boot-web,不过这个基本所有项目都有不特意强调了。

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

生成不同数据源需要的Bean

加入依赖后,我们需要以下几个步骤:

  1. 生成对应每个数据源的DruidDataSource Bean(由于对于多个数据库,同类名Bean有多个,故给每个Bean显性赋名,显性注入防错误)
  2. 将不同数据源注入到JPA,需要配置EntityManager,JpaProperties,PlatformTransactionManager

代码如下,以下代码省略getter,setter,一些具体问题参看代码中注释:

DruidDBConfig.java

@Configuration
@ConfigurationProperties("druid")
public class DruidDBConfig {
    //此处替换成你自己的logger方案
    private final Logger logger = LogSingleton.getLogSingleton().getLogger();

    //这些变量在yml中配置

    private String dbUrl1;
    private String username1;
    private String password1;
    private String driverClassName1;
    private String validationQuery1;

    private String dbUrl2;
    private String username2;
    private String password2;
    private String driverClassName2;
    private String validationQuery2;

    @Value("5")
    private int initialSize;
    @Value("5")
    private int minIdle;
    @Value("30")
    private int maxActive;
    /**
     * 单位是毫秒
     */
    @Value("60000")
    private int maxWait;
    /**
     * 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
     */
    @Value("60000")
    private int timeBetweenEvictionRunsMillis;
    /**
     * 配置一个连接在池中最小生存的时间,单位是毫秒
     */
    @Value("300000")
    private int minEvictableIdleTimeMillis;
    @Value("true")
    private boolean testWhileIdle;
    @Value("true")
    private boolean testOnBorrow;
    @Value("false")
    private boolean testOnReturn;

    /**
     * 打开PSCache,并且指定每个连接上PSCache的大小
     */
    @Value("true")
    private boolean poolPreparedStatements;
    @Value("50")
    private int maxPoolPreparedStatementPerConnectionSize;
    /**
     * 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
     */
    @Value("stat,wall,log4j")
    private String filters;
    /**
     * 通过connectProperties属性来打开mergeSql功能;慢SQL记录
     */
    @Value("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500")
    private String connectionProperties;

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary
    public DataSource primaryDataSource() {
        return getDruidDataSource(username1, password1, dbUrl1, driverClassName1, validationQuery1);
    }

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    public DataSource secondaryDataSource() {
        return getDruidDataSource(username2, password2, dbUrl2, driverClassName2, validationQuery2);
    }

    /*
    /根据配置文件生成DruidDataSource
     */
    private DruidDataSource getDruidDataSource(String username, String password, String url, String driverClassName, String validationQuery) {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            logger.severe("druid configuration initialization filter : " + e);
        }
        datasource.setConnectionProperties(connectionProperties);

        return datasource;
    }
}
@Configuration
@ConfigurationProperties("druid")
public class DruidDBConfig {
    //此处替换成你自己的logger方案
    private final Logger logger = LogSingleton.getLogSingleton().getLogger();

    //这些变量在yml中配置

    private String dbUrl1;
    private String username1;
    private String password1;
    private String driverClassName1;
    private String validationQuery1;

    private String dbUrl2;
    private String username2;
    private String password2;
    private String driverClassName2;
    private String validationQuery2;

    @Value("5")
    private int initialSize;
    @Value("5")
    private int minIdle;
    @Value("30")
    private int maxActive;
    /**
     * 单位是毫秒
     */
    @Value("60000")
    private int maxWait;
    /**
     * 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
     */
    @Value("60000")
    private int timeBetweenEvictionRunsMillis;
    /**
     * 配置一个连接在池中最小生存的时间,单位是毫秒
     */
    @Value("300000")
    private int minEvictableIdleTimeMillis;
    @Value("true")
    private boolean testWhileIdle;
    @Value("true")
    private boolean testOnBorrow;
    @Value("false")
    private boolean testOnReturn;

    /**
     * 打开PSCache,并且指定每个连接上PSCache的大小
     */
    @Value("true")
    private boolean poolPreparedStatements;
    @Value("50")
    private int maxPoolPreparedStatementPerConnectionSize;
    /**
     * 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
     */
    @Value("stat,wall,log4j")
    private String filters;
    /**
     * 通过connectProperties属性来打开mergeSql功能;慢SQL记录
     */
    @Value("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500")
    private String connectionProperties;

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary
    public DataSource primaryDataSource() {
        return getDruidDataSource(username1, password1, dbUrl1, driverClassName1, validationQuery1);
    }

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    public DataSource secondaryDataSource() {
        return getDruidDataSource(username2, password2, dbUrl2, driverClassName2, validationQuery2);
    }

    /*
    /根据配置文件生成DruidDataSource
     */
    private DruidDataSource getDruidDataSource(String username, String password, String url, String driverClassName, String validationQuery) {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        //configuration
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            logger.severe("druid configuration initialization filter : " + e);
        }
        datasource.setConnectionProperties(connectionProperties);

        return datasource;
    }
}

PrimaryConfig.java

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryPrimary",
        transactionManagerRef = "transactionManagerPrimary",
        basePackages = {"com.XX.XX请替换为你的数据源1Repository目录"})
public class PrimaryConfig {

    @Resource
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Resource
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
        //在yml中配置Hibernate,因此此处仅需一个空的Hibernate设置。
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    /**
     * 设置实体类所在位置
     */
    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(primaryDataSource)
                .packages("com.XX.XX请替换为你的数据源1Entity目录")
                .persistenceUnit("primaryPersistenceUnit")
                .properties(getVendorProperties())
                .build();
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryPrimary",
        transactionManagerRef = "transactionManagerPrimary",
        basePackages = {"com.XX.XX请替换为你的数据源1Repository目录"})
public class PrimaryConfig {

    @Resource
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Resource
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
        //在yml中配置Hibernate,因此此处仅需一个空的Hibernate设置。
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    /**
     * 设置实体类所在位置
     */
    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(primaryDataSource)
                .packages("com.XX.XX请替换为你的数据源1Entity目录")
                .persistenceUnit("primaryPersistenceUnit")
                .properties(getVendorProperties())
                .build();
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }
}

SecondaryConfig.java

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactorySecondary",
        transactionManagerRef = "transactionManagerSecondary",
        basePackages = {"com.XX.XX请替换为你的数据源2Repository目录"})
public class SecondaryConfig {

    @Resource
    @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;

    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }

    @Resource
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
        //在yml中配置Hibernate,因此此处仅需一个空的Hibernate设置。
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secondaryDataSource)
                .packages("com.XX.XX请替换为你的数据源2Entity目录")
                .persistenceUnit("secondaryPersistenceUnit")
                .properties(getVendorProperties())
                .build();
    }

    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }

}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactorySecondary",
        transactionManagerRef = "transactionManagerSecondary",
        basePackages = {"com.XX.XX请替换为你的数据源2Repository目录"})
public class SecondaryConfig {

    @Resource
    @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;

    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }

    @Resource
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
        //在yml中配置Hibernate,因此此处仅需一个空的Hibernate设置。
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secondaryDataSource)
                .packages("com.XX.XX请替换为你的数据源2Entity目录")
                .persistenceUnit("secondaryPersistenceUnit")
                .properties(getVendorProperties())
                .build();
    }

    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }

}

之后配置yml,注入在DruidDBConfig中未设置的值:

druid:
    # 第一个数据源的配置
    driverClassName1: com.microsoft.sqlserver.jdbc.SQLServerDriver
    dbUrl1: jdbc:sqlserver://XXX;databaseName=XXX
    username1: sa
    password1: 123456
    # 测试连通,必须是单条的可顺利执行的SQL语句。
    validationQuery1: select 1
    # 第二个数据源的配置
    driverClassName2: com.microsoft.sqlserver.jdbc.SQLServerDriver
    dbUrl2: jdbc:sqlserver://YYY;databaseName=YYY
    username2: sa
    password2: 123456
    # 测试连通,必须是单条的可顺利执行的SQL语句。
    validationQuery2: select 1
spring:
    # hibernate配置
    jpa:
        database: sql_server
        generate-ddl: true
        show-sql: true
        hibernate:
            ddl-auto: update
            naming:
                physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

一些坑

一定要加log4j依赖

Entity的主键自增策略要从@GeneratedValue改为@GeneratedValue(strategy=GeneratedType.IDENTITY),因为默认方法在Druid下无法工作。