Springboot JPA MongoDB 示例

pom.xml



    4.0.0

    org.springframework
    gs-accessing-data-mongodb
    0.1.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
    

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-releases
            Spring Releases
            https://repo.spring.io/libs-release
        
    
    
        
            spring-releases
            Spring Releases
            https://repo.spring.io/libs-release
        
    

数据库配置

application.properties

spring.data.mongodb.uri=mongodb://name:[email protected]:27017/test

entity

src/main/java/hello/Customer.java

package hello;

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

}

repository

src/main/java/hello/CustomerRepository.java

package hello;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface CustomerRepository extends MongoRepository {

    public Customer findByFirstName(String firstName);
    public List findByLastName(String lastName);

}

调用

src/main/java/hello/Application.java

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

    }

}

测试

启动项目:

mvn spring-boot:run

输出结果:

Customers found with findAll():
-------------------------------
Customer[id=5b839c86a9ccdc10222b5c01, firstName='Alice', lastName='Smith']
Customer[id=5b839c86a9ccdc10222b5c02, firstName='Bob', lastName='Smith']

Customer found with findByFirstName('Alice'):
--------------------------------
Customer[id=5b839c86a9ccdc10222b5c01, firstName='Alice', lastName='Smith']
Customers found with findByLastName('Smith'):
--------------------------------
Customer[id=5b839c86a9ccdc10222b5c01, firstName='Alice', lastName='Smith']
Customer[id=5b839c86a9ccdc10222b5c02, firstName='Bob', lastName='Smith']

官方文档

你可能感兴趣的:(Springboot JPA MongoDB 示例)