springboot中配置连接多个mongodb数据库

springboot中配置连接多个mongodb数据库
application.properties 添加配置
#gamemongo

gamemongo.server.ip=192.168.0.100

gamemongo.server.port=27017

gamemongo.server.username=dbuser

gamemongo.server.password=123456

gamemongo.server.db=Game

#commonmongo

commonmongo.server.ip=192.168.0.100

commonmongo.server.port=27017

commonmongo.server.username=dbuser

commonmongo.server.password=123456

commonmongo.server.db=Common

增加两个配置文件CommonMongoConfig.java和GameMongoConfig.java
package com.server.mongo;



import com.mongodb.MongoClientURI;

import com.server.redis.RedisConfig;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;

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;

import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;

import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;

import org.springframework.data.mongodb.core.convert.MappingMongoConverter;

import org.springframework.data.mongodb.core.mapping.MongoMappingContext;



@Configuration

@AutoConfigureAfter(RedisConfig.class)

public class CommonMongoConfig {



    @Value("${server.jbId}")

    int jbId;

    @Value("${commonmongo.server.ip}")

    private String ip;

    @Value("${commonmongo.server.port}")

    private int port;

    @Value("${commonmongo.server.username}")

    private String username;

    @Value("${commonmongo.server.password}")

    private String password;

    @Value("${commonmongo.server.db}")

    private String db;



    @Bean

    @Primary

    public MongoProperties commonMongoProperties() {

        MongoProperties cfg = new MongoProperties();

        cfg.setUri(String.format("mongodb://%s:%s@%s:%s/%s", username, password, ip, port, db + jbId));

        return cfg;

    }



    @Primary

    @Bean(name = "commonMongoTemplate")

    public MongoTemplate commonMongoTemplate() throws Exception {

        MongoDbFactory factory = commonFactory(commonMongoProperties());

        MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), new MongoMappingContext());

        converter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return new MongoTemplate(factory, converter);

    }



    @Bean

    @Primary

    public MongoDbFactory commonFactory(MongoProperties mongoProperties) throws Exception {

        return new SimpleMongoDbFactory(new MongoClientURI(mongoProperties.getUri()));

    }

}

package com.server.mongo;



import com.mongodb.MongoClientURI;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.autoconfigure.mongo.MongoProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.mongodb.MongoDbFactory;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;

import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;

import org.springframework.data.mongodb.core.convert.MappingMongoConverter;

import org.springframework.data.mongodb.core.mapping.MongoMappingContext;



@Configuration

public class GameMongoConfig {



    @Value("${server.svId}")

    int svId;

    @Value("${gamemongo.server.ip}")

    private String ip;

    @Value("${gamemongo.server.port}")

    private int port;

    @Value("${gamemongo.server.username}")

    private String username;

    @Value("${gamemongo.server.password}")

    private String password;

    @Value("${gamemongo.server.db}")

    private String db;



    @Bean

    public MongoProperties gameMongoProperties() {

        MongoProperties cfg = new MongoProperties();

        cfg.setUri(String.format("mongodb://%s:%s@%s:%s/%s", username, password, ip, port, db + svId));

        return cfg;

    }



    @Bean(name = "gameMongoTemplate")

    public MongoTemplate gameMongoTemplate() throws Exception {

        MongoDbFactory factory = gameFactory(gameMongoProperties());

        MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), new MongoMappingContext());

        converter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return new MongoTemplate(factory, converter);

    }



    @Bean

    public MongoDbFactory gameFactory(MongoProperties mongoProperties) throws Exception {

        return new SimpleMongoDbFactory(new MongoClientURI(mongoProperties.getUri()));

    }

}

通过@Autowired注入@Qualifier指定使用数据源
   @Autowired

   @Qualifier(value = "commonMongoTemplate")

   public MongoTemplate template;

例如下面代码,不同的实体类要存在哪个库就注入哪个数据源就行了,就能达到不同数据存储在不同数据库的目的
package com.server.dao;



import com.mongodb.BasicDBObject;

import com.mongodb.DBObject;

import com.server.Const.DbConst;

import com.server.cache.CacheHelper;

import com.server.entity.CityState;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.data.domain.Sort;

import org.springframework.data.domain.Sort.Direction;

import org.springframework.data.domain.Sort.Order;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.BasicQuery;

import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import org.springframework.data.mongodb.core.query.Update;

import org.springframework.stereotype.Component;



import java.util.List;



@Component

public class CityStateDao {

   @Autowired

   @Qualifier(value = "commonMongoTemplate")

   public MongoTemplate template;



   public List<CityState> findAll() {

      return template.findAll(CityState.class);

   }



   public List<CityState> find(Query query) {

      return template.find(query, CityState.class);

   }



   public CityState findById(long id) {

      return template.findById(id, CityState.class);

   }



   public void insert(CityState entity) {

      template.insert(entity);

   }



   public void save(CityState entity) {

      template.save(entity);

   }

   

   public void update(long id, Update update) {

      Query query = new Query(Criteria.where("_id").is(id));

      template.upsert(query, update, CityState.class);

   }

   

   public void remove(long id) {

      Query query = new Query(Criteria.where("_id").is(id));

      template.remove(query, CityState.class);

   }

   

   public long count(Query query) {

      return template.count(query, CityState.class);

   }

}

Author:

发表评论

电子邮件地址不会被公开。 必填项已用*标注