您好,登錄后才能下訂單哦!
使用springboot如何實現對mongodb進行整合?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
環境依賴
在pom文件引入spring-boot-starter-data-mongodb依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
數據源配置
如果mongodb端口是默認端口,并且沒有設置密碼,可不配置,sprinboot會開啟默認的。
spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db
mongodb設置了密碼,這樣配置:
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); } }
數據操作dao層
public interface CustomerRepository extends MongoRepository<Customer, String> { public Customer findByFirstName(String firstName); public List<Customer> findByLastName(String lastName); }
寫一個接口,繼承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); } }
在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']
看完上述內容,你們掌握使用springboot如何實現對mongodb進行整合的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。