java version of spring cloud + spring boot + redis multi-tenant social e-commerce platform (eight) springboot integration mongodb

Ready to work

  • Install MongoDB
  • jdk 1.8
  • maven 3.0
  • idea

Environmental dependent

Pom document incorporated spring-boot-starter-data-mongodb dependency:

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

Data source configuration

If mongodb port is the default port, and no password is set, from time to configure, sprinboot will open the default.

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

mongodb set a password, this configuration:

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 manipulation layer dao

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

Write an interface, inherited MongoRepository, this interface has CURD functions of several. If you want to customize some queries, such as firstName according to query, access to query according to lastName, only need to define a method can be. Note firstName strict accordance with the corresponding fields mongodb deposit. In a typical application of java, to write such a method interface, you need to achieve their own, but in springboot, you only need to write an interface name and the corresponding parameter in the format can be, because you have to help springboot achieved.

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);
        }
    }

Social e-commerce platform source code, please add penguin beg: 3536247259

Guess you like

Origin www.cnblogs.com/springfresh/p/10996045.html