您好,登錄后才能下訂單哦!
前言
本文主要給大家介紹了關于利用Spring Data MongoDB持久化文檔數據的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
介紹
Spring Data MongoDB
1.Spring Data MongoDB提供了三種方式在Spring應用中使用MongoDB
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 //這是一個文檔 public class Order { @Id //指定id private String id; @Field("client") //覆蓋默認的域名 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
第一種方式:
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的配置 * 擴展AbstractMongoConfiguration * */ @Configuration @EnableMongoRepositories(basePackages="com.adagio.db") //啟用MongoDB的Repository功能 public class MongoConfig2 extends AbstractMongoConfiguration { @Override protected String getDatabaseName() { return "OrdersDB"; //指定數據庫名 } @Autowired private Environment env; @Override public Mongo mongo() throws Exception { // return new MongoClient(); //創建Mongo客戶端 //如果MongoDB服務器運行在其他的機器上 // return new MongoClient("mongoServer"); //如果MongoDB服務器監聽的端口不是默認端口27017 // return new MongoClient("mongoServer", 37017); //如果MongoDB服務器在生產配置上,啟用了認證功能 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.為模型添加注解,實現MongoDB持久化
Order.items
屬性,不是 關聯關系,會完全嵌入到Order中4.使用MongoTemplate訪問MongoDB
5.編寫MongoDB Repository
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.編寫混合實現
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; //創建查詢 Criteria where = Criteria.where("type").is(type); Query query = Query.query(where); //執行查詢 return mongo.find(query, Order.class); } }
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。