multiple datasource in spring boot for mysql and mongo

时间:2017-11-08 22:01:46

标签: java spring spring-boot

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 .e.g

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.

1 个答案:

答案 0 :(得分:0)

这是一个例子 @EnableJpaRepositories

  • 使用basePackages指定要扫描带注释组件的包。

  • 使用em.setPackagesToScan设置是否对类路径中的实体类使用基于Spring的扫描。

让我们在代码之后创建mysql和mongo配置类。

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


}

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


}

实体和存储库类

  • 您可以管理不同的包,因为您的客户详细信息,付款详细信息,所有私人数据类应放入“yourpackage.mysql.entities”,其他数据类如coomon详细信息/可搜索数据将被放入“yourpackage.mongo”。实体“

我希望这会有所帮助