Multi-data source configuration for Springboot-mongodb

Source: Internet
Author: User
Tags mongoclient

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.