我有一个mongodb springboot应用程序连接到2个不同的数据库,具有相同的集合名称和数据库名称,但不同的uri。
我的application.properties
spring.data.mongodb.uri = uri
spring.data.mongodb.secondDB.uri = uri
spring.data.mongodb.database = database_name
spring.data.mongodb.secondDB.database = database_name
我的AppConfiguration文件
@Configuration
public class MultipleMongoConfig {
@Primary
@Bean(name = "newdb1Properties")
@ConfigurationProperties(prefix = "spring.data.mongodb")
public MongoProperties getNewDb1Props() throws Exception {
return new MongoProperties();
}
@Bean(name = "newdb2Properties")
@ConfigurationProperties(prefix = "spring.data.mongodb.secondDB")
public MongoProperties getNewDb2Props() throws Exception {
return new MongoProperties();
}
@Primary
@Bean(name = "newdb1MongoTemplate")
public MongoTemplate newdb1MongoTemplate() throws Exception {
return new MongoTemplate(newdb1MongoDatabaseFactory(getNewDb1Props()));
}
@Bean(name ="newdb2MongoTemplate")
public MongoTemplate newdb2MongoTemplate() throws Exception {
return new MongoTemplate(newdb2MongoDatabaseFactory(getNewDb2Props()));
}
@Primary
@Bean
public MongoDatabaseFactory newdb1MongoDatabaseFactory(MongoProperties mongo) throws Exception {
return new SimpleMongoClientDatabaseFactory(
mongo.getUri()
);
}
@Bean
public MongoDatabaseFactory newdb2MongoDatabaseFactory(MongoProperties mongo) throws Exception {
return new SimpleMongoClientDatabaseFactory(
mongo.getUri()
);
}
然后为每个数据源设置配置文件
@Configuration
@EnableMongoRepositories(basePackages = {"com.example.app.firstDatabse.Repository"},
mongoTemplateRef = "newdb1MongoTemplate"
)
public class NewDb1Config {
}
和
@Configuration
@EnableMongoRepositories(basePackages = {"com.example.app.secondDatabse.Repository"},
mongoTemplateRef = "newdb2MongoTemplate"
)
public class NewDb1Config {
}
对于模型,我有以下
@AllArgsConstructor
@NoArgsConstructor
@ToSting
@Document(collection = "coll")
public class FirstModel{
@Id
public String id;
@Field("f_name")
public String firstName;
@Field("l_name")
public String lastName;
@Field("age")
public int age;
@Field("gender")
public String gender;
}
我的第二个模型是相同的
@AllArgsConstructor
@NoArgsConstructor
@ToSting
@Document(collection = "coll")
public class SecondModel{
@Id
public String id;
@Field("f_name")
public String firstName;
@Field("l_name")
public String lastName;
@Field("age")
public int age;
@Field("gender")
public String gender;
}
我的控制器
@ResController
@RequestMapping("/controller")
public class Controller{
@Autowired
private FirstDataabseRepository repo;
@Autowired
private SecondDataabseRepository repo;
@Resource
private MongoTemplate mongoTemplate;
@RequestMapping("/findByName")
public List<SecondModel> findByName(){
Criteria criteria = new Criteria();
criteria = Criteria.where("f_name").is("John");
Query q = new Query(criteria);
List<SecondModel> results = mongoTemplate.find(q,SecondModel.class);
return results;
}
}
所以结果显示的是第一个集合的结果而不是第二个集合的结果。我需要为mongoTemplate做什么来查询第二个集合而不是第一个。
解决方案我必须添加一个限定符,并有一个蒙古模板为每个集合
@ResController
@RequestMapping("/controller")
public class Controller{
@Autowired
private FirstDataabseRepository repo;
@Autowired
private SecondDataabseRepository repo;
@Resource
@Qualifier(value="newdb1MongoTemplate")
private MongoTemplate mongoTemplate;
@Resource
@Qualifier(value="newdb2MongoTemplate")
private MongoTemplate mTemplate;
@RequestMapping("/findByName")
public List<SecondModel> findByName(){
Criteria criteria = new Criteria();
criteria = Criteria.where("f_name").is("John");
Query q = new Query(criteria);
List<SecondModel> results = mTemplate.find(q,SecondModel.class);
return results;
}
}