前言
本文主要给大家介绍了关于利用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/274316.html">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); }}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对VeVb武林网的支持。
新闻热点
疑难解答
图片精选