• Spring Boot多数据源配置(二)MongoDB


    Spring Boot多数据源配置(一)durid、mysql、jpa 整合中已经讲过了Spring Boot如何配置mysql多数据源。本篇文章讲一下Spring Boot如何配置mongoDB多数据源。

    配置文件

    spring:
    #mongo配置
      data:
        mongodb:
          statis:
            database: kxlist_statis
            uri: 192.168.1.115:27017
          list:
            database: kxlist_list
            uri: 192.168.1.115:27017
    
    

    JAVA文件:

    • 总的配置:
    @Configuration
    public class MultipleMongoProperties {
        @Bean(name="statisMongoProperties")
        @Primary
        @ConfigurationProperties(prefix="spring.data.mongodb.statis")
        public MongoProperties statisMongoProperties() {
            System.out.println("-------------------- statisMongoProperties init ---------------------");
            return new MongoProperties();
        }
    
        @Bean(name="listMongoProperties")
        @ConfigurationProperties(prefix="spring.data.mongodb.list")
        public MongoProperties listMongoProperties() {
            System.out.println("-------------------- listMongoProperties init ---------------------");
            return new MongoProperties();
        }
    
    }
    
    • statis数据源配置
    @Configuration
    @EnableMongoRepositories(basePackages = "com.kxlist.statistics.domain.statis", mongoTemplateRef = "statisMongo")
    public class StatisMongoMongoTemplate {
        @Autowired
        @Qualifier("statisMongoProperties")
        private MongoProperties mongoProperties;
    
        @Primary
        @Bean(name = "statisMongo")
        public MongoTemplate statisMongoTemplate() throws Exception {
            return new MongoTemplate(statisFactory(this.mongoProperties));
        }
    
        @Bean
        @Primary
        public MongoDbFactory statisFactory(MongoProperties mongoProperties) throws Exception {
    
            ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());
    
            return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());
    
        }
    }
    
    • list数据源配置
      和statis的配置很相似
    @Configuration
    @EnableMongoRepositories(basePackages = "com.kxlist.statistics.domain.list", mongoTemplateRef = "listMongo")
    public class ListMongoTemplate {
        @Autowired
        @Qualifier("listMongoProperties")
        private MongoProperties mongoProperties;
    
        @Bean(name = "listMongo")
        public MongoTemplate listTemplate() throws Exception {
            return new MongoTemplate(listFactory(this.mongoProperties));
        }
    
        @Bean
        public MongoDbFactory listFactory(MongoProperties mongoProperties) throws Exception {
    
            ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());
    
            return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());
    
        }
    }
    

    在相应的包下建立实体类和Repository就可以了。

    首发地址

    http://www.devzxd.top/2017/06/06/springboot-datasource-mongo.html

  • 相关阅读:
    curl发送post请求,统计响应时间
    云集微店、拼多多等顽疾凸显,社交电商如何突围?
    App音频内录 录音
    nginx支持android、ios、微信扫一扫
    hadoop 2.7.1安装和配置
    Centos7上HBase的安装和配置
    HBase各版本对Hadoop版本的支持情况
    40个Java多线程问题总结
    JAVA多线程之volatile 与 synchronized 的比较
    深入解析spring中用到的九种设计模式
  • 原文地址:https://www.cnblogs.com/devzxd/p/springboot-datasource-mongo.html
Copyright © 2020-2023  润新知