MongoDB Java基本操作
https://www.cnblogs.com/RealWorld/p/9209687.html
springboot2與mongodb整合
https://blog.csdn.net/p_programmer/article/details/89289067?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-7
springboot集成高版本mongodb3.11.2
https://blog.csdn.net/xjmfc/article/details/106929136?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-1
SpringBoot 2.2.0 SpringData MongoDB使用示例(一)
https://blog.csdn.net/pingpei1133/article/details/103104426?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-16.compare&depth_1-
utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-16.compare
mongodb實現多數據源
https://www.cnblogs.com/mczhou2/p/12713042.html
______________________________________________________________________________________________________________________________________________
mongodb實現多數據源
最近在開發過程中遇到一個需求,想要實現該需求需要把一個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多數據源就配置好了。這時候大家也許會問,前期准備工作好了,到底怎么用?你確定你想知道,那下回來我手把手教你。
