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

    复制请注明出处,在世界中挣扎的灰太狼
  • 相关阅读:
    遗传算法在自动组卷中的应用
    MYSQL添加远程用户或允许远程访问
    Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not access
    Linux iptables开启80端口
    struts2
    struts2 action之间参数的传递
    struts2
    struts2 -result 中的 redirect 和 redirectAction 的 区别
    struts2
    servlet 基础
  • 原文地址:https://www.cnblogs.com/XingXiaoMeng/p/13879371.html
Copyright © 2011-2022 走看看