前言
本文主要给大家介绍了关于利用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持久化
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); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对小牛知识库的支持。
为了学习数据的持久化,写一个简单的地址薄合约.虽然这个例子因为各种原因作为生产环境的合约不太实用,但它是一个很好的合约用来学习EOSIO的数据持久化并且不会因为与eosio multi_index不相关的相关业务逻辑分心. Step 1:创建一个新的文件夹 进入之前的目录: cd /Users/zhong/coding/CLion/contracts 为我们的合约创建一个新的目录并进去: mkd
我试图在mongo数据库中插入一个文档(json字符串)。其中一个键“profile”的值是json字符串。所以,它基本上是一个嵌套的json结构。我知道可以通过滥用document类中的collection refs/one may关系来插入嵌套的json。 我在这里面临的问题是,嵌套部分的json结构不是固定的,因此不能抽象为java类,因为它是从社交网络API获取的自定义数据json。将“p
我创建了一个简单的3实体数据模型,当试图持久化数据时,它不起作用。下面是实体及其id类,server: 服务: 容器: 创建的数据库似乎正常:
本文向大家介绍Oracle 10g利用amdu抽取数据文件的方法教程,包括了Oracle 10g利用amdu抽取数据文件的方法教程的使用技巧和注意事项,需要的朋友参考一下 前言 本文主要给大家介绍的是关于Oracle 10g利用amdu抽取数据文件的相关内容,下面话不多说了,来一起看看详细的介绍吧 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 需求:实验在Oracle 10g环
我使用SpringDataMongo作为ORM来访问我的MongoDb。我需要阅读一个MongoDb集合,其中包含我不管理内容及其有效性的文档。我面临一个问题:文档并非都是有效的,当我使用“findAll”方法时,我只捕获了一个异常。我希望找到所有有效的文档并拒绝无效的文档,而不是这种行为。 例如,我有一个带有原始布尔值的bean,在文档中,该字段被设置为String类型。因此,当我使用findA
数据落盘问题的由来 这本质上是数据持久化问题,对于有些应用依赖持久化数据,比如应用自身产生的日志需要持久化存储的情况,需要保证容器里的数据不丢失,在Pod挂掉后,其他应用依然可以访问到这些数据,因此我们需要将数据持久化存储起来。 数据落盘问题解决方案 下面以一个应用的日志收集为例,该日志需要持久化收集到ElasticSearch集群中,如果不考虑数据丢失的情形,可以直接使用前面提到的应用日志收集一