spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: xxx
password: xxx
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
#Oracle需要打开注释
#validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
stat-view-servlet:
enabled: true
url-pattern: /druid/*
#login-username: admin
#login-password: admin
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: false
wall:
config:
multi-statement-allow: true
##多数据源的配置
dynamic:
datasource:
sqlserver:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://192.168.0.xx:1433;DatabaseName=xx;allowMultiQueries=true
username: xx
password: xxx
sqlserver2:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://192.168.0.xx:1433;DatabaseName=xxx;allowMultiQueries=true
username: xx
password: xxx
sqlserver3:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://192.168.0.xx:1433;DatabaseName=xx;allowMultiQueries=true
username: xx
password: xxx
使用:
在service中加上对应的数据库注解
package io.renren.modules.finance.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.finance.dao.OvertimeDao;
import io.renren.modules.finance.entity.OvertimeEntity;
import io.renren.modules.finance.entity.YueDuJlKkMxEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class FinanceService extends ServiceImpl<OvertimeDao, OvertimeEntity> {
@Autowired
private OvertimeDao overtimeDao;
@DataSource("sqlserver")
public List<OvertimeEntity> getOvertime(Map<String, Object> map){
List<OvertimeEntity> maps = overtimeDao.getOvertime(map);
return maps;
}
@DataSource("sqlserver2")
public IPage<OvertimeEntity> getOvertime(IPage<OvertimeEntity> page, Map<String, Object> params) {
return overtimeDao.getOvertime(page, params);
}
@DataSource("sqlserver3")
public List<Map<String, Object>> getRePort(Map<String, Object> map){
List<Map<String, Object>> list = baseMapper.getRePort(map);
return list;
}
}