1.配置pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.1</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</exclusion>
</exclusions>
</dependency>
2.配置文件:application-local.yml
spring:
datasource:
dynamic:
primary: db1 #设置默认的数据源或者数据源组,默认值即为master
strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源.
datasource:
db1:
url: jdbc:oracle:thin:@192.168.xxx.xxx:1521:orcl
username: test
password: test
driver-class-name: oracle.jdbc.driver.OracleDriver # 3.2.0开始支持SPI可省略此配置
db2:
url: jdbc:oracle:thin:@192.168.xxx.xxx:1521:orcl
username: HIS_ENCOUNTER
password: HIS_ENCOUNTER
driver-class-name: oracle.jdbc.driver.OracleDriver # 3.2.0开始支持SPI可省略此配置
type: com.zaxxer.hikari.HikariDataSource
hikari:
maximum-pool-size: 20
# minimum-idle: 5
auto-commit: true
pool-name: YKYHikariCP
max-lifetime: 1800000
connection-timeout: 10000
connection-test-query: SELECT 1 FROM DUAL
idle-timeout: 30000
mybatis-plus:
mapper-locations: classpath:/com/echovin/**/mapper/*.xml
3.使用@DS注解
@DS("db1")
@Service
public class ConcreteCachingAuthorizationService extends CachingAuthorizationService {
@Autowired
private UserDao userDao;
@Override
protected AuthenticationToken getAuthorizationInternal(String userCode) {
List<UserPermissonEntity> permissions = userDao.queryUserPermission(userCode);
if(permissions == null || permissions.isEmpty()) return null;
AuthenticationToken token = new AuthenticationToken();
token.setId(permissions.get(0).getId());
token.setPrincipal(userCode);
token.setCredentials(permissions.get(0).getPassword());
token.setStaffId(permissions.get(0).getStaffId());
token.setAuthorities(permissions.get(0).getPermission());
return token;
}
}