最近在一个需要中需要去连接其他的项目的数据库,本来想使用jbdc直接链接下的,反正就是获取数据,但是想想还是算了,正好springboot数据源配置还没有玩过,那么就正好趁这次机会学习并使用下
至于pom文件需要的依赖自然不必多说。直接从yml文件说起
1 spring: 2 datasource: 3 druid: 4 master: 5 driver-class-name: com.mysql.cj.jdbc.Driver 6 url: jdbc:mysql://xxxx:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false 7 username: root 8 password: 111111 9 slave: 10 url: jdbc:mysql://yyyy:3306/test?useUnicode=true&characherEncoding=utf-8&useSSL=false 11 username: root 12 password: 111111 13 driver-class-name: com.mysql.cj.jdbc.Driver 14 db-type: com.alibaba.druid.pool.DruidDataSource 15 initial-size: 50 16 min-idle: 50 17 max-active: 100 18 max-wait: 60000 19 time-between-eviction-runs-millis: 60000
注意其中加粗的部分:
master表示主数据源,slave表示从数据源。公共的部分可以直接写在druid节点下面。下面需要注意的时候针对不同的数据源需要有不同的配置文件
主数据源的配置类
1 package com.test; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import org.apache.ibatis.session.SqlSessionFactory; 5 import org.mybatis.spring.SqlSessionFactoryBean; 6 import org.mybatis.spring.annotation.MapperScan; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Value; 9 import org.springframework.context.annotation.Bean; 10 import org.springframework.context.annotation.Configuration; 11 import org.springframework.context.annotation.Primary; 12 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 13 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 14 15 import javax.sql.DataSource; 16 17 @Configuration 18 // 扫描 Mapper 接口并容器管理 19 @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") 20 public class MasterDataSourceConfig { 21 22 //mapper接口文件存放的目录 精确到 master 目录,以便跟其他数据源隔离 23 static final String PACKAGE = "com.test.mapper.master"; 24 static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml"; 25 26 @Value("${spring.datasource.druid.master.url}") 27 private String url; 28 29 @Value("${spring.datasource.druid.master.username}") 30 private String user; 31 32 @Value("${spring.datasource.druid.master.password}") 33 private String password; 34 35 @Value("${spring.datasource.druid.master.driver-class-name}") 36 private String driverClass; 37 38 /** 39 * 默认数据源 40 * @return 41 */ 43 @Bean 44 @Primary 45 public DataSource masterDataSource() { 46 DruidDataSource dataSource = new DruidDataSource(); 47 dataSource.setDriverClassName(driverClass); 48 dataSource.setUrl(url); 49 dataSource.setUsername(user); 50 dataSource.setPassword(password); 51 return dataSource; 52 } 53 54 //@Bean(name = "masterTransactionManager") 55 @Bean 56 @Primary 57 public DataSourceTransactionManager masterTransactionManager() { 58 return new DataSourceTransactionManager(masterDataSource()); 59 } 60 61 62 @Bean 63 @Primary 64 public SqlSessionFactory masterSqlSessionFactory(@Autowired DataSource masterDataSource) 65 throws Exception { 66 final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); 67 sessionFactory.setDataSource(masterDataSource); 68 sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() 69 .getResources(MasterDataSourceConfig.MAPPER_LOCATION)); 70 return sessionFactory.getObject(); 71 } 72 }
从数据源的配置类
1 package com.test; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import org.apache.ibatis.session.SqlSessionFactory; 5 import org.mybatis.spring.SqlSessionFactoryBean; 6 import org.mybatis.spring.annotation.MapperScan; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Value; 9 import org.springframework.context.annotation.Bean; 10 import org.springframework.context.annotation.Configuration; 11 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 12 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 13 14 import javax.sql.DataSource; 15 16 17 @Configuration 18 // 扫描 Mapper 接口并容器管理 19 @MapperScan(basePackages = SlaveDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "slaveSqlSessionFactory") 20 public class SlaveDataSourceConfig { 21 22 //mapper接口文件的存放目录 精确到 slave目录,以便跟其他数据源隔离 23 static final String PACKAGE = "com.test.mapper.slave"; 24 static final String MAPPER_LOCATION = "classpath:mapper/slave/*.xml"; 25 26 @Value("${spring.datasource.druid.slave.url}") 27 private String url; 28 29 @Value("${spring.datasource.druid.slave.username}") 30 private String user; 31 32 @Value("${spring.datasource.druid.slave.password}") 33 private String password; 34 35 @Value("${spring.datasource.druid.slave.driver-class-name}") 36 private String driverClass; 37 38 39 @Bean 40 public DataSource slaveDataSource() { 41 DruidDataSource dataSource = new DruidDataSource(); 42 dataSource.setDriverClassName(driverClass); 43 dataSource.setUrl(url); 44 dataSource.setUsername(user); 45 dataSource.setPassword(password); 46 return dataSource; 47 } 48 49 50 @Bean 51 public DataSourceTransactionManager slaveTransactionManager() { 52 return new DataSourceTransactionManager(slaveDataSource()); 53 } 54 55 56 @Bean 57 public SqlSessionFactory slaveSqlSessionFactory(@Autowired DataSource slaveDataSource) 58 throws Exception { 59 final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); 60 sessionFactory.setDataSource(slaveDataSource); 61 sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() 62 .getResources(SlaveDataSourceConfig.MAPPER_LOCATION)); 63 return sessionFactory.getObject(); 64 } 65 }
总结:上面的配置类的位置是随便放的,方式MappSacan的位置需要注意,数据源扫描哪个mapper目录。那么该mapper目录就默认使用当前是数据源
参考链接:https://zhuanlan.zhihu.com/p/31575567