https://blog.csdn.net/forezp/article/details/70341818
本文出自方志朋的博客
這篇文章主要介紹springboot如何整合mongodb。
準(zhǔn)備工作
安裝 MongoDB
jdk 1.8
maven 3.0
idea
環(huán)境依賴
在pom文件引入spring-boot-starter-data-mongodb依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
數(shù)據(jù)源配置
如果mongodb端口是默認(rèn)端口,并且沒有設(shè)置密碼,可不配置,sprinboot會(huì)開啟默認(rèn)的。
spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db
mongodb設(shè)置了密碼,這樣配置:
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname
定義一個(gè)簡(jiǎn)單的實(shí)體
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);
}
}
數(shù)據(jù)操作dao層
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
寫一個(gè)接口,繼承MongoRepository,這個(gè)接口有了幾本的CURD的功能。如果你想自定義一些查詢,比如根據(jù)firstName來(lái)查詢,獲取根據(jù)lastName來(lái)查詢,只需要定義一個(gè)方法即可。注意firstName嚴(yán)格按照存入的mongodb的字段對(duì)應(yīng)。在典型的java的應(yīng)用程序,寫這樣一個(gè)接口的方法,需要自己實(shí)現(xiàn),但是在springboot中,你只需要按照格式寫一個(gè)接口名和對(duì)應(yīng)的參數(shù)就可以了,因?yàn)閟pringboot已經(jīng)幫你實(shí)現(xiàn)了。
測(cè)試
@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);
}
}
在springboot的應(yīng)用程序,加入測(cè)試代碼。啟動(dòng)程序,控制臺(tái)打印了:
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’]
測(cè)試通過。
源碼下載:https://github.com/forezp/SpringBootLearning
參考資料
寫在最后
歡迎關(guān)注、喜歡、和點(diǎn)贊后續(xù)將推出更多的spring cloud
教程,敬請(qǐng)期待。
歡迎關(guān)注我的微信公眾號(hào)獲取更多更全的學(xué)習(xí)資源,視頻資料,技術(shù)干貨!
公眾號(hào)回復(fù)“學(xué)習(xí)”,拉你進(jìn)程序員技術(shù)討論群,干貨資源第一時(shí)間分享。
公眾號(hào)回復(fù)“視頻”,領(lǐng)取800GJava視頻學(xué)習(xí)資源。
公眾號(hào)回復(fù)“全棧”,領(lǐng)取1T前端,Java,產(chǎn)品經(jīng)理,微信小程序,Python等資源合集大放送。
公眾號(hào)回復(fù)“慕課”,領(lǐng)取1T慕課實(shí)戰(zhàn)學(xué)習(xí)資源。
公眾號(hào)回復(fù)“實(shí)戰(zhàn)”,領(lǐng)取750G項(xiàng)目實(shí)戰(zhàn)學(xué)習(xí)資源。
公眾號(hào)回復(fù)“面試”,領(lǐng)取8G面試實(shí)戰(zhàn)學(xué)習(xí)資源。