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);
            }
    
        }
    
    }
  • 相关阅读:
    PHP实现用户注册并保存数据到文件
    浏览器与服务端请求响应流程与HTTP协议
    apache(OS 10013)以一种访问权限不允许的方式做了一个访问套接字的尝试 ...
    webpack4.0样式处理(1)
    webpack4.0:html插件
    webpack4.0:webpack基础配置
    webpack4.0---url-loader
    webpack4.0学习(1)
    深拷贝和浅拷贝
    'mongoimport'不是内部或外部命令,也不是可运行的程序
  • 原文地址:https://www.cnblogs.com/feika/p/4383794.html
Copyright © 2011-2022 走看看