zoukankan      html  css  js  c++  java
  • spring集成mongodb简单使用和测试方式

    @EnableMongoRepositories
    @ComponentScan(basePackages = "cn.example")
    @Configuration
    public class AppConfig extends AbstractMongoClientConfiguration {
    
    //    @Bean
        public MongoClientFactoryBean mongoClientFactoryBean() {
            MongoClientFactoryBean mongoClientFactoryBean = new MongoClientFactoryBean();
            mongoClientFactoryBean.setMongoClientSettings(mongoClientSettings());
            return mongoClientFactoryBean;
        }
        
        @Override
        protected String getDatabaseName() {
            // TODO Auto-generated method stub
            return "test";
        }
        
        @Override
        public void configureClientSettings(Builder builder) {
            builder.applyConnectionString(new ConnectionString("mongodb://localhost:27017"));
        }
    
    }
    public class Application 
    {
        @SuppressWarnings("resource")
        public static void main( String[] args )
        {
            AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
            PersonRepository personRepository = annotationConfigApplicationContext.getBean(PersonRepository.class);
            
            Person person = new Person();
            person.setName("我是iemo2");
            person.setAge(21);
            person.setEmail("qq.Eail.com");
            personRepository.insert(person);
            
            
            
        }
    }

    源代码:https://github.com/starSmallDream/SpringIntegratedMongoDB

    spring官方文档:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-java-instantiating-container

    复制请注明出处,在世界中挣扎的灰太狼
  • 相关阅读:
    函数与宏定义实验报告
    C语言作业3
    循环结构课后反思
    C语言作业2
    C程序设计实验报告
    百分制成绩五级分制输出--二次作业
    第九章 结构体与共用体
    第八章 指针实验
    第七章 数组实验
    第六章 函数和宏定义实验(2)
  • 原文地址:https://www.cnblogs.com/XingXiaoMeng/p/13879371.html
Copyright © 2011-2022 走看看