github:https://github.com/xiaozhuanfeng/mongoProj
現MongoDB有兩個數據庫:
pom.xml:
<!-- mongodb 配置 --> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
-
Lombok - 是一個可以通過簡單的注解形式來幫助我們簡化消除一些必須有但顯得很臃腫的Java代碼的工具,通過使用對應的注解,可以在編譯源碼的時候生成對應的方法。簡單試了以下這個工具還挺好玩的,加上注解我們就不用手動寫 getter\setter、構建方式類似的代碼了。
-
spring-boot-autoconfigure - 就是spring boot的自動化配置
注意這里Idea還可以設置Lombok插件:
https://blog.csdn.net/qq_37433657/article/details/83275051
配置文件:
#primary 數據源
mongodb.primary.host=192.168.0.102
mongodb.primary.port=27017
mongodb.primary.database=long
mongodb.secondary.host=192.168.0.102
mongodb.secondary.port=27017
mongodb.secondary.database=jianghu
1、配置兩個數據庫的數據源
package com.example.demo.config; import lombok.Data; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @Data @ConfigurationProperties(prefix = "mongodb") public class MultipleMongoProperties { private MongoProperties primary = new MongoProperties(); private MongoProperties secondary = new MongoProperties(); //安裝Lombok插件,@Data /* public MongoProperties getPrimary() { return primary; } public MongoProperties getSecondary() { return secondary; }*/ }
2、配置不同包路徑下使用不同的數據源
package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; @Configuration @EnableMongoRepositories(basePackages = "com.example.demo.repository.primary", mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE) public class PrimaryMongoConfig { protected static final String MONGO_TEMPLATE = "primaryMongoTemplate"; }
package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; @Configuration @EnableMongoRepositories(basePackages = "com.example.demo.repository.secondary", mongoTemplateRef = SecondaryMongoConfig.MONGO_TEMPLATE) public class SecondaryMongoConfig { protected static final String MONGO_TEMPLATE = "secondaryMongoTemplate"; }
3、讀取對應的配置信息並且構造對應的MongoTemplate
package com.example.demo.config; import com.mongodb.MongoClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.mongo.MongoProperties; 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.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; @Configuration public class MultipleMongoConfig { @Autowired private MultipleMongoProperties mongoProperties; @Primary @Bean(name = PrimaryMongoConfig.MONGO_TEMPLATE) public MongoTemplate primaryMongoTemplate() throws Exception { return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary())); } @Bean @Qualifier(SecondaryMongoConfig.MONGO_TEMPLATE) public MongoTemplate secondaryMongoTemplate() throws Exception { return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary())); } @Bean @Primary public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception { return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()), mongo.getDatabase()); } @Bean public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception { return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()), mongo.getDatabase()); } }
4、新增實體類
對應DB=long的col集合
package com.example.demo.dto; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.mongodb.core.mapping.Document; @Data @AllArgsConstructor @NoArgsConstructor @Document(collection = "col") public class Human { private String name; private int age; private String gender; @Override public String toString() { return "Human{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } }
對應DB=jianghu 的wuxia集合
package com.example.demo.dto; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.mongodb.core.mapping.Document; @Data @AllArgsConstructor @NoArgsConstructor @Document(collection = "wuxia") public class Swordsman { private String name; private String age; private String skill; @Override public String toString() { return "Swordsman{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", skill='" + skill + '\'' + '}'; } }
5、對應的Repository
package com.example.demo.repository.primary; import com.example.demo.dto.Human; import org.springframework.data.mongodb.repository.MongoRepository; public interface PrimaryRepository extends MongoRepository<Human, String> { }
package com.example.demo.repository.secondary; import com.example.demo.dto.Swordsman; import org.springframework.data.mongodb.repository.MongoRepository; public interface SecondaryRepository extends MongoRepository<Swordsman, String> { }
測試類:
package com.example.demo; import com.example.demo.dto.Human; import com.example.demo.dto.Swordsman; import com.example.demo.repository.primary.PrimaryRepository; import com.example.demo.repository.secondary.SecondaryRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class MultipleMongoTest { @Autowired private PrimaryRepository primaryRepository; @Autowired private SecondaryRepository secondaryRepository; @Test public void test1(){ List<Human> primaries = this.primaryRepository.findAll(); for (Human primary : primaries) { System.out.println(primary.toString()); } List<Swordsman> secondaries = this.secondaryRepository.findAll(); for (Swordsman secondary : secondaries) { System.out.println(secondary.toString()); } } }
測試結果: