這篇文章主要介紹springboot-MongoDB如何實(shí)現(xiàn)多數(shù)據(jù)源配置,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序設(shè)計(jì)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了鹽湖免費(fèi)建站歡迎大家使用!
在日常工作中,我們可能需要連接多個(gè)MongoDB數(shù)據(jù)源,比如用戶庫(kù)user,日志庫(kù)log。本章我們來(lái)記錄連接多個(gè)數(shù)據(jù)源的步驟,以兩個(gè)數(shù)據(jù)源為例,多個(gè)數(shù)據(jù)源類推。
1、pom.xml中引入mongodb的依賴
org.springframework.boot spring-boot-starter-data-mongodb org.projectlombok lombok org.springframework.boot spring-boot-autoconfigure RELEASE
Lombok - 是一個(gè)可以通過(guò)簡(jiǎn)單的注解形式來(lái)幫助我們簡(jiǎn)化消除一些必須有但顯得很臃腫的Java代碼的工具,通過(guò)使用對(duì)應(yīng)的注解,可以在編譯源碼的時(shí)候生成對(duì)應(yīng)的方法。簡(jiǎn)單試了以下這個(gè)工具還挺好玩的,加上注解我們就不用手動(dòng)寫(xiě) getter\setter、構(gòu)建方式類似的代碼了。
2, yml配置:
mongodb: primary: host: 192.168.9.60 port: 20000 database: test secondary: host: 192.168.9.60 port: 20000 database: test1
3, 配置2個(gè)庫(kù)的數(shù)據(jù)源:
@Data @ConfigurationProperties(prefix = "mongodb") public class MultipleMongoProperties { private MongoProperties primary = new MongoProperties(); private MongoProperties secondary = new MongoProperties(); }
3.1) 第一個(gè)庫(kù)封裝:
@Configuration @EnableMongoRepositories(basePackages = "com.neo.model.repository.primary", mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE) public class PrimaryMongoConfig { protected static final String MONGO_TEMPLATE = "primaryMongoTemplate"; }
3.2) 第二個(gè)庫(kù)封裝 :
@Configuration @EnableMongoRepositories(basePackages = "com.neo.model.repository.secondary", mongoTemplateRef = SecondaryMongoConfig.MONGO_TEMPLATE) public class SecondaryMongoConfig { protected static final String MONGO_TEMPLATE = "secondaryMongoTemplate"; }
3.3) mongoTemplate:
@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, 創(chuàng)建2個(gè)庫(kù)對(duì)應(yīng)的repository:
@Data @AllArgsConstructor @NoArgsConstructor @Document(collection = "first_mongo") public class PrimaryMongoObject { @Id private String id; private String value; @Override public String toString() { return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\'' + '}'; } }
public interface PrimaryRepository extends MongoRepository{ }
測(cè)試:
@RunWith(SpringRunner.class) @SpringBootTest public class MuliDatabaseTest { @Autowired private PrimaryRepository primaryRepository; @Autowired private SecondaryRepository secondaryRepository; @Test public void TestSave() { System.out.println("************************************************************"); System.out.println("測(cè)試開(kāi)始"); System.out.println("************************************************************"); this.primaryRepository .save(new PrimaryMongoObject(null, "第一個(gè)庫(kù)的對(duì)象")); this.secondaryRepository .save(new SecondaryMongoObject(null, "第二個(gè)庫(kù)的對(duì)象")); Listprimaries = this.primaryRepository.findAll(); for (PrimaryMongoObject primary : primaries) { System.out.println(primary.toString()); } List secondaries = this.secondaryRepository.findAll(); for (SecondaryMongoObject secondary : secondaries) { System.out.println(secondary.toString()); } System.out.println("************************************************************"); System.out.println("測(cè)試完成"); System.out.println("************************************************************"); } }
以上是“springboot-mongodb如何實(shí)現(xiàn)多數(shù)據(jù)源配置”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!