春季启动-多数据库访问(MYSQL) [英] Spring boot - Multiple Database Access (MYSQL)

查看:118
本文介绍了春季启动-多数据库访问(MYSQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经在解决我的问题上工作了很长时间,找不到适合我问题的解决方案.

I am working on my Problem for a very long time by now and I can't find a proper solution for my issue.

我需要顺序访问不同的数据库.如您在下面的代码中所见,我试图更改到数据库的链接.

I need sequential access to different databases. I tried to change the link to the databases as u can see in the code below.

public static void changeDB(String dbname) throws IOException{

    File f = new File("application.properties");
    if (f.exists()) {
        f.delete(); 
    }
    f.createNewFile();
    System.out.println("gelöscht");


    FileWriter fw = new FileWriter("application.properties");
    BufferedWriter bw = new BufferedWriter(fw);

    String dbchanger = "spring.datasource.url = jdbc:mysql://localhost:3306/kamis" + dbname;

    bw.write("server.port = 8000");
    bw.write("\n");
    bw.write(dbchanger);
    bw.write("\n");
    bw.write("spring.datasource.username=root");
    bw.write("\n");
    bw.write("spring.datasource.password=");
    bw.write("\n");
    bw.write("spring.datasource.driver-class-name = com.mysql.jdbc.Driver");
    bw.write("\n");
    bw.write("spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect");
    bw.write("\n");
    bw.write("spring.jpa.hibernate.ddl-auto=update");
    bw.write("\n");
    bw.write("spring.devtools.livereload.enabled=true");

    bw.close();
    System.out.println("neu erstellt");

}

到目前为止,这已经以某种方式起作用.问题在于该应用程序需要重新加载以从我的application.properties中读取更改以连接到另一个数据库,这对我而言并不是真正的解决方案,因为该应用程序会重新启动.它也必须是动态可变的,因为我正在使用30多个数据库.

This is working in some way so far. The problem is that the application needs an reload to read the changes from my application.properties to connect to the other database which isnt really a solution for me because the application restarts. It also has to be dynamically changeable because i'm working with more than 30 databases.

也许你们可以帮助我弄清楚如何重新加载它,或者为我提供一种更合适的解决方案来解决我的问题.

Maybe you guys can help me figure out how to reload it or maybe give me a more fitting solution approach to my issue.

先谢谢了. 凯

推荐答案

您可以使用我已经做过的以下配置在Spring Boot中使用多个数据库. Application.properties:-

You can use the following configuration i have done to use multiple database in spring boot. Application.properties:-

server.port=6060
spring.ds_post.url =jdbc:postgresql://localhost:5432/kode12
spring.ds_post.username =postgres
spring.ds_post.password =root
spring.ds_post.driverClassName=org.postgresql.Driver
spring.ds_mysql.url = jdbc:mysql://localhost:3306/kode12
spring.ds_mysql.username = root
spring.ds_mysql.password = root
spring.ds_mysql.driverClassName=com.mysql.jdbc.Driver

MultipleDBConfig.java

@Configuration
public class MultipleDBConfig {
    @Bean(name = "mysqlDb")
    @ConfigurationProperties(prefix = "spring.ds_mysql")
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "mysqlJdbcTemplate")
    public JdbcTemplate jdbcTemplate(@Qualifier("mysqlDb") DataSource dsMySQL) {
        return new JdbcTemplate(dsMySQL);
    }

    @Bean(name = "postgresDb")
    @ConfigurationProperties(prefix = "spring.ds_post")
    public DataSource postgresDataSource() {
        return  DataSourceBuilder.create().build();
    }

    @Bean(name = "postgresJdbcTemplate")
    public JdbcTemplate postgresJdbcTemplate(@Qualifier("postgresDb") 
                                              DataSource dsPostgres) {
        return new JdbcTemplate(dsPostgres);
    }
}

这是带注释的配置类,其中包含函数和 用于加载我们的PostgreSQL和MySQL配置的注释.它是 还负责为每个实例创建一个JDBCTemplate实例.

This is the annotated configuration class, containing functions and annotations for loading our PostgreSQL and MySQL configuration. It is also responsible for creating a JDBCTemplate instance for each.

这篇关于春季启动-多数据库访问(MYSQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆