zoukankan      html  css  js  c++  java
  • Spring-boot访问MongoDB

    1、访问配置信息

    package hello;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.authentication.UserCredentials;
    import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
    import org.springframework.data.mongodb.core.MongoOperations;
    import org.springframework.data.mongodb.core.MongoTemplate;
    
    import com.mongodb.Mongo;
    import com.mongodb.MongoClient;
    
    @Configuration
    public class MongoConfig extends AbstractMongoConfiguration {
     
        @Bean
        public Mongo mongo() throws Exception {
            return new MongoClient();
        }
     
        @Bean
        public MongoTemplate mongoTemplate() throws Exception {
            UserCredentials user = new UserCredentials("scott", "tiger");
            return new MongoTemplate(mongo(), "test1", user);
        }
    
        @Override
        protected String getDatabaseName() {
            // TODO Auto-generated method stub
            return "test1";
        }
     
    }

    2、POJO类

    package hello;
    
    import org.springframework.data.annotation.Id;
    
    
    public class Customer {
    
        @Id
        private String id;
    
        private String firstName;
        private String lastName;
    
        public Customer() {}
    
        public Customer(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        @Override
        public String toString() {
            return String.format(
                    "Customer[id=%s, firstName='%s', lastName='%s']",
                    id, firstName, lastName);
        }
    
    }

    3、Repository接口

    package hello;
    
    import java.util.List;
    
    import org.springframework.data.mongodb.repository.MongoRepository;
    
    public interface CustomerRepository extends MongoRepository<Customer, String> {
    
        public Customer findByFirstName(String firstName);
        public List<Customer> findByLastName(String lastName);
    
    }

    4、Application

    package hello;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.AutoConfigureBefore;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Import;
    
    @SpringBootApplication
    @AutoConfigureBefore
    @Import(MongoConfig.class)
    public class Application implements CommandLineRunner {
    
        @Autowired
        private CustomerRepository repository;
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
    
            repository.deleteAll();
    
            // save a couple of customers
            repository.save(new Customer("Alice", "Smith"));
            repository.save(new Customer("Bob", "Smith"));
    
            // fetch all customers
            System.out.println("Customers found with findAll():");
            System.out.println("-------------------------------");
            for (Customer customer : repository.findAll()) {
                System.out.println(customer);
            }
            System.out.println();
    
            // fetch an individual customer
            System.out.println("Customer found with findByFirstName('Alice'):");
            System.out.println("--------------------------------");
            System.out.println(repository.findByFirstName("Alice"));
    
            System.out.println("Customers found with findByLastName('Smith'):");
            System.out.println("--------------------------------");
            for (Customer customer : repository.findByLastName("Smith")) {
                System.out.println(customer);
            }
    
        }
    
    }
  • 相关阅读:
    好理解的堆排序
    SpringCloud 整合 Python 服务
    SpringCloud入门总结
    Spring异常集中处理和日志集中打印
    Java枚举类型的理解及在后台响应中的使用
    Elasticsearch合并高亮字段
    Elasticsearch分析器结构组成
    Elasticsearch实现英文区分大小写搜索
    Nginx三大功能
    Elasticsearch Java Client 版本区别及起步(5.X 和6.X)
  • 原文地址:https://www.cnblogs.com/feika/p/4383794.html
Copyright © 2011-2022 走看看