最近在开发过程中遇到一个需求,想要实现该需求需要把一个mongodb库里面的表&数据复制到另一个mongodb库。首先需要做的一件事就是配置多数据源,由于项目采用SpringBoot,所以配置多数据源也是在这个框架基础上的。话不多说,直接上代码
首先pom.xml需要引入如下配置
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-configuration-processor</artifactId> 4 <optional>true</optional> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-data-mongodb</artifactId> 9 </dependency>
分别定义三个类AbstractMongoConfig、PrimaryMongoConfig、SecondMongoConfig
1 import com.mongodb.MongoClientURI; 2 import org.springframework.data.mongodb.MongoDbFactory; 3 import org.springframework.data.mongodb.core.MongoTemplate; 4 import org.springframework.data.mongodb.core.SimpleMongoDbFactory; 5 6 /** 7 * 描述 8 * 9 * @author mczhou2 2020/3/2010:50 10 */ 11 public abstract class AbstractMongoConfig { 12 //连接MongoDB地址 13 private String uri; 14 /** 15 * 获取mongoDBTemplate对象 16 */ 17 public abstract MongoTemplate getMongoTemplate() throws Exception; 18 /** 19 * 创建mongoDb工厂 20 */ 21 public MongoDbFactory mongoDbFactory() throws Exception { 22 MongoClientURI mongoclienturi = new MongoClientURI(uri); 23 return new SimpleMongoDbFactory(mongoclienturi); 24 } 25 public String getUri() { 26 return uri; 27 } 28 29 public void setUri(String uri) { 30 this.uri = uri; 31 } 32 }
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; import org.springframework.data.mongodb.core.MongoTemplate; /** * 描述 * * @author mczhou2 2020/3/20 10:51 */ @Configuration @ConfigurationProperties(prefix = "spring.data.mongodb.primary") public class PrimaryMongoConfig extends AbstractMongoConfig{ @Override @Primary @Bean(name = "primaryMongoTemplate") public MongoTemplate getMongoTemplate() throws Exception { return new MongoTemplate((mongoDbFactory())); } }
1 import org.springframework.boot.context.properties.ConfigurationProperties; 2 import org.springframework.context.annotation.Bean; 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.context.annotation.Primary; 5 import org.springframework.data.mongodb.core.MongoTemplate; 6 7 /** 8 * 描述 9 * 10 * @author mczhou2 2020/3/2010:51 11 */ 12 @Configuration 13 @ConfigurationProperties(prefix = "spring.data.mongodb.second") 14 public class SecondaryMongoConfig extends AbstractMongoConfig{ 15 16 @Override 17 @Primary 18 @Bean(name = "secondMongoTemplate") 19 public MongoTemplate getMongoTemplate() throws Exception { 20 return new MongoTemplate((mongoDbFactory())); 21 } 22 }
接下来启动类的注解需要屏蔽之前的类,否在项目启动报错
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; 4 import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; 5 6 @SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) 7 public class DemoApplication { 8 public static void main(String[] args) { 9 SpringApplication.run(DemoApplication.class,args); 10 } 11 }
至此,mongodb多数据源就配置好了。这时候大家也许会问,前期准备工作好了,到底怎么用?你确定你想知道,那下回来我手把手教你。