springboot+mysql 多数据源配置

    技术2022-07-11  80

    Springboot 多数据源配置

    1、配置文件
    #主数据源配置 master.spring.datasource.driver = com.mysql.jdbc.Driver master.spring.datasource.url = ... master.spring.datasource.username = ... master.spring.datasource.password = ... master.spring.datasource.initialPoolSize = 1 master.spring.datasource.minIdle = 10 master.spring.datasource.maxActive = 100 #配置另一个数据源 second.spring.datasource.driver = com.mysql.jdbc.Driver second.spring.datasource.url = ... second.spring.datasource.username = ... second.spring.datasource.password = ... second.spring.datasource.initialPoolSize = 1 second.spring.datasource.minIdle = 10 second.spring.datasource.maxActive = 100
    2、创建生成数据源的类
    @Configuration @Profile(value = {"default", "prod"}) public class DataSourceConfig { @Value("${master.spring.datasource.url}") private String url; @Value("${master.spring.datasource.username}") private String user; @Value("${master.spring.datasource.password}") private String password; @Value("${master.spring.datasource.driver}") private String driverClass; @Value("${master.spring.datasource.initialPoolSize}") private int initialPoolSize; @Value("${master.spring.datasource.minIdle}") private int minIdle; @Value("${master.spring.datasource.maxActive}") private int maxActive; @Value("${second.spring.datasource.url}") private String url2; @Value("${second.spring.datasource.username}") private String user2; @Value("${second.spring.datasource.password}") private String password2; @Value("${second.spring.datasource.driver}") private String driverClass2; @Value("${second.spring.datasource.initialPoolSize}") private int initialPoolSize2; @Value("${second.spring.datasource.minIdle}") private int minIdle2; @Value("${second.spring.datasource.maxActive}") private int maxActive2; @Primary @Bean(name = "localDataSource", destroyMethod = "close", autowire = Autowire.BY_NAME) public DruidDataSource localDataSource() { return getDruidDataSource(url, user, password, initialPoolSize, minIdle, maxActive); } @Bean(name = "local2DataSource", destroyMethod = "close", autowire = Autowire.BY_NAME) public DruidDataSource local2DataSource() { return getDruidDataSource(url2, user2, password2, initialPoolSize2, minIdle2, maxActive2); } //数据源生成函数 private DruidDataSource getDruidDataSource(String jdbcUrl, String jdbcUser, String jdbcPassword, int initialPoolSize, int minIdle, int maxActive) { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName("com.mysql.jdbc.Driver"); druidDataSource.setUrl(jdbcUrl); druidDataSource.setUsername(jdbcUser); druidDataSource.setPassword(jdbcPassword); druidDataSource.setInitialSize(initialPoolSize); druidDataSource.setMinIdle(minIdle); druidDataSource.setMaxActive(maxActive); return druidDataSource; } }
    3、配置数据源1
    @Configuration //注意basePackages中的路径一定要扫描到数据源1repository具体的文件夹下,xxx是自己的路径 @MapperScan(basePackages = {"com.xxx.xxx.xxx.xxx"}, sqlSessionTemplateRef = "localSqlSessionTemplate") @EnableApolloConfig("mysql") public class LocalDaoConfiguration { @Primary @Bean(name = "localSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //xxx数据源1 xml是自己的路径 Resource[] resources = resolver.getResources("classpath:mapping/xxx/*.xml"); Resource[] resArray = Arrays.copyOf(resources, resources.length); sqlSessionFactoryBean.setMapperLocations(resArray); return sqlSessionFactoryBean.getObject(); } @Primary @Bean(name = "localTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("localDataSource") DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } @Primary @Bean(name = "localTransactionTemplate") public TransactionTemplate transactionTemplate( @Qualifier("localTransactionManager") DataSourceTransactionManager transactionManager) { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); return transactionTemplate; } @Primary @Bean(name = "localSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate( @Qualifier("localSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
    4、配置数据源2
    @Configuration //注意basePackages中的路径一定要扫描到数据源2repository具体的文件夹下,xxx是自己的路径 @MapperScan(basePackages = {"com.xxx.xxx.xxx.xxx"}, sqlSessionTemplateRef = "local2SqlSessionTemplate") @EnableApolloConfig("mysql") public class LocalDaoConfiguration { @Primary @Bean(name = "local2SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("local2DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //xxx是数据源2 自己的路径 Resource[] resources = resolver.getResources("classpath:mapping/xxx/*.xml"); Resource[] resArray = Arrays.copyOf(resources, resources.length); sqlSessionFactoryBean.setMapperLocations(resArray); return sqlSessionFactoryBean.getObject(); } @Primary @Bean(name = "local2TransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("local2DataSource") DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } @Primary @Bean(name = "local2TransactionTemplate") public TransactionTemplate transactionTemplate( @Qualifier("local2TransactionManager") DataSourceTransactionManager transactionManager) { TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); return transactionTemplate; } @Primary @Bean(name = "local2SqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate( @Qualifier("local2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
    5、就可以愉快的运行了
    Processed: 0.010, SQL: 9