Mybatis多数据源切换实现代码
这次要完成的是从一个数据库中读取数据,然后再把数据插入到另一个数据库中。在同一套项目代码中要完成这个操作,就不可避免的涉及到了多数据源。本文即介绍在mybatis中完成多数据源的切换相关内容
指定数据源一
@Configuration //扫描Mapper接口并容器管理 @MapperScan(basePackages=MasterDataSourceConfig.PACKAGE,sqlSessionFactoryRef="masterSqlSessionFactory") publicclassMasterDataSourceConfig{ //精确到master目录,以便跟其他数据源隔离 staticfinalStringPACKAGE="com.datareach.kafka.dao.master"; staticfinalStringMAPPER_LOCATION="classpath:mapper/master/*.xml"; //application.yml中的值可以通过@Value注解进行读取 @Value("${master.datasource.url}") privateStringurl; @Value("${master.datasource.username}") privateStringuser; @Value("${master.datasource.password}") privateStringpassword; @Value("${master.datasource.driver-class-name}") privateStringdriverClass; @Bean(name="masterDataSource") @Primary publicDataSourcemasterDataSource(){ DruidDataSourcedataSource=newDruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); returndataSource; } @Bean(name="masterTransactionManager") @Primary publicDataSourceTransactionManagermasterTransactionManager(){ returnnewDataSourceTransactionManager(masterDataSource()); } @Bean(name="masterSqlSessionFactory") @Primary publicSqlSessionFactorymasterSqlSessionFactory(@Qualifier("masterDataSource")DataSourcemasterDataSource) throwsException{ finalSqlSessionFactoryBeansessionFactory=newSqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(newPathMatchingResourcePatternResolver() .getResources(MasterDataSourceConfig.MAPPER_LOCATION)); returnsessionFactory.getObject(); } }
数据源一的相关配置
#master数据源配置 master: datasource: url:jdbc:postgresql://localhost:5432/postgres?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8 driver-class-name:org.postgresql.Driver username:product password: initial-size:1 min-idle:1 max-active:20 test-on-borrow:true max-wait:60000 time-between-eviction-runs-millis:60000 min-evictable-idle-time-millis:300000 validation-query:SELECT1FROMDUAL test-While-Idle:true test-on-return:false pool-prepared-statements:false max-pool-prepared-statement-per-connection-size:20 filters:stat,wall,log4j,config
指定数据源二
@Configuration //扫描Mapper接口并容器管理 @MapperScan(basePackages=SecondDataSourceConfig.PACKAGE,sqlSessionFactoryRef="secondSqlSessionFactory") publicclassSecondDataSourceConfig{ //精确到cluster目录,以便跟其他数据源隔离 staticfinalStringPACKAGE="com.datareach.kafka.dao.secondary"; staticfinalStringMAPPER_LOCATION="classpath:mapper/secondary/*.xml"; @Value("${second.datasource.url}") privateStringurl; @Value("${second.datasource.username}") privateStringuser; @Value("${second.datasource.password}") privateStringpassword; @Value("${second.datasource.driver-class-name}") privateStringdriverClass; @Bean(name="secondDataSource") publicDataSourceclusterDataSource(){ DruidDataSourcedataSource=newDruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); returndataSource; } @Bean(name="secondTransactionManager") publicDataSourceTransactionManagerclusterTransactionManager(){ returnnewDataSourceTransactionManager(clusterDataSource()); } @Bean(name="secondSqlSessionFactory") publicSqlSessionFactoryclusterSqlSessionFactory(@Qualifier("secondDataSource")DataSourceclusterDataSource) throwsException{ finalSqlSessionFactoryBeansessionFactory=newSqlSessionFactoryBean(); sessionFactory.setDataSource(clusterDataSource); sessionFactory.setMapperLocations(newPathMatchingResourcePatternResolver() .getResources(SecondDataSourceConfig.MAPPER_LOCATION)); returnsessionFactory.getObject(); } }
数据源二的相关配置
second: datasource: url:jdbc:mysql://localhost:40000/PG_Data?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8 username:root password: driver-class-name:com.mysql.jdbc.Driver max-idle:10 max-wait:10000 min-idle:5 initial-size:5
其实就是实例化了两个SqlSessionFactory——masterSqlSessionFactory和secondSqlSessionFactory,然后通过注解@MapperScan指定扫描指定的mapper接口时用指定的SqlSessionFactory进行连接构建,从而实现了多数据源。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。