1. 程式人生 > >轉載:SpringBoot非官方教程 | 第八篇:springboot整合mongodb

轉載:SpringBoot非官方教程 | 第八篇:springboot整合mongodb

這篇文章主要介紹springboot如何整合mongodb。

準備工作

  • jdk 1.8
  • maven 3.0
  • idea

環境依賴

在pom檔案引入spring-boot-starter-data-mongodb依賴:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

##資料來源配置

如果mongodb埠是預設埠,並且沒有設定密碼,可不配置,sprinboot會開啟預設的。

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

mongodb設定了密碼,這樣配置:

spring.data.mongodb.uri=mongodb://name:[email protected]:27017/dbname
  • 1

定義一個簡單的實體

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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

資料操作dao層

public interface CustomerRepository extends MongoRepository<Customer, String> {
    public Customer findByFirstName(String firstName);
    public List<Customer> findByLastName(String lastName);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

寫一個介面,繼承MongoRepository,這個介面有了幾本的CURD的功能。如果你想自定義一些查詢,比如根據firstName來查詢,獲取根據lastName來查詢,只需要定義一個方法即可。注意firstName嚴格按照存入的mongodb的欄位對應。在典型的java的應用程式,寫這樣一個介面的方法,需要自己實現,但是在springboot中,你只需要按照格式寫一個介面名和對應的引數就可以了,因為springboot已經幫你實現了。

測試

@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);
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

在springboot的應用程式,加入測試程式碼。啟動程式,控制檯列印了:

Customers found with findAll(): ------------------------------- Customer[id=58f880f589ffb696b8a6077e, firstName=‘Alice’, lastName=‘Smith’] Customer[id=58f880f589ffb696b8a6077f, firstName=‘Bob’, lastName=‘Smith’] Customer found with findByFirstName(‘Alice’): -------------------------------- Customer[id=58f880f589ffb696b8a6077e, firstName=‘Alice’, lastName=‘Smith’] Customers found with findByLastName(‘Smith’): -------------------------------- Customer[id=58f880f589ffb696b8a6077e, firstName=‘Alice’, lastName=‘Smith’] Customer[id=58f880f589ffb696b8a6077f, firstName=‘Bob’, lastName=‘Smith’]

測試通過。

參考資料

優秀文章推薦: