如何在springboot里面配置多个数据源,并且配置多个连接池?持久层用的是springdatajpa请大神们回答一下 100
两个同时可共用,或者通过配置哪个用哪个。最好是亲自做过的,百度上的东西我都看过了,跟实际有点出入...
两个同时可共用,或者通过配置哪个用哪个。最好是亲自做过的,百度上的东西我都看过了,跟实际有点出入
展开
1个回答
展开全部
1.首先在创建应用对象时引入autoConfig

package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

2.其次配置文件

######primary#############
datasource.primary.url=jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=PlayNowLog
datasource.primary.username=sa
datasource.primary.password=xxxxxx
datasource.primary.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
######secondary#############
datasource.secondary.url=jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=PlayNow_New
datasource.secondary.username=sa
datasource.secondary.password=xxxxxx
datasource.secondary.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

3.再其次是数据源的创建

@Configuration
public class GlobalDataConfiguration {
@Bean(name="primaryDataSource")

package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

2.其次配置文件

######primary#############
datasource.primary.url=jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=PlayNowLog
datasource.primary.username=sa
datasource.primary.password=xxxxxx
datasource.primary.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
######secondary#############
datasource.secondary.url=jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=PlayNow_New
datasource.secondary.username=sa
datasource.secondary.password=xxxxxx
datasource.secondary.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

3.再其次是数据源的创建

@Configuration
public class GlobalDataConfiguration {
@Bean(name="primaryDataSource")
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询