java專案同時訪問兩個資料庫的配置
阿新 • • 發佈:2020-12-13
技術標籤:java專案同時訪問兩個資料庫的配置mybatismysqlspring boot
java專案同時訪問兩個資料庫的配置
(1)在application.yml配置檔案中對兩個資料庫進行訪問配置(任何兩種資料庫都可以用如下的配置)
# ----------------------------------------------------------------> 資料庫 datasource: one: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/anji_tea?useSSL = false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 username: root password: 123456 two: driver-class-name: org.postgresql.Driver url: jdbc:postgresql://localhost:5432/postgis_24_sample username: postgres password: 123456
(2)分別建立兩個類進行兩個資料庫訪問的配置
①資料庫1
package com.sunvua.config; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; //import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * 資料庫1配置 * @author */ @Configuration @MapperScan(basePackages = "com.sunvua.sys.**.mapper.**", sqlSessionTemplateRef = "oneSqlSessionTemplate") public class OneDataSourceConfig { @Bean(name = "oneDataSource") @ConfigurationProperties(prefix = "spring.datasource.one") @Primary public DataSource oneDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "oneSqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml")); return bean.getObject(); } @Bean(name = "oneTransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "oneSqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
②資料庫2
package com.sunvua.config; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * 資料庫2配置 * @author */ @Configuration @MapperScan(basePackages = "com.sunvua.twokinds.**.mapper.**",sqlSessionTemplateRef = "twoSqlSessionTemplate") public class TwoDataSourceConfig { @Bean(name = "twoDataSource") @ConfigurationProperties(prefix = "spring.datasource.two") public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "twoSqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("twoDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/**/*.xml")); return bean.getObject(); } @Bean(name = "twoTransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("twoDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "twoSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("twoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }
.xml分佈: