zoukankan      html  css  js  c++  java
  • Springboot集成MongoDB

    简介

    MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是可以应用于各种规模的企业、各个行业以及各类应用程序的开源数据库。作为一个适用于敏捷开发的数据库,MongoDB的数据模式可以随着应用程序的发展而灵活地更新。与此同时,它也为开发人员 提供了传统数据库的功能:二级索引,完整的查询系统以及严格一致性等等。 MongoDB能够使企业更加具有敏捷性和可扩展性,各种规模的企业都可以通过使用MongoDB来创建新的应用,提高与客户之间的工作效率,加快产品上市时间,以及降低企业成本。

    安装mongoDB

    https://www.cnblogs.com/woshimrf/p/linux-install-mongodb.html

    创建项目

    https://github.com/Ryan-Miao/springboot-with-mongodb

    pom

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.test</groupId>
      <artifactId>springboot-with-mongodb</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>springboot-with-mongodb</name>
      <description>Demo project for Spring Boot</description>
    
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
      </parent>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger2</artifactId>
          <version>2.7.0</version>
        </dependency>
        <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger-ui</artifactId>
          <version>2.7.0</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <scope>runtime</scope>
        </dependency>
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    
    
    </project>
    
    

    配置文件

    spring.data.mongodb.uri=mongodb://localhost:27017/demo
    

    创建一个表/集合

    一个消费者

    @Data
    public class Customer {
    
        @Id
        public String id;
    
        public String firstName;
        public String lastName;
        private List<Hobby> hobbies;
    
        public Customer() {
        }
    
        public Customer(String firstName, String lastName, List<Hobby> hobbies) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.hobbies = hobbies;
        }
    
    }
    
    • import org.springframework.data.annotation.Id; 是mongodb里的主键

    创建Repository

    JPA的一个特性就是简化了CRUD, 通过解析方法名实现数据的传输

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

    方法名findBy字段名即可实现查询。

    启动并测试

    
    @SpringBootApplication
    public class SpringbootWithMongodbApplication implements CommandLineRunner {
    
        private final CustomerRepository repository;
        private final BookRepository bookRepository;
        private final AuthorRepository authorRepository;
    
        @Autowired
        public SpringbootWithMongodbApplication(CustomerRepository repository,
            BookRepository bookRepository,
            AuthorRepository authorRepository) {
            this.repository = repository;
            this.bookRepository = bookRepository;
            this.authorRepository = authorRepository;
        }
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootWithMongodbApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
    
            repository.deleteAll();
    
            // save a couple of customers
            repository.save(new Customer("Alice", "Smith", Lists.newArrayList(new Hobby("读书", 1), new Hobby("看电影", 2))));
            repository.save(new Customer("Bob", "Smith", Lists.newArrayList()));
    
            // 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);
            }
    
        }
    }
    

    至此,hello world完成。基本实现了mongoDB持久层的工作,只要继续深入开发即可。

    关联表

    创建一个书籍的集合

    import java.time.LocalDate;
    import lombok.Data;
    import org.springframework.data.annotation.Id;
    import org.springframework.data.mongodb.core.mapping.Document;
    import org.springframework.data.mongodb.core.mapping.Field;
    
    @Data
    @Document(collection = "books")
    public class Book {
    
        @Id
        private String id;
        private String title;
        @Field("published")
        private LocalDate publicationDate;
    
        // No args Constructor
        public Book(String title, LocalDate publicationDate) {
            this.title = title;
            this.publicationDate = publicationDate;
        }
    
    }
    
    • @Field指定数据库映射的字段
    • @Transient标注的字段则不会映射到db
    • @Document(collection = "books")可以指定集合名称,如果不指定则是类名首字母小写

    创建一个作者,作者拥有书籍

    @Data
    public class Author {
    
        @Id
        private ObjectId id;
        @Indexed(unique = true)
        private String name;
        @DBRef
        private Set<Book> books;
    
        // No args Constructor
        public Author(String name) {
            this.name = name;
        }
    
    }
    
    • @DBRef会引用books的表
    • @Indexed(unique = true)设置索引,并且是唯一性索引

    CRUD

    暂时不自定义查询了,利用内置的查询即可

    public interface AuthorRepository extends MongoRepository<Author, ObjectId> {
    
    }
    public interface BookRepository extends MongoRepository<Book, ObjectId> {
    
    }
    

    测试

    bookRepository.deleteAll();
    authorRepository.deleteAll();
    Book ci = new Book("Continous Integration", LocalDate.now());
    // id will be generated after save
    bookRepository.save(ci);
    Book c2 = new Book("Java编程思想", LocalDate.now());
    Book c3 = new Book("Java核心技术", LocalDate.now());
    Book c4 = new Book("Effective Java", LocalDate.now());
    Book c5 = new Book("深入理解虚拟机", LocalDate.now());
    Book c6 = new Book("深入理解虚拟机", LocalDate.now());
    bookRepository.save(c2);
    bookRepository.save(c3);
    bookRepository.save(c4);
    bookRepository.save(c5);
    bookRepository.save(c6);
    
    List<Book> books = bookRepository.findAll();
    System.out.println(books);
    
    Author julius = new Author("Julius");
    julius.setBooks(Stream.of(ci, c2, c3, c4, c5, c6).collect(Collectors.toSet()));
    authorRepository.save(julius);
    System.out.println(authorRepository.findAll());
    

    启动可以看到控制台输出:

    [Book(id=5b0bec767a49d017f0e46c63, title=Continous Integration, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c64, title=Java编程思想, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c65, title=Java核心技术, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c66, title=Effective Java, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c67, title=深入理解虚拟机, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c68, title=深入理解虚拟机, publicationDate=2018-05-28)]
    [Author(id=5b0bec767a49d017f0e46c69, name=Julius, books=[Book(id=5b0bec767a49d017f0e46c64, title=Java编程思想, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c68, title=深入理解虚拟机, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c67, title=深入理解虚拟机, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c63, title=Continous Integration, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c65, title=Java核心技术, publicationDate=2018-05-28), Book(id=5b0bec767a49d017f0e46c66, title=Effective Java, publicationDate=2018-05-28)])]
    
    

    连接db,查询

    db.author.find({})
    
    { 
        "_id" : ObjectId("5b0bec767a49d017f0e46c69"), 
        "name" : "Julius", 
        "books" : [
            DBRef("books", ObjectId("5b0bec767a49d017f0e46c64")), 
            DBRef("books", ObjectId("5b0bec767a49d017f0e46c68")), 
            DBRef("books", ObjectId("5b0bec767a49d017f0e46c67")), 
            DBRef("books", ObjectId("5b0bec767a49d017f0e46c63")), 
            DBRef("books", ObjectId("5b0bec767a49d017f0e46c65")), 
            DBRef("books", ObjectId("5b0bec767a49d017f0e46c66"))
        ], 
        "_class" : "com.test.springbootwithmongodb.entity.Author"
    }
    
    

    MongoTemplate

    可以自己注入MongoTemplate来实现更多操作, 比如

    private final MongoTemplate mongoTemplate;
    
    List<Customer> list = mongoTemplate.findAll(Customer.class);  
    

    索引

    还可以这样设置联合索引

    @Document
    @CompoundIndexes({
        @CompoundIndex(name = "email_age", def = "{'email.id' : 1, 'age': 1}")
    })
    public class User {
        //
    }
    

    查询索引

    db.user.getIndexes();
    
    {
        "v" : 1,
        "key" : {
            "email.id" : 1,
            "age" : 1
        },
        "name" : "email_age",
        "ns" : "test.user"
    }
    
  • 相关阅读:
    Java基础——Java反射机制
    Java基础——Java常用类
    Java基础——多线程
    Java基础——IO流--转换流、标准输入输出流
    Java基础——IO流
    Java基础——枚举与注解
    Java基础——泛型
    codeforces B. Bear and Strings 解题报告
    codeforces A. Black-and-White Cube 解题报告
    codeforces B. Ping-Pong (Easy Version) 解题报告
  • 原文地址:https://www.cnblogs.com/woshimrf/p/springboot-with-mongodb.html
Copyright © 2011-2022 走看看