springboot中durid多資料來源配置
阿新 • • 發佈:2020-12-07
本博主要記錄個人在工作中遇到的問題與解決方案,僅供日後回顧,若對大家有幫助請點贊,謝謝!
springboot中durid多資料來源配置
mybatis: mapperLocations: classpath:/mapper/*.xml configuration: call-setters-on-nulls: true spring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: url: jdbc:mysql://localhost:3306/zjuap #mysql配置 driver-class-name: com.mysql.jdbc.Driver #mysql配置 #url: jdbc:oracle:thin:@127.0.0.1:1521:orcl #oracle配置 #driver-class-name: oracle.jdbc.OracleDriver #oracle配置 username: zjft #密碼加密 java -cp druid-1.1.10.jar com.alibaba.druid.filter.config.ConfigTools you_password #密碼為加密的 zjft8888 password: MA+imF+ye4ptAG+nuJsA/RYs8MZPTXotRC0DBdR5rFeAXHL8h7kxFJEcmukugC1N7g== initial-size: 10 max-active: 20 min-idle: 4 max-wait: 60000 validation-query: SELECT 1 #mysql配置 #validation-query: select 1 from dual #oracle配置 validation-query-timeout: 30000 #配置連線屬性,是否開啟密碼加密,以及配置加密加密的公鑰 public key connection-properties: config.decrypt=true;config.decrypt.key=MFwwDQYJQ/BRnCnkCAwEAAQ== filter: config: enabled: true #如果啟用密碼加密配置的話,這裡必須開啟,目的是注入密碼加解密過濾器
2.資料庫驅動依賴新增
<!--ORM框架--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.9</version> </dependency> <!--Druid連線池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--資料庫驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.3</version> <scope>runtime</scope> </dependency>
3.接下來就是java config的配置
@Configuration @ConditionalOnMissingBean({Marker.class}) //這裡是我的自動裝配條件註解 @MapperScan(basePackages = "com.zjft.uap.mapper.dao", sqlSessionTemplateRef = "zjuapSqlSessionTemplate") public class DefaultDBSourceConfiguration { /** * 建立主資料來源物件,注入到Spring * 可擴充套件多個數據源 */ @Bean(name = "zjuapDataSource") @ConfigurationProperties(prefix = "spring.datasource.druid") //注意這裡,讀取application.yml配置 public DataSource dataSource(ConfigFilter configFilter) { // 這裡是個坑,如果自定義資料來源需要對密碼加密的話,必須要加上這個過濾器,否則連線會報錯 DruidDataSource dataSource = new DruidDataSource(); /* 下面的這個ConfigFilter必須得加,否則在application.yml中配置密碼加密,在啟動專案時會報錯 * [com.alibaba.druid.pool.DruidDataSource] [ERROR] create connection SQLException, url:
*jdbc:mysql://localhost:3306/zjuap, errorCode 1045, state 28000 *java.sql.SQLException: Access denied for user 'zjft'@'localhost' (using password: YES) */ List filters = new ArrayList<ConfigFilter>(){{ add(configFilter); }}; dataSource.setProxyFilters(filters); return dataSource; } /** * 從配置檔案獲取主資料來源的連線資訊 */ @Value("${mybatis.mapperLocations}") private String mapperLocation; @Bean public DatabaseIdProvider databaseIdProvider(){ DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider(); Properties p = new Properties(); p.setProperty("Oracle", "oracle"); p.setProperty("MySQL", "mysql"); databaseIdProvider.setProperties(p); return databaseIdProvider; } /** *建立SqlSessionFactory */ @Bean(name = "zjuapSqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("zjuapDataSource") DataSource dataSource, MybatisProperties mybatisProperties) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocation)); bean.setConfiguration(mybatisProperties.getConfiguration()); bean.setDatabaseIdProvider(databaseIdProvider()); bean.setVfs(SpringBootVFS.class); return bean.getObject(); } /** * 配置事務 */ @Bean(name = "zjuapTransactionManager") @Primary public DataSourceTransactionManager transactionManager(@Qualifier("zjuapDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** *建立SqlSessionTemplate */ @Bean(name = "zjuapSqlSessionTemplate") @Primary public SqlSessionTemplate sqlSessionTemplate(@Qualifier("zjuapSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }