springboot整合MongoDb多数据源
1、引入包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> <version>2.7.3</version> </dependency>
2、application.yml 配置文件
3、封装
package com.sxsoft.config;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* @program: sxsoft_operation_admin
* @ClassName DataSourceConfig
* @description:monogodb多数据源配置 非关系型数据库
* @author: 黄涛
* @create: 2022-10-08 15:41
* @Version 1.0
**/
@Configuration
public class MongoDBConfig {
@Bean(name = "oneMongoProperties")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.qitamongodbserver")
public MongoProperties masterMongoProperties() {
return new MongoProperties();
}
@Bean(name = "twoMongoProperties")
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.shuilimongodbserver")
public MongoProperties twoMongoProperties() {
return new MongoProperties();
}
@Bean(name = "threeMongoProperties")
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.zhujianmongodbserver")
public MongoProperties threeMongoProperties() {
return new MongoProperties();
}
}
package com.sxsoft.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.mongodb.MongoDatabaseFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; /** * @program: sxsoft_operation_admin * @ClassName DefaultMongoTemplateConf * @description: 其他工程mogodb,系统默认的 * @author: 黄涛 * @create: 2023-07-21 16:25 * @Version 1.0 **/ @Configuration @EnableMongoRepositories(mongoTemplateRef = "mongoTemplate") public class DefaultMongoTemplateConfig { private final MongoProperties mongoProperties; public DefaultMongoTemplateConfig(@Qualifier("oneMongoProperties") MongoProperties mongoProperties) { this.mongoProperties = mongoProperties; } @Bean @Primary public MongoTemplate mongoTemplate() { return new MongoTemplate(mongoDatabaseFactory(mongoProperties)); } @Bean(name = "oneMongoFactory") @Primary public MongoDatabaseFactory mongoDatabaseFactory(MongoProperties mongoProperties) { return new SimpleMongoClientDatabaseFactory(mongoProperties.getUri()); } }
package com.sxsoft.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDatabaseFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; /** * @program: sxsoft_operation_admin * @ClassName TwoMongoTemplateConf * @description: 水利工程mongodb * @author: 黄涛 * @create: 2023-07-21 16:29 * @Version 1.0 **/ @Configuration @EnableMongoRepositories(mongoTemplateRef = "twoMongo") public class ShuiLiMongoTemplateConfig { private final MongoProperties mongoProperties; public ShuiLiMongoTemplateConfig(@Qualifier("twoMongoProperties") MongoProperties mongoProperties) { this.mongoProperties = mongoProperties; } @Bean(name = "twoMongo") public MongoTemplate mongoTemplate() { return new MongoTemplate(mongoDatabaseFactory(mongoProperties)); } @Bean(name = "twoMongoFactory") public MongoDatabaseFactory mongoDatabaseFactory(MongoProperties mongoProperties) { return new SimpleMongoClientDatabaseFactory(mongoProperties.getUri()); } }
package com.sxsoft.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDatabaseFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; /** * @program: sxsoft_operation_admin * @ClassName TwoMongoTemplateConf * @description: 住建mongodb * @author: 黄涛 * @create: 2023-07-21 16:29 * @Version 1.0 **/ @Configuration @EnableMongoRepositories(mongoTemplateRef = "threeMongo") public class ZhuJianMongoTemplateConfig { private final MongoProperties mongoProperties; public ZhuJianMongoTemplateConfig(@Qualifier("threeMongoProperties") MongoProperties mongoProperties) { this.mongoProperties = mongoProperties; } @Bean(name = "threeMongo") public MongoTemplate mongoTemplate() { return new MongoTemplate(mongoDatabaseFactory(mongoProperties)); } @Bean(name = "threeMongoFactory") public MongoDatabaseFactory mongoDatabaseFactory(MongoProperties mongoProperties) { return new SimpleMongoClientDatabaseFactory(mongoProperties.getUri()); } }
3、使用
1、使用默认的db
private MongoTemplate mongoTemplate;//系统默认的mogodb
@Autowired
public void setMongoTemplate(MongoTemplate mongoTemplate){
this.mongoTemplate = mongoTemplate;
}
2、使用第二个db
private MongoTemplate mongoTemplate;
@Autowired
@Qualifier("twoMongo")
public void setMongoTemplate(MongoTemplate mongoTemplate){
this.mongoTemplate = mongoTemplate;
}
2、使用第三个db
private MongoTemplate mongoTemplate;
@Autowired
@Qualifier("threeMongo")
public void setMongoTemplate(MongoTemplate mongoTemplate){
this.mongoTemplate = mongoTemplate;
}
吾乃代码搬运工,侵联删