摘要:如果你想自定義一些查詢,比如根據(jù)來查詢,獲取根據(jù)來查詢,只需要定義一個(gè)方法即可。注意嚴(yán)格按照存入的的字段對應(yīng)。測試在的應(yīng)用程序,加入測試代碼。啟動(dòng)程序,控制臺打印了測試通過。
這篇文章主要介紹springboot如何整合mongodb。
準(zhǔn)備工作安裝 MongoDB jdk 1.8 maven 3.0 idea環(huán)境依賴
在pom文件引入spring-boot-starter-data-mongodb依賴:
數(shù)據(jù)源配置org.springframework.boot spring-boot-starter-data-mongodb
如果mongodb端口是默認(rèn)端口,并且沒有設(shè)置密碼,可不配置,sprinboot會開啟默認(rèn)的。
spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db
mongodb設(shè)置了密碼,這樣配置:
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname定義一個(gè)簡單的實(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{ public Customer findByFirstName(String firstName); public List findByLastName(String lastName); }
寫一個(gè)接口,繼承MongoRepository,這個(gè)接口有了幾本的CURD的功能。如果你想自定義一些查詢,比如根據(jù)firstName來查詢,獲取根據(jù)lastName來查詢,只需要定義一個(gè)方法即可。注意firstName嚴(yán)格按照存入的mongodb的字段對應(yīng)。在典型的java的應(yīng)用程序,寫這樣一個(gè)接口的方法,需要自己實(shí)現(xiàn),但是在springboot中,你只需要按照格式寫一個(gè)接口名和對應(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)用程序,加入測試代碼。啟動(dòng)程序,控制臺打印了:
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’]
測試通過。
源碼下載:https://github.com/forezp/Spr...
參考資料accessing-data-mongodb
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/70381.html
摘要:通用是為了解決使用中的基本操作,使用它可以很方便的進(jìn)行開發(fā),可以節(jié)省開發(fā)人員大量的時(shí)間。當(dāng)該參數(shù)設(shè)置為時(shí),時(shí)會查詢第一頁,超過總數(shù)時(shí),會查詢最后一頁。 SpringBoot 是為了簡化 Spring 應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物,自動(dòng)裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相關(guān)的依賴就可以輕易的搭建出一個(gè) WEB 工...
摘要:構(gòu)建工程創(chuàng)建一個(gè)工程,在它的程序入口加上開啟調(diào)度任務(wù)。創(chuàng)建定時(shí)任務(wù)創(chuàng)建一個(gè)定時(shí)任務(wù),每過在控制臺打印當(dāng)前時(shí)間。通過在方法上加注解,表明該方法是一個(gè)調(diào)度任務(wù)。 這篇文章將介紹怎么通過spring去做調(diào)度任務(wù)。 構(gòu)建工程 創(chuàng)建一個(gè)Springboot工程,在它的程序入口加上@EnableScheduling,開啟調(diào)度任務(wù)。 @SpringBootApplication @EnableSch...
摘要:開公眾號差不多兩年了,有不少原創(chuàng)教程,當(dāng)原創(chuàng)越來越多時(shí),大家搜索起來就很不方便,因此做了一個(gè)索引幫助大家快速找到需要的文章系列處理登錄請求前后端分離一使用完美處理權(quán)限問題前后端分離二使用完美處理權(quán)限問題前后端分離三中密碼加鹽與中異常統(tǒng)一處理 開公眾號差不多兩年了,有不少原創(chuàng)教程,當(dāng)原創(chuàng)越來越多時(shí),大家搜索起來就很不方便,因此做了一個(gè)索引幫助大家快速找到需要的文章! Spring Boo...
閱讀 850·2021-11-18 10:02
閱讀 2598·2021-11-11 16:54
閱讀 2794·2021-09-02 09:45
閱讀 682·2019-08-30 12:52
閱讀 2823·2019-08-29 14:04
閱讀 2776·2019-08-29 12:39
閱讀 480·2019-08-29 12:27
閱讀 1914·2019-08-26 13:23