Springboot多数据源配置
发布时间:
2023-03-02
预览次数:
多数据源配置文件
#数据库相关配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/myserver?characterEncoding=UTF8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.max-idle=5 spring.datasource.max-wait=10000 spring.datasource.min-idle=1 spring.datasource.initial-size=1 #数据库相关配置 spring.datasource.mcms.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.mcms.jdbc-url=jdbc:mysql://localhost:3306/mcms?characterEncoding=UTF8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false spring.datasource.mcms.username=root spring.datasource.mcms.password=123456 spring.datasource.mcms.max-idle=5 spring.datasource.mcms.max-wait=10000 spring.datasource.mcms.min-idle=1 spring.datasource.mcms.initial-size=1
java文件,注意@Primary主数据源才配这个,次数据源去掉这个配置。
@Configuration public class ServerConfig implements WebMvcConfigurer { //多数据源配置,https://www.jianshu.com/p/34730e595a8c,https://gitee.com/didispace/SpringBoot-Learning/tree/master/1.x/Chapter3-2-3 @Bean(name = "mcmsJdbcTemplate") public JdbcTemplate mcmsJdbcTemplate( @Qualifier("mcmsDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "mcmsDataSource") @Qualifier("mcmsDataSource") //@Primary @ConfigurationProperties(prefix="spring.datasource.mcms") public DataSource mcmsDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "dataSource") @Qualifier("dataSource") @Primary @ConfigurationProperties(prefix="spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); }
使用
@Autowired @Qualifier("mcmsJdbcTemplate") private JdbcTemplate mcmsJdbcTemplate;
上一篇:
Three.js中文手册