利用Spring Data MongoDB持久化文檔數(shù)據(jù)的方法教程
前言
本文主要給大家介紹了關(guān)于利用Spring Data MongoDB持久化文檔數(shù)據(jù)的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
介紹
- NoSQL:not only SQL,非關(guān)系型數(shù)據(jù)
- MongoDB是文檔型數(shù)據(jù),文檔是獨(dú)立的實(shí)體,文檔數(shù)據(jù)庫(kù)不適用于關(guān)聯(lián)關(guān)系明顯的數(shù)據(jù)
Spring Data MongoDB
1.Spring Data MongoDB提供了三種方式在Spring應(yīng)用中使用MongoDB
- 通過(guò)注解實(shí)現(xiàn)對(duì)象-文檔映射
- 使用MongoTemplate實(shí)現(xiàn)基于模板的數(shù)據(jù)庫(kù)訪問(wèn)
- 自動(dòng)化的運(yùn)行時(shí)Repository生成功能
import java.util.Collection; import java.util.LinkedHashSet; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; /** * Spring Data MongoDB注解將Java類型映射為文檔 */ @Document //這是一個(gè)文檔 public class Order { @Id //指定id private String id; @Field("client") //覆蓋默認(rèn)的域名 private String customer; private String type; private Collection<Item> items = new LinkedHashSet<>(); public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCustomer() { return customer; } public void setCustomer(String customer) { this.customer = customer; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Collection<Item> getItems() { return items; } public void setItems(Collection<Item> items) { this.items = items; } }
2.啟用MongoDB
- 通過(guò)@EnableJpaRepositories注解啟用Spring Data的自動(dòng)化JPA Repository生成功能
- @EnableMongoRepositories為MongoDB實(shí)現(xiàn)了相同的功能
第一種方式:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import com.mongodb.MongoClient; /** * * Spring Data MongoDB的配置 * */ @Configuration @EnableMongoRepositories(basePackages="com.adagio.db") //啟用MongoDB的Repository功能 public class MongoConfig { /** * MongoTemplate Bean * @param mongoDbFactory * @return */ @Bean public MongoOperations mongoTemplate(){ return new MongoTemplate(mongoDbFactory()); } /** * MongoDbFactory bean * @return */ public MongoDbFactory mongoDbFactory(){ return new SimpleMongoDbFactory(mongoClient(), "com.adagio.db"); } /** * MongoClient bean * @return */ public MongoClient mongoClient(){ return new MongoClient("localhost"); } }
第二種方式
import java.util.Arrays; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import com.mongodb.Mongo; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; /** * * Spring Data MongoDB的配置 * 擴(kuò)展AbstractMongoConfiguration * */ @Configuration @EnableMongoRepositories(basePackages="com.adagio.db") //啟用MongoDB的Repository功能 public class MongoConfig2 extends AbstractMongoConfiguration { @Override protected String getDatabaseName() { return "OrdersDB"; //指定數(shù)據(jù)庫(kù)名 } @Autowired private Environment env; @Override public Mongo mongo() throws Exception { // return new MongoClient(); //創(chuàng)建Mongo客戶端 //如果MongoDB服務(wù)器運(yùn)行在其他的機(jī)器上 // return new MongoClient("mongoServer"); //如果MongoDB服務(wù)器監(jiān)聽(tīng)的端口不是默認(rèn)端口27017 // return new MongoClient("mongoServer", 37017); //如果MongoDB服務(wù)器在生產(chǎn)配置上,啟用了認(rèn)證功能 MongoCredential credential = MongoCredential.createCredential( env.getProperty("mongo.username") , "OrdersDB", env.getProperty("mongo.password").toCharArray()); return new MongoClient( new ServerAddress("localhost", 37017), Arrays.asList(credential)); } }
3.為模型添加注解,實(shí)現(xiàn)MongoDB持久化
- 用于對(duì)象-文檔映射的Spring Data MongoDB注解
@Document 標(biāo)示映射到MongoDB文檔上的領(lǐng)域?qū)ο?類似JPA @Entity注解
@Id 標(biāo)示某個(gè)域?yàn)镮D域
@DbRef 標(biāo)示某個(gè)域要引用的其它的文檔,這個(gè)文檔有可能位于另一個(gè)數(shù)據(jù)庫(kù)中
@Field 為文檔域指定自定義的元數(shù)據(jù)
@Version 標(biāo)示某個(gè)屬性用作版域 - 注意:沒(méi)有添加注解的屬性,也會(huì)持久化為文檔中域,除非設(shè)置瞬時(shí)態(tài)(transient)
- 注意:
Order.items
屬性,不是 關(guān)聯(lián)關(guān)系,會(huì)完全嵌入到Order中
4.使用MongoTemplate訪問(wèn)MongoDB
- 配置類中配置的MongoTemplate bean,將其注入到使用的地方
- @Autowired MongoOperations mongo;
- MongoOperations是MongoTemplate所實(shí)現(xiàn)的接口
- void save(Object objectToSave, String collectionName);
- save第一個(gè)參數(shù)是新創(chuàng)建的對(duì)象,第二個(gè)參數(shù)是要保存的文檔存儲(chǔ)的名稱
5.編寫(xiě)MongoDB Repository
- 使用Spring Data MongoDB來(lái)創(chuàng)建Repository
- 通過(guò)@EnableMongoRepositories注解啟用Spring Data MongoDB的Repository功能
- 通過(guò)擴(kuò)展MongoRepository接口,能夠繼承多個(gè)CRUD操作
6.查詢方式:
- 自定義查詢
- 指定查詢
- 混合定義查詢
//自定義查詢 List<Order> findByCustomer(String customer); List<Order> getByCustomer(String customer); List<Order> readByCustomer(String customer); int countByCustomer(String customer); List<Order> findByCustomerLike(String customer); List<Order> findByCustomerAndType(String customer, String type); List<Order> getByType(String type); //指定查詢 @Query("{customer:'Chuck Wagon'}") List<Order> findChucksOrders();
混合自定義的功能
1.首先,定義中間接口
import java.util.List; public interface OrderOperations { List<Order> findOrderByType(String t); }
2.編寫(xiě)混合實(shí)現(xiàn)
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; public class OrderOperationsimpl implements OrderOperations { @Autowired private MongoOperations mongo; //注入MongoOperations @Override public List<Order> findOrderByType(String t) { String type = t.equals("NET") ? "WEB" : t; //創(chuàng)建查詢 Criteria where = Criteria.where("type").is(type); Query query = Query.query(where); //執(zhí)行查詢 return mongo.find(query, Order.class); } }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
- String在棧中,StringBuffer在堆中!所以String是不可變的,數(shù)據(jù)是共享的。StringBuffer都是獨(dú)占的,是可變的(因?yàn)槊看味际莿?chuàng)建新的對(duì)象?。?/div> 2015-11-11
SpringBoot Actuator未授權(quán)訪問(wèn)漏洞解決方案
工作的時(shí)候遇到過(guò)提示Spring Boot后端存在Actuator未授權(quán)訪問(wèn)漏洞,網(wǎng)上有很多詳細(xì)的解釋文章,在這里做一個(gè)簡(jiǎn)單的總結(jié)、介紹和分享,需要的朋友可以參考下2023-09-09深入了解Java中Cookie和Session的區(qū)別
會(huì)話跟蹤是Web程序中常用的技術(shù),用來(lái)跟蹤用戶的整個(gè)會(huì)話,常用的會(huì)話跟蹤技術(shù)是Cookie與Session,本文就詳細(xì)的介紹一下Java中Cookie和Session的區(qū)別,感興趣的可以了解一下2023-06-06如何將eclipse項(xiàng)目導(dǎo)入到idea的方法步驟(圖文)
這篇文章主要介紹了如何將eclipse項(xiàng)目導(dǎo)入到idea的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03MyBatis常用動(dòng)態(tài)sql大總結(jié)
這篇文章主要給大家介紹了關(guān)于MyBatis常用動(dòng)態(tài)sql的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05java自定義注解實(shí)現(xiàn)前后臺(tái)參數(shù)校驗(yàn)的實(shí)例
下面小編就為大家?guī)?lái)一篇java自定義注解實(shí)現(xiàn)前后臺(tái)參數(shù)校驗(yàn)的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11解決Android Studio安裝后運(yùn)行出錯(cuò)dose not...和Internal error...
這篇文章主要介紹了解決Android Studio安裝后運(yùn)行出錯(cuò)dose not...和Internal error...的相關(guān)資料,需要的朋友可以參考下2017-03-03最新評(píng)論