从0开始搭建一个生产级SpringBoot2.0.X项目(十三)SpringBoot连接MongoDB
前言
最近有个想法想整理一个内容比较完整springboot项目初始化Demo。
SpringBoot连接MongoDB
一、pom文件新增依赖
<!--mongodb-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!--mongodb-->
二、增加Mongo配置
spring:
data:
mongodb:
host: 127.0.0.1
port: 27017
database: murg
三、创建简易的工具类
package com.murg.bootdemo.util;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
@Component
public class MongoUtil {
@Autowired
private MongoTemplate mongoTemplate;
public MongoOperations getMongoTemplate() {
return mongoTemplate;
}
public <T> T insert(T objectToSave, String collectionName) {
return mongoTemplate.insert(objectToSave, collectionName);
}
public <T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName) {
return mongoTemplate.insert(batchToSave, collectionName);
}
public <T> T save(T objectToSave) {
return mongoTemplate.save(objectToSave);
}
public <T> T save(T objectToSave, String collectionName) {
return mongoTemplate.save(objectToSave, collectionName);
}
public <T> T findOne(Query query, Class<T> entityClass) {
return mongoTemplate.findOne(query, entityClass);
}
public <T> T findOne(Query query, Class<T> entityClass, String collectionName) {
return mongoTemplate.findOne(query, entityClass, collectionName);
}
public boolean exists(Query query, String collectionName) {
return mongoTemplate.exists(query, collectionName);
}
public boolean exists(Query query, Class<?> entityClass, String collectionName) {
return mongoTemplate.exists(query, entityClass);
}
public <T> List<T> find(Query query, Class<T> entityClass, String collectionName) {
return mongoTemplate.find(query, entityClass, collectionName);
}
public long count(Query query, String collectionName) {
return mongoTemplate.count(query, collectionName);
}
public UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName) {
return mongoTemplate.updateMulti(query, update, entityClass, collectionName);
}
public DeleteResult remove(Object object, String collectionName) {
return mongoTemplate.remove(object, collectionName);
}
}
三、测试类测试
package com.murg.bootdemo;
import com.murg.bootdemo.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import com.murg.bootdemo.util.MongoUtil;
import java.util.HashMap;
import java.util.Map;
@ActiveProfiles("dev")
@RunWith(SpringRunner.class)
@SpringBootTest(classes={BootdemoApplication.class})// 指定启动类
@Slf4j
public class MongonDbTests {
@Autowired
MongoUtil mongoUtil;
@Test
public void testMongonDb() throws InterruptedException {
Map map = new HashMap();
map.put("id","1");
map.put("userid","00001");
map.put("name","张三");
/*-----------新增-----------------*/
Map usermap = mongoUtil.insert(map, "user");
log.info("新增user: {}", JsonUtil.toJson(usermap));
//Criteria类可以用于编写特定的执行条件,与Query对象进行组合使用
//使用criatira对象去调用andOperator()来编写具体具体条件;
//**单个条件**的编写方式为:Criteria.where("id").is(id);
//如果是**多个条件**,则Criteria.where("id").is(id)**,and("name").is(name)**
// Criteria.where("userid").is(usermap.get("userid"));
//再把criatira对象传入query对象即可
//为了省略Criteria对象的写法,此处可以直接利用query对象来调用addCriteria()来编写条件就行
/*-----------查询-----------------*/
Query query = new Query(Criteria.where("userid").is(usermap.get("userid")));
usermap = mongoUtil.findOne(query, Map.class, "user");
log.info("one: {}", JsonUtil.toJson(usermap));
/*-----------修改-----------------*/
//名字更新成李四
Update update = new Update();
update.set("name", "李四");
mongoUtil.updateMulti(query, update, Map.class, "user");
usermap = mongoUtil.findOne(query, Map.class, "user");
log.info("one: {}", JsonUtil.toJson(usermap));
}
}
运行结果如下
mongodb数据