SpringBoot+Mybatis-Plusmysql多数据源+mongodb多数据源[备忘] 1. yml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapping/*.xml
global-config:
refresh-mapper:true
spring:
profiles:
active: dev
datasource:
dynamic:
primary: master
strict:false
datasource:
master:
driver-class-name: ine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://ip1:3306/sfdoc?serverTimezone=UTC
username: root
password:123456
mysql_local:
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://ip2:3306/test?serverTimezone=UTC
username: root
password:123456
# sqlserver
dkd_bk:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://ip3:1433; databaseName=IDCMon
username: sa
password:138********
data:
mongodb:
#xd ubuntu
primary:
uri: mongodb://10.42.3.20:27017/search
#xd Windows
secondary:
uri: mongodb://账号:密码@ip地址:27017/sf-yf?authSource=admin
#xd Windows
thirdly:
uri: mongodb://账号:密码@ip地址:27017/sf-zdcy?authSource=admin
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
server:
port:8001
# 没选⽤p6spy 请⾃⾏修改对应位置
2. 常量类
public static final String DS_MYSQL_LOCAL ="mysql_local";
public static final String DS_MONGODB_XD_UBUNTU ="mongodb_xd_ubuntu";
public static final String DS_SERVER_DKY_BK ="dkd_bk";
3. 实现类和Dao层增加@DS注解
@DS 可以注解在⽅法上或类上,同时存在就近原则 ⽅法上注解 优先于 类上注解。
@DS(“dsName”) dsName可以为组名也可以为具体某个库的名称
官⽹推荐: 只在service的类和⽅法上添加注解,不建议在mapper上添加注解。
@Component
@DS(DBConstants.DS_MYSQL_LOCAL)
public interface TestMapper extends BaseMapper<TestMongoDB>{
}
4. 控制层使⽤
@Autowired
private TestDBService testDBService ;
@ApiOperation(value ="测试mysql多数据源")
@GetMapping("getAll")
public Result getAll(){
List<TestDB> list = testDBService.list();
return Result.ok().data("data",list);
}
结果:
{
"success":true,
"code":200,
"message":"成功",
"data":{
"data":[
{
"id":1,
"name":"测试1",
"age":19,
"sex":"男"
},
{
"id":2,
"name":"测试2",
"age":24,
"sex":"⼥"mongodb和mysql结合
}
]
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。