Enterprise spring boot tutorial spring boot integrates mongodb

Posted by penguinmasta on Mon, 09 Dec 2019 06:08:11 +0100

Preparation

  • Install MongoDB
  • jdk 1.8
  • maven 3.0
  • idea

Environmental dependence

Introduce spring boot starter data mongodb dependency in pom file:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

Data source configuration

If the mongodb port is the default port and there is no password set, it can not be configured. Springboot will turn on the default port.

spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db

mongodb sets the password, which is configured as follows:

spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname

Define a simple entity

mongodb

package com.forezp.entity;
 
import org.springframework.data.annotation.Id;
 
 
public class Customer {
 
    @Id
    public String id;
 
    public String firstName;
    public 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);
    }
 
}

Data operation dao layer

public interface CustomerRepository extends MongoRepository<Customer, String> {
 
    public Customer findByFirstName(String firstName);
    public List<Customer> findByLastName(String lastName);
 
}

Write an interface, inherit MongoRepository, this interface has several CURD functions. If you want to customize some queries, such as query according to firstName, get query according to lastName, just define a method. Note that firstName strictly corresponds to the field of the stored mongodb. In a typical java application, to write such an interface method, you need to implement it by yourself, but in springboot, you only need to write an interface name and corresponding parameters according to the format, because springboot has been implemented for you.

test

@SpringBootApplication
public class SpringbootMongodbApplication  implements CommandLineRunner {
 
 
    @Autowired
    private CustomerRepository repository;
 
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMongodbApplication.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);
        }
    }

Spring Cloud large enterprise distributed microservice Cloud Architecture source code Please add Penguin request: 17917433880

Topics: Programming MongoDB Spring SpringBoot JDK