简体   繁体   中英

multiple datasource in spring boot for mysql and mongo

i am new to spring boot. My requirement is to use both mysql and mongo database. For some transaction related operation i will use mysql and for fetching purpose i will store/retrieve the data to mongo. I went through a lots of exampe, now i am completely confused. i want some structure like JPArepository, where i can use the inbuilt methods .eg

public interface CustomerRepository extends MongoRepository<Customer, String> {}

and

public interface UserRepository extends CrudRepository<User, Long> { }

Just mention the connection and that will point to the right database. i have lots of tables in both the database. A sample example will also be helpful how to write queries using the different connections.

This is an example @EnableJpaRepositories

  • using basePackages for specifying the packages to scan for annotated components.

  • using em.setPackagesToScan for set whether to use Spring-based scanning for entity classes in the classpath.

Let's create mysql and mongo config class following code.

For MySQL

@Configuration
@EnableJpaRepositories(basePackages = {"yourpackage.mysql.repositories"},
    entityManagerFactoryRef = "mysqlEntityManagerFactory",
    transactionManagerRef = "mysqlTransactionManager")
@EnableTransactionManagement
public class mysqlConfig {

@Bean(name = "mysqlEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSourceWrite());
    em.setPackagesToScan(new String[]{"yourpackage.mysql.entities"});
    em.setPersistenceUnitName("trueid");
    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
}

@Bean(name = "dataSourceMysql",destroyMethod = "close")
public DataSource dataSource() {
    ...
    return dataSource;
}


@Bean(name = "mysqlTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier(value = "entityManagerFactory") EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory);

    return transactionManager;
}

@Bean(name = "mysqlExceptionTranslation")
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
}

Properties additionalProperties() {

    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", ...);
    properties.setProperty("hibernate.show_sql", "false");
    return properties;
}


}

For Mongo

@Configuration
@EnableJpaRepositories(basePackages = {"yourpackage.mongo.repositories"},
    entityManagerFactoryRef = "mongoEntityManagerFactory",
    transactionManagerRef = "mongoTransactionManager")
@EnableTransactionManagement
public class mongoConfig {

@Bean(name = "mongoEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSourceWrite());
    em.setPackagesToScan(new String[]{"yourpackage.mongo.entities"});
    em.setPersistenceUnitName("trueid");
    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
}

@Bean(name = "dataSourceMongo",destroyMethod = "close")
public DataSource dataSource() {
    ...
    return dataSource;
}


@Bean(name = "mongoTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier(value = "mongoEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory);

    return transactionManager;
}

@Bean(name = "mongoExceptionTranslation")
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
}

Properties additionalProperties() {

    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", ...);
    properties.setProperty("hibernate.show_sql", "false");
    return properties;
}


}

Entities and Repositories class

  • You can manage different package as your detail the customer, payment details, all private data class should put to "yourpackage.mysql.entities" and other data class like coomon details / searchable data will be put to "yourpackage.mongo.entities"

I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM