In daily work, we operate Mongodb database through Spring Data Mongodb. In Spring Boot, we only need to introduce spring-boot-starter-data-mongodb.

Then configure the connection information as follows:

spring.data.mongodb.uri=mongodb://localhost:27017/test
Copy the code

or

pring.data.mongodb.authentication-database= # Authentication database name.
spring.data.mongodb.database=test # Database name.
spring.data.mongodb.host=localhost # Mongo server host.
spring.data.mongodb.password= # Login password of the mongo server.
spring.data.mongodb.port=27017 # Mongo server port.
spring.data.mongodb.username= # Login user of the mongo server.
Copy the code

Spring-boot-starter-data-mongodb provides two configuration modes, uri and host. Uris can be configured with multiple addresses, which is how clusters are configured. Host can be connected to only one node.

When a project needs to connect to multiple databases, the automatic configuration of spring-boot-starter-data-mongodb cannot meet the requirements. Therefore, I have encapsulated a multi-data source mongodb spring-boot-starter.

I wrote a previous article on the use of multiple data sources: spring-boot-starter-mongodb-pool

Since this is the first release, there are some things that are not taken into account. We recently updated it, added some content, and upgraded it to Spring Boot 2.0.

Github:github.com/yinjihuan/s…

First of all, a brief introduction to how to use:

Configure the warehouse address:

<repositories>
	<repository>
		<id>jitpack.io</id>
		<url>https://www.jitpack.io</url>
	</repository>
</repositories>
Copy the code

Configure the latest version. Only Spring Boot 2.0 is supported

<dependency> <groupId>com.github.yinjihuan</groupId> <artifactId>spring-boot-starter-mongodb-pool</artifactId> The < version > 2.0.2 < / version > < / dependency >Copy the code

My configuration methods are also URI and host. For URI, only mongo node information is configured, which is different from the default URI format and does not contain user information and connection parameters.

URI Configuration cluster

spring.data.mongodb.testMongoTemplate.uri=localhost:27017,localhost:27018
spring.data.mongodb.testMongoTemplate.username=yinjihuan
spring.data.mongodb.testMongoTemplate.password=123456
spring.data.mongodb.testMongoTemplate.database=test
spring.data.mongodb.testMongoTemplate.authenticationDatabase=admin
Copy the code

HOST Configuration

spring.data.mongodb.testMongoTemplate.host=localhost
spring.data.mongodb.testMongoTemplate.port=27017
spring.data.mongodb.testMongoTemplate.database=test
spring.data.mongodb.testMongoTemplate.username=yinjihuan
spring.data.mongodb.testMongoTemplate.password=123456
Copy the code

TestMongoTemplate is the MongoTemplate object we use to manipulate the test database. The framework will create it for you automatically, just inject it and use it.

@Autowired
@Qualifier("testMongoTemplate")
private MongoTemplate testMongoTemplate;
Copy the code

MongoTemplate: MongoTemplate: MongoTemplate: MongoTemplate: MongoTemplate: MongoTemplate

spring.data.mongodb.testMongoTemplate.host=localhost
spring.data.mongodb.testMongoTemplate.port=27017
spring.data.mongodb.testMongoTemplate.database=test
spring.data.mongodb.testMongoTemplate.username=yinjihuan
spring.data.mongodb.testMongoTemplate.password=123456

spring.data.mongodb.test2MongoTemplate.host=localhost
spring.data.mongodb.test2MongoTemplate.port=27017
spring.data.mongodb.test2MongoTemplate.database=test2
spring.data.mongodb.test2MongoTemplate.username=yinjihuan
spring.data.mongodb.test2MongoTemplate.password=123456
Copy the code

Inject the same object as the database you operate on:

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

@Autowired
@Qualifier("test2MongoTemplate")
private MongoTemplate test2MongoTemplate;
Copy the code

Updated instructions

  • Spring Boot 2.x Version 1.0.0 only supports Spring Boot 1.x
  • Add user authentication configuration The first version 1.0.0 did not add account and password authentication logic
  • Cluster ADDRESS Configuration Cluster information can be configured using the URI

For framework implementation details, go to cxytiandi.com/course