一个SpringBoot项目,同时连接两个数据库:比如一个是Mysql数据库,一个是oracle数据库(啥数据库都一样,连接两个同为oracle的数据库,或两个不同的数据库,只需要更改对应的driver-class-name和jdbc-url等即可)注意:连接什么数据库,要引入对应数据库的包。
第一步:
导入pom
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>
第二步:
修改application.yml配置文件(我采用本地的,IP地址是一致的,实际开发中,是两台云服务,两台MySQL地址进行主从读写)
mysql1:driver-class-name: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8username:"root"password:"12345678"type: com.alibaba.druid.pool.DruidDataSource
mysql2:driver-class-name: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/test1?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8username:"root"password:"12345678"type: com.alibaba.druid.pool.DruidDataSource
注意
格式
上面spring.datasource省略了
第三步:
建造配置类:
(1)第一个库配置信息:
/**
* 数据库leadnews_article
*/@Configuration@MapperScan(basePackages ={"com.example.demo.mapper.db1"}, sqlSessionFactoryRef ="sqlSessionFactoryArticle")publicclassDBSrcArticle{@BeanpublicSqlSessionFactorysqlSessionFactoryArticle(@Qualifier("mysql1")DataSource dataSource)throwsException{MybatisSqlSessionFactoryBean sqlSessionFactory =newMybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(newPathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));return sqlSessionFactory.getObject();}@BeanpublicSqlSessionTemplatesqlSessionTemplateArticle(@Qualifier("sqlSessionFactoryArticle")SqlSessionFactory sqlSessionFactory)throwsException{returnnewSqlSessionTemplate(sqlSessionFactory);}}
(2)第二个库配置
/**
* 数据库leadnews_user
*/@Configuration@MapperScan(basePackages ={"com.example.demo.mapper.db2"}, sqlSessionFactoryRef ="sqlSessionFactoryUser")publicclass dBSrcUser {@Bean@PrimarypublicSqlSessionFactorysqlSessionFactoryUser(@Qualifier("mysql2")DataSource dataSource)throwsException{MybatisSqlSessionFactoryBean sqlSessionFactory =newMybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(newPathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*.xml"));return sqlSessionFactory.getObject();}@Bean@PrimarypublicSqlSessionTemplatesqlSessionTemplateUser(@Qualifier("sqlSessionFactoryUser")SqlSessionFactory sqlSessionFactory)throwsException{returnnewSqlSessionTemplate(sqlSessionFactory);}}
(3)数据源配置:
/**
* 数据源配置
*/@ConfigurationpublicclassDataSourceConfig{@Primary@Bean(name ="mysql1")@ConfigurationProperties(prefix ="spring.datasource.mysql1")publicDataSourcedBSrcArticle(){returnDataSourceBuilder.create().build();}@Primary@Bean(name ="mysql2")@ConfigurationProperties(prefix ="spring.datasource.mysql2")publicDataSourcedBSrcUser(){returnDataSourceBuilder.create().build();}}
注意:连接两个以上的数据库,需要对mapper文件夹进行
分包
!
第四步:
在启动类中加上这三个注解:
@SpringBootApplication(exclude ={DataSourceAutoConfiguration.class})@MapperScan(basePackages ={"com.example.demo.entity.db1"}, sqlSessionFactoryRef ="sqlSessionFactoryArticle")@MapperScan(basePackages ={"com.example.demo.entity.db2"}, sqlSessionFactoryRef ="sqlSessionFactoryUser")
此时:已经可以访问两个数据库内容了。
版权归原作者 爱写Bug的小孙 所有, 如有侵权,请联系我们删除。