三方组件pom:
<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.4</version> </dependency>
使用方式:
1、properties文件配置多数据源,设置主数据源(这里的登录信息的ip端口过滤处理了,替换成自己的即可)
spring.datasource.dynamic.primary=master spring.datasource.dynamic.datasource.mysql.url=jdbc:mysql://.../emaotong?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true spring.datasource.dynamic.datasource.mysql.username= spring.datasource.dynamic.datasource.mysql.password= spring.datasource.dynamic.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.dynamic.datasource.oracle.url=jdbc:oracle:thin:@.../orcl spring.datasource.dynamic.datasource.oracle.username= spring.datasource.dynamic.datasource.oracle.password= spring.datasource.dynamic.datasource.oracle.driver-class-name=oracle.jdbc.driver.OracleDriver
2、在service的对应方法上使用@DS注解指定使用的数据源,不指定则使用默认数据源
@Override @DS("oracle") public int oracleInvoiceCount() { return testMapper.oracleInvoiceCount(); } @Override @DS("mysql") public int mysqlInvoiceCount() { return testMapper.mysqlInvoiceCount(); }
项目结构:
全部代码:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zhen.study</groupId> <artifactId>dynamic-datasource</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dynamic-datasource</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <scope>runtime</scope> </dependency> <!-- https://mvnrepository.com/artifact/cn.easyproject/orai18n --> <dependency> <groupId>cn.easyproject</groupId> <artifactId>orai18n</artifactId> <version>12.1.0.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.3.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>pom.xml
server.port=8080 mybatis.mapper-locations=classpath:mapper/*.xml spring.datasource.dynamic.primary=master spring.datasource.dynamic.strict=false spring.datasource.dynamic.datasource.mysql.url=jdbc:mysql://.../emaotong?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true spring.datasource.dynamic.datasource.mysql.username= spring.datasource.dynamic.datasource.mysql.password= spring.datasource.dynamic.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.dynamic.datasource.oracle.url=jdbc:oracle:thin:@.../orcl spring.datasource.dynamic.datasource.oracle.username= spring.datasource.dynamic.datasource.oracle.password= spring.datasource.dynamic.datasource.oracle.driver-class-name=oracle.jdbc.driver.OracleDriverapplication.properties
package com.zhen.study.dynamicdatasource; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author zhen */ @SpringBootApplication @MapperScan("com.zhen.study.dynamicdatasource.mapper") public class DynamicDatasourceApplication { public static void main(String[] args) { SpringApplication.run(DynamicDatasourceApplication.class, args); } }DynamicDatasourceApplication
package com.zhen.study.dynamicdatasource.mapper; /** * @author zhen */ public interface TestMapper { /** * 查询oracle中发票数 * @return */ public int oracleInvoiceCount(); /** * 查询mysql中发票数 * @return */ public int mysqlInvoiceCount(); }TestMapper
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zhen.study.dynamicdatasource.mapper.TestMapper"> <select id="oracleInvoiceCount" resultType="java.lang.Integer"> select count(*) from so_saleinvoice </select> <select id="mysqlInvoiceCount" resultType="java.lang.Integer"> select count(*) from sale_invoice </select> </mapper>TestMapper.xml
package com.zhen.study.dynamicdatasource.service; /** * @author zhen */ public interface TestService { /** * 查询oracle中发票数 * @return */ public int oracleInvoiceCount(); /** * 查询mysql中发票数 * @return */ public int mysqlInvoiceCount(); }TestService
package com.zhen.study.dynamicdatasource.service.impl; import com.baomidou.dynamic.datasource.annotation.DS; import com.zhen.study.dynamicdatasource.mapper.TestMapper; import com.zhen.study.dynamicdatasource.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author zhen */ @Service public class TestServiceImpl implements TestService { @Autowired private TestMapper testMapper; @Override @DS("oracle") public int oracleInvoiceCount() { return testMapper.oracleInvoiceCount(); } @Override @DS("mysql") public int mysqlInvoiceCount() { return testMapper.mysqlInvoiceCount(); } }TestServiceImpl