springboot2.0.3 Configuring multiple data sources (SQLite and MySQL) using JPA and Hibernate

Source: Internet
Author: User
Tags sqlite

application.properties configuration:

#Database configuration
mysql.spring.datasource.jdbc-url = jdbc: mysql: //127.0.0.1: 3306 / test
mysql.spring.datasource.username = admin
mysql.spring.datasource.password = 123456
mysql.spring.datasource.driver-class-name = com.mysql.jdbc.Driver
#Database connection pool
mysql.spring.datasource.max-idle = 100
mysql.spring.datasource.max-wait = 10000
mysql.spring.datasource.min-idle = 5
mysql.spring.datasource.initial-size = 5

#sqliteDatabase configuration
sqlite.spring.datasource.jdbc-url = jdbc: sqlite: test.db
sqlite.spring.datasource.driver-class-name = org.sqlite.JDBC
sqlite.spring.datasource.username =
sqlite.spring.datasource.password =

#Turn off hibernate's automatic mechanism for creating table structures
spring.jpa.hibernate.ddl-auto = none
spring.jpa.show-sql = true
DataSourceConfig:

@Configuration
public class DataSourceConfig {

    @Bean (name = "sqliteDataSource")
    @Qualifier (value = "sqliteDataSource") // The unique identifier of the spring assembly bean
    @ConfigurationProperties (prefix = "sqlite.spring.datasource") // Configuration prefix of the data source in the application.properties configuration file
    public DataSource sqliteDataSource () {
        return DataSourceBuilder.create (). build ();
    }

    @Primary // Configure this data source as the main data source
    @Bean (name = "mysqlDataSource")
    @Qualifier (value = "mysqlDataSource")
    @ConfigurationProperties (prefix = "mysql.spring.datasource")
    public DataSource mysqlDataSource () {
        return DataSourceBuilder.create (). build ();
    }
}
MysqlDataSourceConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories (entityManagerFactoryRef = "entityManagerFactoryMysql", // EntityManagerFactory reference
        transactionManagerRef = "transactionManagerMysql", // transactionManager reference
        basePackages = {"com.xxx.mysql"})
public class MysqlDataSourceConfig {
    / **
     * Inject mysql data source
     * /
    @Resource (name = "mysqlDataSource")
    private DataSource mysqlDataSource;

    / **
     * Inject JPA configuration entity
     * /
    @Autowired
    private JpaProperties jpaProperties;

    / **
     * There is no need to configure the dialect of the database.
     * Like hibernate.hbm2ddl.auto can be configured here. But mine is configured in application.properties.
     * /
    private Map <String, Object> getVendorProperties () {
        HibernateSettings hibernateSettings = new HibernateSettings ();
        return jpaProperties.getHibernateProperties (hibernateSettings);
    }

    / **
     * Configure EntityManagerFactory entity
     *
     * @param builder
     * @return entity management factory
     * packages Scan the package names annotated by @Entity
     * persistenceUnit The name of the persistence unit. If only one EntityManagerFactory is established, you can omit this, but if there are multiple in the same application, you should give them different names
     * properties General properties of standard JPA or vendor specific configuration. These attributes override any values provided in the constructor.
     * /
    @Primary
    @Bean (name = "entityManagerFactoryMysql")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysql (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource (mysqlDataSource)
                .properties (getVendorProperties ())
                .packages ("com.xxx.mysql")
                .persistenceUnit ("mysqlPersistenceUnit")
                .build ();
    }

    / **
     * Configure EntityManager entity
     *
     * @param builder
     * @return entity manager
     * /
    @Primary
    @Bean (name = "entityManagerMysql")
    public EntityManager entityManager (EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryMysql (builder) .getObject (). createEntityManager ();
    }


    / **
     * Configure transaction transactionManager
     *
     * @param builder
     * @return transaction manager
     * /
    @Primary
    @Bean (name = "transactionManagerMysql")
    public PlatformTransactionManager transactionManagerMysql (EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager (entityManagerFactoryMysql (builder) .getObject ());
    }

}
SqliteDataSourceConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories (entityManagerFactoryRef = "entityManagerFactorySqlite", // EntityManagerFactory reference
        transactionManagerRef = "transactionManagerSqlite", // transactionManager reference
        basePackages = {"com.xxx.sqlite"})
public class SqliteDataSourceConfig {
    / **
     * Inject sqlite data source
     * /
    @Resource (name = "sqliteDataSource")
    private DataSource sqliteDataSource;

    / **
     * Inject JPA configuration entity
     * /
    @Autowired
    private JpaProperties jpaProperties;

    / **
     * There is no need to configure the dialect of the database.
     * /
    private Map <String, Object> getVendorProperties () {
        HibernateSettings hibernateSettings = new HibernateSettings ();
        return jpaProperties.getHibernateProperties (hibernateSettings);
    }

    / **
     * Configure EntityManagerFactory entity
     *
     * @param builder
     * @return entity management factory
     * packages Scan the package names annotated by @Entity
     * persistenceUnit The name of the persistence unit. If only one EntityManagerFactory is established, you can omit this, but if there are multiple in the same application, you should give them different names
     * properties General properties of standard JPA or vendor specific configuration. These attributes override any values provided in the constructor.
     * /
    @Bean (name = "entityManagerFactorySqlite")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySqlite (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource (sqliteDataSource)
                .properties (getVendorProperties ())
                .packages ("com.xxx.sqlite")
                .persistenceUnit ("sqlitePersistenceUnit")
                .build ();
    }

    / **
     * Configure EntityManager entity
     *
     * @param builder
     * @return entity manager
     * /
    @Bean (name = "entityManagerSqlite")
    public EntityManager entityManager (EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySqlite (builder) .getObject (). createEntityManager ();
    }


    / **
     * Configure transaction transactionManager
     *
     * @param builder
     * @return transaction manager
     * /
    @Bean (name = "transactionManagerSqlite")
    public PlatformTransactionManager transactionManagerSqlite (EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager (entityManagerFactorySqlite (builder) .getObject ());
    }

}
Then com.xxx.mysql and com.xxx.sqlite directories can be placed in their respective domain and repository. It is no different from a single data source.

 

reference:

https://www.cnblogs.com/sxdcgaq8080/p/7978205.html (springboot-1.5.9)

https://my.oschina.net/chinesedragon/blog/1647846 (springboot-2.0.0)

springboot2.0.3 uses jpa and hibernate to configure multiple data sources (sqlite and mysql)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.