All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jbatis.dds.solon.starter.config.MongoPlusConfiguration Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package org.jbatis.dds.solon.starter.config;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;

import org.jbatis.dds.kernel.cache.global.MongoClientCache;
import org.jbatis.dds.kernel.convert.CollectionNameConvert;
import org.jbatis.dds.kernel.execute.SqlExecute;
import org.jbatis.dds.kernel.interceptor.BaseInterceptor;
import org.jbatis.dds.kernel.mapper.MongoPlusMapMapper;
import org.jbatis.dds.kernel.toolkit.MongoCollectionUtils;
import org.jbatis.dds.kernel.toolkit.UrlJoint;
import org.jbatis.dds.solon.starter.property.MongoDBCollectionProperty;
import org.jbatis.dds.solon.starter.property.MongoDBConnectProperty;
import org.jbatis.dds.solon.starter.property.MongoDBLogProperty;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

import java.util.Optional;

/**
 *
 * 连接配置
 **/
@Configuration
public class MongoPlusConfiguration {

    private SqlExecute sqlExecute;

    private MongoClient mongoClient;

    public SqlExecute getSqlExecute() {
        return sqlExecute;
    }

    @Bean
    @Condition(onMissingBean = SqlExecute.class)
    public SqlExecute sqlExecute(@Inject("${jbatis-dds.data.mongodb}") MongoDBConnectProperty mongoDBConnectProperty,
                                 @Inject(value = "${jbatis-dds.configuration.collection}",required = false) MongoDBCollectionProperty mongoDBCollectionProperty) {
        if (this.sqlExecute != null) {
            return this.sqlExecute;
        }
        mongoDBCollectionProperty = Optional.ofNullable(mongoDBCollectionProperty).orElseGet(MongoDBCollectionProperty::new);
        SqlExecute sqlExecute = new SqlExecute();
        sqlExecute.setSlaveDataSources(mongoDBConnectProperty.getSlaveDataSource());
        sqlExecute.setBaseProperty(mongoDBConnectProperty);
        CollectionNameConvert collectionNameConvert =
                MongoCollectionUtils.build(mongoDBCollectionProperty.getMappingStrategy());
        sqlExecute.setCollectionNameConvert(collectionNameConvert);
        UrlJoint urlJoint = new UrlJoint(mongoDBConnectProperty);
        this.mongoClient = MongoClients.create(MongoClientSettings.builder()
                .applyConnectionString(new ConnectionString(urlJoint.jointMongoUrl())).addCommandListener(new BaseInterceptor()).build());
        MongoClientCache.mongoClient = this.mongoClient;
        sqlExecute.setMongoClient(this.mongoClient);
        this.sqlExecute = sqlExecute;
        return sqlExecute;
    }

    @Bean("mongo")
    @Condition(onMissingBean = MongoClient.class)
    public MongoClient mongo() {
        return this.mongoClient;
    }

    @Bean
    @Condition(onMissingBean = MongoPlusMapMapper.class)
    public MongoPlusMapMapper mongoPlusMapMapper(@Inject SqlExecute sqlExecute){
        return new MongoPlusMapMapper(sqlExecute);
    }

    @Bean
    public MongoPlusAutoConfiguration mongoPlusAutoConfiguration(@Inject SqlExecute sqlExecute,
                                                                 @Inject("${jbatis-dds}") MongoDBLogProperty mongoDBLogProperty,
                                                                 @Inject(value = "${jbatis-dds.configuration.collection}",required = false) MongoDBCollectionProperty mongoDBCollectionProperty){
        mongoDBCollectionProperty = Optional.ofNullable(mongoDBCollectionProperty).orElseGet(MongoDBCollectionProperty::new);
        return new MongoPlusAutoConfiguration(this.sqlExecute,mongoDBLogProperty,mongoDBCollectionProperty);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy