springboot如何進(jìn)行整合MongoDB,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)客戶idc服務(wù)中心,提供資陽移動(dòng)機(jī)房、成都服務(wù)器、成都主機(jī)托管、成都雙線服務(wù)器等業(yè)務(wù)的一站式服務(wù)。通過各地的服務(wù)中心,我們向成都用戶提供優(yōu)質(zhì)廉價(jià)的產(chǎn)品以及開放、透明、穩(wěn)定、高性價(jià)比的服務(wù),資深網(wǎng)絡(luò)工程師在機(jī)房提供7*24小時(shí)標(biāo)準(zhǔn)級(jí)技術(shù)保障。
安裝 MongoDB
jdk 1.8
maven 3.0
idea
在pom文件引入spring-boot-starter-data-mongodb依賴:
org.springframework.boot spring-boot-starter-data-mongodb
如果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
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); } }
public interface CustomerRepository extends MongoRepository{ public Customer findByFirstName(String firstName); public List findByLastName(String lastName); }
寫一個(gè)接口,繼承MongoRepository,這個(gè)接口有了幾本的CURD的功能。如果你想自定義一些查詢,比如根據(jù)firstName來查詢,獲取根據(jù)lastName來查詢,只需要定義一個(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)了。
@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)打印了:
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è)試通過。
關(guān)于springboot如何進(jìn)行整合mongodb問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。