Maven
1
2
3
4
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
|
Multiple Data Source Configuration
Configuration file.
1
2
3
4
5
6
7
8
|
spring:
data:
mongodb:
uri: mongodb://192.168.150.154:17017
database: ewell-label
mongodb-target:
uri: mongodb://192.168.150.154:17017
database: ewell-label-target
|
java configuration
Master data source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package com.winterchen.label.service.configuration;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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.MongoTransactionManager;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
/**
* @author winterchen
* @version 1.0
* @date 2020/12/2 3:51 下午
* @description 业务mongo数据源
**/
@Configuration
public class BusinessMongoConfig extends AbstractMongoConfiguration {
@Value("${spring.data.mongodb.uri}")
private String uri;
@Value("${spring.data.mongodb.database}")
private String database;
@Override
protected String getDatabaseName() {
return database;
}
@Override
public MongoClient mongoClient() {
MongoClientURI mongoClientURI = new MongoClientURI(uri);
return new MongoClient(mongoClientURI);
}
@Primary
@Bean("mongoMappingContext")
public MongoMappingContext mongoMappingContext() {
MongoMappingContext mappingContext = new MongoMappingContext();
return mappingContext;
}
@Primary
@Bean
public MongoTransactionManager transactionManager(@Qualifier("mongoDbFactory") MongoDbFactory mongoDbFactory) throws Exception {
return new MongoTransactionManager(mongoDbFactory);
}
@Primary
@Bean("mongoDbFactory")
public MongoDbFactory mongoDbFactory() {
return new SimpleMongoDbFactory(mongoClient(), getDatabaseName());
}
@Primary
@Bean("mappingMongoConverter") //使用自定义的typeMapper去除写入mongodb时的“_class”字段
public MappingMongoConverter mappingMongoConverter(@Qualifier("mongoDbFactory") MongoDbFactory mongoDbFactory,
@Qualifier("mongoMappingContext") MongoMappingContext mongoMappingContext) throws Exception {
DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
return converter;
}
@Primary
@Bean(name = "mongoTemplate")
public MongoTemplate getMongoTemplate(@Qualifier("mongoDbFactory") MongoDbFactory mongoDbFactory,
@Qualifier("mappingMongoConverter") MappingMongoConverter mappingMongoConverter) throws Exception {
return new MongoTemplate(mongoDbFactory, mappingMongoConverter);
}
}
|
Secondary Data Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package com.winterchen.label.service.configuration;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
/**
* @author winterchen
* @version 1.0
* @date 2020/12/2 3:53 下午
* @description TODO
**/
@Configuration
public class TargetMongoConfig extends AbstractMongoConfiguration {
@Value("${spring.data.mongodb-target.uri}")
private String uri;
@Value("${spring.data.mongodb-target.database}")
private String database;
@Override
protected String getDatabaseName() {
return database;
}
@Override
public MongoClient mongoClient() {
MongoClientURI mongoClientURI = new MongoClientURI(uri);
return new MongoClient(mongoClientURI);
}
@Bean("targetMongoMappingContext")
public MongoMappingContext mongoMappingContext() {
MongoMappingContext mappingContext = new MongoMappingContext();
return mappingContext;
}
@Bean("TARGET_MONGO_TRANSACTION_MANAGER")
public MongoTransactionManager transactionManager(@Qualifier("targetMongoDbFactory") MongoDbFactory mongoDbFactory) throws Exception {
return new MongoTransactionManager(mongoDbFactory);
}
@Bean("targetMongoDbFactory")
public MongoDbFactory mongoDbFactory() {
return new SimpleMongoDbFactory(mongoClient(), getDatabaseName());
}
@Bean("targetMappingMongoConverter") //使用自定义的typeMapper去除写入mongodb时的“_class”字段
public MappingMongoConverter mappingMongoConverter(@Qualifier("targetMongoDbFactory") MongoDbFactory mongoDbFactory,
@Qualifier("targetMongoMappingContext") MongoMappingContext mongoMappingContext) throws Exception {
DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
return converter;
}
/**
* MongoTemplate实现
*/
@Bean(name = "targetMongoTemplate")
public MongoTemplate getMongoTemplate(@Qualifier("targetMongoDbFactory") MongoDbFactory mongoDbFactory,
@Qualifier("mappingMongoConverter") MappingMongoConverter mappingMongoConverter) throws Exception {
return new MongoTemplate(mongoDbFactory, mappingMongoConverter);
}
}
|
Usage
Using the default data source.
1
2
|
@Autowired
private MongoTemplate mongoTemplate;
|
To use another data source, just set the name:
1
2
3
|
@Autowired
@Qualifier("targetMongoTemplate")
private MongoTemplate targetMongoTemplate;
|
Transaction
General.
1
|
@Transactional(rollbackFor = Throwable.class)
|
For other data sources, just specify the transaction manager.
1
|
@Transactional(rollbackFor = Throwable.class, transactionManager = "TARGET_MONGO_TRANSACTION_MANAGER")
|
Note: The two transactions cannot be mixed
Reference https://blog.winterchen.com/2020/12/28/2020-12-28-mongodb-mutil-source/