Introduction of MongoDB dependency in Pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-data-mongodb</artifactid></dependency><dependency> <groupId> Org.projectlombok</groupid> <artifactId>lombok</artifactId></dependency>< dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-autoconfigure</artifactid> <version>RELEASE</version></dependency>
Lombok-is a simple form of annotations that can help us simplify the elimination of some of the necessary but very bloated Java code tools, by using the corresponding annotations, you can compile the source code when the corresponding method is generated. It's fun to just try the following tool, and with annotations we don't have to write Getter\setter and build similar code manually.
2, YML configuration:
MongoDB: primary: 192.168. 9.6020000 database:test secondary: 192.168. 9.60 20000 database:test1
3, configure the data source for 2 libraries:
" MongoDB " )publicclass multiplemongoproperties { privateNew mongoproperties (); Private New mongoproperties ();}
3.1) First Library package:
" com.neo.model.repository.primary " , = primarymongoconfig.mongo_template)publicclass primarymongoconfig { protected Static " primarymongotemplate " ;}
3.2) A second library package:
" com.neo.model.repository.secondary " , = secondarymongoconfig.mongo_template)publicclass secondarymongoconfig { protectedstatic"secondarymongotemplate";}
3.3) Mongotemplate:
@Configuration Public classMultiplemongoconfig {@AutowiredPrivatemultiplemongoproperties mongoproperties; @Primary @Bean (Name=primarymongoconfig.mongo_template) Publicmongotemplate primarymongotemplate () throws Exception {return NewMongotemplate (Primaryfactory ( This. Mongoproperties.getprimary ())); } @Bean @Qualifier (secondarymongoconfig.mongo_template) Publicmongotemplate secondarymongotemplate () throws Exception {return NewMongotemplate (Secondaryfactory ( This. Mongoproperties.getsecondary ())); } @Bean @Primary Publicmongodbfactory primaryfactory (mongoproperties Mongo) throws Exception {return NewSimplemongodbfactory (Newmongoclient (Mongo.gethost (), Mongo.getport ()), mongo.getdatabase ()); } @Bean Publicmongodbfactory secondaryfactory (mongoproperties Mongo) throws Exception {return NewSimplemongodbfactory (Newmongoclient (Mongo.gethost (), Mongo.getport ()), mongo.getdatabase ()); }}
4, create the corresponding repository for 2 libraries:
@Data @allargsconstructor@noargsconstructor@document (collection="First_mongo") Public classPrimarymongoobject {@IdPrivateString ID; PrivateString value; @Override PublicString toString () {return "primarymongoobject{"+"id= '"+ ID +'\ ''+", value= '"+ Value +'\ ''+'}'; }}
Public Interface Primaryrepository extends Mongorepository<primarymongoobject, string> {}
Test:
@RunWith (Springrunner.class) @SpringBootTest Public classmulidatabasetest {@AutowiredPrivateprimaryrepository primaryrepository; @AutowiredPrivatesecondaryrepository secondaryrepository; @Test Public voidTestsave () {System. out. println ("************************************************************"); System. out. println ("Test Start"); System. out. println ("************************************************************"); This. Primaryrepository. Save (NewPrimarymongoobject (NULL,"object of the first library")); This. Secondaryrepository. Save (NewSecondarymongoobject (NULL,"object of the second library")); List<PrimaryMongoObject> primaries = This. Primaryrepository.findall (); for(Primarymongoobject primary:primaries) {System. out. println (Primary.tostring ()); } List<SecondaryMongoObject> secondaries = This. Secondaryrepository.findall (); for(Secondarymongoobject secondary:secondaries) {System. out. println (Secondary.tostring ()); } System. out. println ("************************************************************"); System. out. println ("Test Complete"); System. out. println ("************************************************************"); }}
Multi-data source configuration for Springboot-mongodb