SpringBoot配置多数据源实现过程

最近想写一个能用的API服务程序,它将实现我平常所用到的所有API服务请求,那它涉及的数据源肯定就不止一个,关于多数据源我的配置如下 (SpringBoot1.5.6)

  • 先关闭SpringBoot对于MyBatis的自动注入
@EnableAutoConfiguration(exclude= {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,MybatisAutoConfiguration.class})
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • 配置数据库连接类
@Configuration
@MapperScan(basePackages = "com.miapecloud.mapper.shiroAction",sqlSessionFactoryRef = "shiroActionSqlSessionFactory")
public class ShiroActionDataSourceConfig {
	@Primary
	@Bean(name = "shiroActionDataSource")
    @ConfigurationProperties("datasource.shiroAction")
    public DataSource shiroActionDataSource(){
        return DataSourceBuilder.create().build();
    }
    
    @Bean(name = "shiroActionSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("shiroActionDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
        .getResources("classpath:mapper/shiroAction/*.xml"));
        return sessionFactoryBean.getObject();
    }
}

这里的 basePackages = “com.miapecloud.mapper.shiroAction” 表示此数据源对应的Mapper包路径; @ConfigurationProperties(“datasource.shiroAction”) 表示application.properties中数据源配置前缀。类似其它数据源也是给出这样一个配置类。

  • application.properties配置配置数据源
#video数据库(视频库)
datasource.video.url=jdbc:mysql://127.0.0.1:3306/video?useSSL=false&characterEncoding=UTF8
datasource.video.driverClassName=com.mysql.jdbc.Driver
datasource.video.username=root
datasource.video.password=123456
datasource.video.type=com.alibaba.druid.pool.DruidDataSource
#以下为连接池的相关参数配置(防止连接长时间不操作断开)
datasource.video.max-idle=10
datasource.video.max-wait=10000
datasource.video.min-idle=5
datasource.video.initial-size=5
datasource.video.validation-query=SELECT 1
datasource.video.test-on-borrow=false
datasource.video.test-while-idle=true
datasource.video.time-between-eviction-runs-millis=18800

#shiro_action数据库(Shiro库)
datasource.shiroAction.url=jdbc:mysql://127.0.0.1:3304/shiro_action?useSSL=false&characterEncoding=UTF8
datasource.shiroAction.driverClassName=com.mysql.jdbc.Driver
datasource.shiroAction.username=root
datasource.shiroAction.password=123456
datasource.shiroAction.type=com.alibaba.druid.pool.DruidDataSource
#以下为连接池的相关参数配置(防止连接长时间不操作断开)
datasource.shiroAction.max-idle=10
datasource.shiroAction.max-wait=10000
datasource.shiroAction.min-idle=5
datasource.shiroAction.initial-size=5
datasource.shiroAction.validation-query=SELECT 1
datasource.shiroAction.test-on-borrow=false
datasource.shiroAction.test-while-idle=true
datasource.shiroAction.time-between-eviction-runs-millis=18800

这样就实现了SpringBoot多数据源配置,配置中一定要添加连接池配置,否则长时间不操作连接会断开但是Spring不知道。具体的项目结构如下:
SpringBoot配置多数据源实现过程_第1张图片

你可能感兴趣的:(springboot,多数据源,JAVA,数据库)