在開發微服務的過程當中,有時咱們的服務須要鏈接兩個以上的數據庫進行業務數據的CRUD操做,這時候就須要咱們進行多數據源的配置。java
pom.xmlmysql
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.tl</groupId> <artifactId>hello-spring-boot-dependencies</artifactId> <version>1.0.0</version> <relativePath>../hello-spring-boot-dependencies/pom.xml</relativePath> </parent> <artifactId>hello-spring-boot-mybatis-hikari</artifactId> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--devtools熱部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- mysql8 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- Swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> </project>
主要配置文件web
datasource.propertiesspring
hikari.primary.jdbc-url=jdbc:mysql://localhost:3306/source_data hikari.primary.username=root hikari.primary.password=root hikari.primary.driver-class-name=com.mysql.cj.jdbc.Driver hikari.second.jdbc-url=jdbc:mysql://localhost:3306/target_data hikari.second.username=root hikari.second.password= root hikari.second.driver-class-name=com.mysql.cj.jdbc.Driver
數據庫鏈接池配置sql
PrimaryDataSource.java數據庫
package com.tl.hello.spring.boot.mybatis.hikari.config; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.PropertySource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * @author tianl * @date 2020/3/27 22:11 */ @Configuration @MapperScan(basePackages = {"com.tl.hello.spring.boot.mybatis.druid.dao.primary"}, sqlSessionFactoryRef = "primarySqlSessionFactory") @PropertySource("classpath:config/datasource.properties") public class PrimaryDataSourceConfig { @Value("${hikari.primary.jdbc-url}") private String jdbcUrl; @Value("${hikari.primary.username}") private String username; @Value("${hikari.primary.password}") private String password; @Value("${hikari.primary.driver-class-name}") private String driverClassName; @Bean(name = {"primaryDataSource"}) @Primary public HikariDataSource dataSource() { HikariDataSource hikariDataSource = new HikariDataSource(); hikariDataSource.setJdbcUrl(jdbcUrl); hikariDataSource.setUsername(username); hikariDataSource.setPassword(password); hikariDataSource.setDriverClassName(driverClassName); return hikariDataSource; } @Bean(name = "primaryTransactionManager") @Primary public DataSourceTransactionManager transactionManager() { return new DataSourceTransactionManager(this.dataSource()); } @Bean(name = "primarySqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sourcedata/*Mapper.xml")); return sessionFactory.getObject(); } }
SecondDataSource.javaapache
package com.tl.hello.spring.boot.mybatis.hikari.config; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * @author tianl * @date 2020/3/27 22:53 */ @Configuration @MapperScan(basePackages = {"com.tl.hello.spring.boot.mybatis.druid.dao.second"},sqlSessionFactoryRef = "secondSqlSessionFactory") @PropertySource("classpath:config/datasource.properties") public class SecondDataSourceConfig { @Value("${hikari.second.jdbc-url}") private String jdbcUrl; @Value("${hikari.second.username}") private String username; @Value("${hikari.second.password}") private String password; @Value("${hikari.second.driver-class-name}") private String driverClassName; @Bean(name = "secondDataSource") public HikariDataSource dataSource(){ HikariDataSource hikariDataSource = new HikariDataSource(); hikariDataSource.setJdbcUrl(jdbcUrl); hikariDataSource.setUsername(username); hikariDataSource.setPassword(password); hikariDataSource.setDriverClassName(driverClassName); return hikariDataSource; } @Bean(name = "secondTransactionManager") public DataSourceTransactionManager transactionManager() { return new DataSourceTransactionManager(this.dataSource()); } @Bean(name = "secondSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mapper/targetdata/*Mapper.xml")); return sessionFactory.getObject(); } }
測試:啓動一個定時任務同步連個數據中數據session
package com.tl.hello.spring.boot.mybatis.hikari.service; import com.tl.hello.spring.boot.mybatis.hikari.model.primary.PrimaryUser; import com.tl.hello.spring.boot.mybatis.hikari.model.second.SecondUser; import com.tl.hello.spring.boot.mybatis.hikari.service.primary.PrimaryUserService; import com.tl.hello.spring.boot.mybatis.hikari.service.second.SecondUserService; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * @author tianl * @date 2020/3/28 0:51 */ @Service public class SyncDataService { @Resource private PrimaryUserService primaryUserService; @Resource private SecondUserService secondUserService; @Scheduled(cron = "0/5 * * * * ?") public void sync(){ PrimaryUser primaryUser = primaryUserService.selectByPrimaryKey(19L); SecondUser secondUser=new SecondUser(); secondUser.setAge(primaryUser.getAge()); secondUser.setName(primaryUser.getName()); secondUser.setUsername(primaryUser.getUsername()); secondUser.setPassword(primaryUser.getPassword()); secondUserService.insertOrUpdate(secondUser); } }
代碼完成地址:[碼雲]mybatis