Springboot整合MongoDB多数据源配置

引入依赖

        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        

配置文件

spring:   
    data:
        mongodb:
          # 第一个数据源
          primary:
            uri: mongodb://ip:port/TJData_copy1
          # 第二个数据源
          second:
            uri: mongodb://ip:port/legal

配置类

获取配置参数MongoProperties

创建MongoDatabaseFactory

创建MongoTemplate(分别选用不同的Factory)

创建GridFsTemplate(选择指定的MongoDatabaseFactory)

package org.jeecg.modules.conf;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;

/**
 * Mongodb配置文件
 *
 * @className: MongoDBConfiguration
 * @author: lianglong
 * @create: 2023-03-23 09:29
 */
@Configuration
public class MongoDBConfiguration {
//    public static final String PRIMARY_TEMPLATE = "mongoTemplate";
//
//    public static final String SECONDARY_TEMPLATE = "secondMongoTemplate";

    /**
     * 第一数据源配置信息
     *
     * @return
     */
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.data.mongodb.primary")
    public MongoProperties primaryMongoProperties() {
        return new MongoProperties();
    }

    /**
     * 第二数据源配置信息
     *
     * @return
     */
    @Bean("secondMongoProperties")
    @ConfigurationProperties(prefix = "spring.data.mongodb.second")
    public MongoProperties secondMongoProperties() {
        return new MongoProperties();
    }

    /**
     * 默认 MongoTemplate
     *
     * @return
     */
    @Bean(name = PrimaryMongoConfig.MONGO_TEMPLATE)
    @Primary
    @Qualifier(PrimaryMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate primaryMongoTemplate() {
        MongoTemplate mongoTemplate = new MongoTemplate(primaryMongoFactory(primaryMongoProperties()));
        MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
        mongoMapping.afterPropertiesSet();
        return mongoTemplate;
    }

    @Bean(name = SecondMongoConfig.MONGO_TEMPLATE)
    @Qualifier(SecondMongoConfig.MONGO_TEMPLATE)
    public MongoTemplate secondMongoTemplate() {
        MongoProperties mongoProperties = secondMongoProperties();
        MongoDatabaseFactory mongoDatabaseFactory = secondMongoFactory(mongoProperties);
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDatabaseFactory);
        MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
        mongoMapping.afterPropertiesSet();
        return mongoTemplate;
    }

    @Primary
    @Bean(name="primaryGridFsTemplate")
    public GridFsTemplate getGridFsTemplate() {
        MongoProperties mongoProperties = secondMongoProperties();
        MongoDatabaseFactory mongoDatabaseFactory = secondMongoFactory(mongoProperties);
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDatabaseFactory);
        MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
        return new GridFsTemplate(mongoDatabaseFactory,mongoMapping);
    }


    /**
     * 默认 MongoDatabaseFactory
     *
     * @param mongoProperties
     * @return
     */
    @Bean
    @Primary
    public MongoDatabaseFactory primaryMongoFactory(MongoProperties mongoProperties) {
        return new SimpleMongoClientDatabaseFactory(mongoProperties.getUri());
    }

    @Bean
    public MongoDatabaseFactory secondMongoFactory(MongoProperties mongoProperties) {
        return new SimpleMongoClientDatabaseFactory(mongoProperties.getUri());
    }

}

设置Repository与MongoTemplate的关联

basePackages指定关联的repository所在包,所以这里建议两个repository在不同的包下。

/**
 * @className: PrimaryMongoConfiguration
 * @author: lianglong
 * @create: 2023-03-23 11:43
 */


@Configuration
@EnableMongoRepositories(basePackages = "org.jeecg.modules.repository.primary",mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE)
public class PrimaryMongoConfig {
    public static final String MONGO_TEMPLATE = "primaryMongoTemplate";

}
@Configuration
@EnableMongoRepositories(basePackages = "org.jeecg.modules.repository.second",mongoTemplateRef = SecondMongoConfig.MONGO_TEMPLATE)
public class SecondMongoConfig {
    public static final String MONGO_TEMPLATE = "secondMongoTemplate";

}

在primary包下:

public interface PrimaryMongoRepository extends MongoRepository {


}

在second包下:

package org.jeecg.modules.repository.second;

import org.jeecg.modules.admin.entity.LegalBaseMongo;
import org.springframework.data.mongodb.repository.MongoRepository;

/**
 * @className: SecondMongoMapper
 * @author: lianglong
 * @create: 2023-03-23 09:53
 */
public interface SecondMongoRepository extends MongoRepository {


}

使用时需要注意:

  
    @Autowired
    private SecondMongoRepository secondMongoRepository;
    @Autowired
    private GridFsTemplate gridfsTemplate;
    @Resource
    private MongoDatabaseFactory secondMongoFactory;

使用MongoTemplate可以使用@Autowired直接注入, 使用GridFsTemplate如果由两个以上需要注意名称,可以参考MongoDatabaseFactory,这里使用@Resource注解,可以按照名称注入。

你可能感兴趣的:(MongoDB,mongodb,java,数据库)