最近因為專案需要,需要在程式中同時存取不同的資料庫。之前使用springboot註解寫入程式碼確實方便快捷,但是如果需要自己需改其中的一些東西來方便自己使用,改起來有點煩,不知道從哪裡入手。寫個這個記錄下。
首先是pom.xml,都是通用的,這裡就不貼出來了。
其次是application.properties配置,筆者因為不同的環境,所以新建了兩個application-dev.properties和application-pro.properties設定檔。
application.properties:指定目前使用該環境的配置.
spring.profiles.active=dev
application-dev.properties的配置(application-pro.properties类似dev的配置)
#datasource1 spring.datasource.username=** spring.datasource.password=***** spring.datasource.jdbc-url=jdbc:mysql://*************** useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.driver-class-name=com.mysql.jdbc.Driver #datasource2 spring.datasource.ds1.username=*** spring.datasource.ds1.password=****** spring.datasource.ds1.jdbc-url=jdbc:mysql://*************** useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.ds1.driver-class-name=com.mysql.jdbc.Drive
不同資料來源的MyBatis (Config1)
@Configuration @MapperScan(basePackages = "com.pet.petgame2.mapper.dao", sqlSessionFactoryRef = "petgame2SessionFactory") public class Config1 { @Bean(name = "petgame2") @ConfigurationProperties(prefix = "spring.datasource") @Primary public DataSource dataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "petgame2SessionFactory") @Primary public SqlSessionFactory sessionFactory(@Qualifier("petgame2") DataSource dataSource) throws Exception{ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:Mapper/pet2Mapper/*.xml")); return factoryBean.getObject(); } @Bean(name = "petgame2TransactionManager") @Primary public DataSourceTransactionManager transactionManager(@Qualifier("petgame2") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } }
不同資料來源的MyBatis (Config2)
#@Configuration @MapperScan(basePackages = "com.pet.petgame2.mapper.dao2", sqlSessionFactoryRef = "petgamesqlSessionFactory") public class Config2 { @Bean(name = "petgame") @ConfigurationProperties(prefix = "spring.datasource.ds1") public DataSource dataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "petgamesqlSessionFactory") public SqlSessionFactory sessionFactory(@Qualifier("petgame") DataSource dataSource) throws Exception{ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:Mapper/petMapper/*.xml")); return factoryBean.getObject(); } @Bean(name = "petgameTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("petgame") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } }
@MapperScan註解會自動掃描mapper的介面文件, factoryBean.setMapperLocations會掃描mapper介面文件對應的Mapper.XML文件,注意這兩個的路徑不要寫錯。 @Primary設定預設的一個資料庫連結配置,必須指定其中一個資料來源為預設。
*注意:如果使用了springboot2.0自帶的Hikari作為連接池,需要注意properties中的
spring.datasource.url改为spring.datasource.jdbc-url
或修改config中DataSource為DataSourceProperties,如下
@Bean(name = "petgame2DataSourceProperties") @Qualifier("petgame2DataSourceProperties") @ConfigurationProperties(prefix = "spring.datasource") @Primary public DataSourceProperties properties(){ return new DataSourceProperties(); } @Bean(name = "petgame2") @ConfigurationProperties(prefix = "spring.datasource") @Primary public DataSource dataSource(){ // return DataSourceBuilder.create().build(); return properties().initializeDataSourceBuilder().build(); }
以上是JAVA開發之springBoot2.0建置雙資料來源的詳細內容。更多資訊請關注PHP中文網其他相關文章!