spring Multiple MongoTemplate

    <!-- 数据环境配置 -->



    <mongo:repositories base-package="com.my9yu.manager.module.server.repository" />



    <mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}"></mongo:mongo>

    <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}"

        mongo-ref="mongo" username="${mongo.username}" password="${mongo.password}" />



    

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

         <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

    </bean>





    <!-- 数据环境配置2 -->

    <mongo:mongo id="mongo2" host="${cpmongo.host}" port="${cpmongo.port}"></mongo:mongo>

    <mongo:db-factory id="mongoDbFactory2" dbname="${cpmongo.dbname}"

        mongo-ref="mongo2" username="${cpmongo.username}" password="${cpmongo.password}" />



    <bean id="mongoTemplate2" name="mongoTemplate2" class="org.springframework.data.mongodb.core.MongoTemplate">

         <constructor-arg name="mongoDbFactory" ref="mongoDbFactory2" />

    </bean>
@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "copy.xml" })

public class MongoCopyData {



    @Autowired

    private MongoTemplate mongoTemplate;

    @Autowired

    @Qualifier("mongoTemplate2")

    private MongoTemplate mongoTemplate2;



    @Autowired

    private OperatorRepository operatorRepository;



    @Test

    public void test() {



        List<Operator> ops = mongoTemplate.find(new Query(), Operator.class);

        List<Operator> ops2 = mongoTemplate2.find(new Query(), Operator.class);



        System.out.println("start++++++++++++++++++++++++++++++");

        System.out.println("1++++++++++++++++++++++++++++++" + JsonUtils.object2String(ops));

        System.out.println("2++++++++++++++++++++++++++++++" + JsonUtils.object2String(ops2));



        System.out.println("1++++++++++++++++++++++++++++++" + mongoTemplate.getDb().getName());

        System.out.println("2++++++++++++++++++++++++++++++" + mongoTemplate2.getDb().getName());

        long start = System.currentTimeMillis();

        for (final Operator op : ops) {

            operatorRepository.save(op);

        }

        long end = System.currentTimeMillis();



    }



}

 

你可能感兴趣的:(template)