MongoDB系列-- SpringBoot 中MongoDB多数据源配置

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

SpringBoot 中MongoDB多数据源配置

数据库

新建两个数据库 firstDB 、secondDB

并添加用户 login

application.properties

在 application.properties 文件中配置

spring.data.mongodb.first.host=localhost
spring.data.mongodb.first.port=27017
spring.data.mongodb.first.database=firstDB
spring.data.mongodb.first.username=login
spring.data.mongodb.first.password=123456
spring.data.mongodb.second.host=localhost
spring.data.mongodb.second.port=27017
spring.data.mongodb.second.database=secondDB
spring.data.mongodb.second.username=login
spring.data.mongodb.second.password=123456
配置多数据源
package com.tcwong.mongodb.config;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.springframework.beans.factory.annotation.Qualifier;
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;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class MongoTemplateConfig {

	[@Primary](https://my.oschina.net/primary)
	@Bean("firstMongoProperties")
	@ConfigurationProperties(prefix = "spring.data.mongodb.first")
	public MongoProperties firstMongoProperties() {
		return new MongoProperties();
	}

	@Bean("secondMongoProperties")
	@ConfigurationProperties(prefix = "spring.data.mongodb.second")
	public MongoProperties secondMongoProperties() {
		return new MongoProperties();
	}

	@Primary
	@Bean(value = "firstMongoTemplate")
	public MongoTemplate firstMongoTemplate(@Qualifier("firstMongoProperties") MongoProperties mongoProperties) {
		return new MongoTemplate(mongoDbFactory(mongoProperties));
	}

	@Bean(value = "secondMongoTemplate")
	public MongoTemplate secondMongoTemplate(@Qualifier("secondMongoProperties") MongoProperties mongoProperties) {
		return new MongoTemplate(mongoDbFactory(mongoProperties));
	}


	private MongoDbFactory mongoDbFactory(MongoProperties mongoProperties) {
		ServerAddress serverAddress = new ServerAddress(mongoProperties.getHost(), mongoProperties.getPort());
		List<MongoCredential> credentialList = new ArrayList<>();
		credentialList.add(MongoCredential.createScramSha1Credential(mongoProperties.getUsername()
				, mongoProperties.getDatabase(), mongoProperties.getPassword()));
		MongoClient mongoClient = new MongoClient(serverAddress,
				credentialList,
				new MongoClientOptions.Builder().build());
		return new SimpleMongoDbFactory(mongoClient, mongoProperties.getDatabase());
	}
}
测试
package com.tcwong.mongodb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class MongoTemplateController {

	@Autowired
	@Qualifier("firstMongoTemplate")
	private MongoTemplate firstMongoTemplate;

	@Autowired
	@Qualifier("secondMongoTemplate")
	private MongoTemplate secondMongoTemplate;

	@GetMapping("/firstMongoTemplate")
	public void firstTemplate() {
		Map<Object, Object> map = new HashMap<>();
		map.put("数据库", "firstMongoTemplate");
		firstMongoTemplate.insert(map, "firstDB");
	}

	@GetMapping("/secondMongoTemplate")
	public void secondTemplate() {
		Map<Object, Object> map = new HashMap<>();
		map.put("数据库", "secondMongoTemplate");
		secondMongoTemplate.insert(map, "secondDB");
	}


}
效果

firstDB 数据库

secondDB 数据库

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看