这里主要介绍的是Spring与MongoDB的持久化实现,使用了Spring的MongoTemplate
spring data MongoDB的开发文档
Spring Data MongoDB 2.1.5.RELEASE API
MongoTemplate相关方法使用文档
MongoTemplate (Spring Data MongoDB 3.4.2 API)
1、增加依赖
<!-- spring整合MongoDB -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${spring.data.version}</version>
</dependency>
2、增加配置
spring.data.mongodb.database=test spring.data.mongodb.uri=mongodb://user:password@192.168.1.2:27017/admin
在对应的 Dao层 注入 MongoTemplate
public void mongoDbJavaTest(){
MongoDbJavaTest mongoDbJavaTest = new MongoDbJavaTest();
mongoDbJavaTest.setName("admin");
mongoDbJavaTest.setRemarks("测试");
Query query = Query.query(Criteria.where("name").is("admin"));
mongoTemplate.find(query,MongoDbJavaTest.class);
mongoTemplate.find(query,MongoDbJavaTest.class,"mongodb_java_test");
mongoTemplate.findAll(MongoDbJavaTest.class);
mongoTemplate.findAll(MongoDbJavaTest.class,"mongodb_java_test");
mongoTemplate.query(MongoDbJavaTest.class);
}
5、插入数据
public void mongoDbJavaSave(){
List<MongoDbJavaTest> list = new ArrayList<>();
MongoDbJavaTest mongoDbJavaTest = new MongoDbJavaTest();
mongoDbJavaTest.setName("admin");
mongoDbJavaTest.setRemarks("测试");
list.add(mongoDbJavaTest);
// 保存对象到mongodb
mongoTemplate.save(mongoDbJavaTest);
mongoTemplate.insert(mongoDbJavaTest);
// 根据集合名称保存对象到mongodb
mongoTemplate.save(mongoDbJavaTest,"mongodb_java_test");
mongoTemplate.insert(mongoDbJavaTest,"mongodb_java_test");
// 根据集合名称保存list到mongodb
mongoTemplate.save(list,"mongodb_java_test");
mongoTemplate.insert(list,"mongodb_java_test");
mongoTemplate.insert(list,MongoDbJavaTest.class);
}
6、更新数据
public void mongoDbJavaUpdate(){
MongoDbJavaTest mongoDbJavaTest = new MongoDbJavaTest();
mongoDbJavaTest.setId("5d1312aeb1829c279c6c256b");
mongoDbJavaTest.setName("admin");
mongoDbJavaTest.setRemarks("测试");
Query query = Query.query(Criteria.where("_id").is("5d1312aeb1829c279c6c256b"));
Update update = Update.update("name","admin");
// 更新一条数据
mongoTemplate.updateFirst(query,update, MongoDbJavaTest.class);
mongoTemplate.updateFirst(query,update, "mongodb_java_test");
mongoTemplate.updateFirst(query,update, MongoDbJavaTest.class,"mongodb_java_test");
// 根据实体更新
mongoTemplate.update(mongoDbJavaTest.getClass());
// 更新多条数据
mongoTemplate.updateMulti(query,update, MongoDbJavaTest.class);
mongoTemplate.updateMulti(query,update,"mongodb_java_test");
mongoTemplate.updateMulti(query,update, MongoDbJavaTest.class,"mongodb_java_test");
// 更新数据,如果数据不存在就新增
mongoTemplate.upsert(query,update, MongoDbJavaTest.class);
mongoTemplate.upsert(query,update,"mongodb_java_test");
mongoTemplate.upsert(query,update, MongoDbJavaTest.class,"mongodb_java_test");
}
7、删除数据
public void mongoDbJavaRemove(){
List<MongoDbJavaTest> list = new ArrayList<>();
MongoDbJavaTest mongoDbJavaTest = new MongoDbJavaTest();
mongoDbJavaTest.setId("5d1312aeb1829c279c6c256b");
list.add(mongoDbJavaTest);
Query query = Query.query(Criteria.where("_id").in("5d1312aeb1829c279c6c256b","5d13133ab1829c29d02ce29c"));
// 根据条件删除
mongoTemplate.remove(query);
mongoTemplate.remove(mongoDbJavaTest);
mongoTemplate.remove(MongoDbJavaTest.class);
// 根据条件删除(可删除多条)
mongoTemplate.remove(query,CarryUserInfo.class);
mongoTemplate.remove(query,MongoDbJavaTest.class,"mongodb_java_test");
}