mongdb同一连接,多数据库配置

 1.配置类,此处为两个数据库列子,使用注解@inject获取注入的两个mongdatastoreone和mongdatastoretwo实列就可以操作相应数据库了

package com.qjxs.config;
import java.util.Arrays;
import java.util.List;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.collect.Lists;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.qjxs.domain.User;

/**
 * 更多mongdb数据库
 * @author cf
 *
 */
@Configuration
public class MoreMongdb {
	
	private Morphia morphia;
	private Datastore ds;

	
	@Bean(name = "morphia")
	public Morphia morphia() {
		morphia = new Morphia();
		// morphia.mapPackage("com.uvwxyz.intv.vo", true);
		//mapPackage("cn.xyz.mianshi.vo", morphia);
		
		morphia.mapPackage("com.qjxs.domain");
		return morphia;
	}

	@Bean(name = "mongdatastoreone")
	public Datastore mongdatastoreone(MongoClient mongoClient) {
		ds = morphia().createDatastore(mongoClient, "mytaskone");
		ds.ensureIndexes();
		ds.ensureCaps();
		

		return ds;
	}

    @Bean(name = "mongdatastoretwo")
	public Datastore mongdatastoretwo(MongoClient mongoClient) {
		ds = morphia().createDatastore(mongoClient, "mytasktwo");
		ds.ensureIndexes();
		ds.ensureCaps();
		
	
		return ds;
	}
	



}

 

你可能感兴趣的:(mongdb同一连接,多数据库配置)