簡體   English   中英

連接到多個db(n db)Java Spring Hibernate

[英]Connect to a multiple db(n db) java spring hibernate

我想在春季啟動時制作一個新的MS,我的應用程序需要連接到多個數據庫(目前它有40個不同的服務器,但可以說N)。 我有一個api,可以給我我想要的數據庫的用戶名和密碼。 我想要一張DB地圖。 我只看到配置

#application.properties

dbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mydb
jdbc.username = root
jdbc.password = password
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = false
hibernate.format_sql = false

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.springhibernate.example.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {


@Autowired
private Environment environment;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.springhibernate.example.model" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
 }

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    return dataSource;
}

private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    return properties;        
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
   HibernateTransactionManager txManager = new HibernateTransactionManager();
   txManager.setSessionFactory(s);
   return txManager;
}

我想進行api調用以獲取基數,然后再初始化db,這可能嗎?

嘗試使用spring事務管理器並為每個數據庫配置任意數量的數據庫,並定義單獨的數據源,然后便可以使用spring和hibernate配置多個數據庫。 在此處輸入鏈接說明

點擊此處查看更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM