1.项目引入pom依赖
<!-- mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.配置连接
#mongodb配置
data:
mongodb:
host: 192.168.0.15
port: 27017
database: test
username: root
password: admindev123$%^
3.简单使用
新建一个实体类
MongodbMessage.class
package org.jeecg.common.mongodb;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
/**
* @Description:消息记录
* @author:hanby
* @date:Created in 2021/08/25
* @version V1.0
*/
@Data
@Document(collection = "MongodbMessage")//对应数据库的表名
public class MongodbMessage {
/**
* 主键
*/
@ApiModelProperty(value = "主键")
private java.lang.String id;
/**
* 发送人
*/
@ApiModelProperty(value = "发送人")
private java.lang.String from;
/**
* 接收人
*/
@ApiModelProperty(value = "接收人")
private java.lang.String to;
/**
* 信息详情
*/
@ApiModelProperty(value = "信息详情")
private java.lang.String message;
/**
* 发送时间
*/
@ApiModelProperty(value = "发送时间")
private Date datetime;
/**
* 组Id
*/
@ApiModelProperty(value = "组Id")
private java.lang.String uid;
}
/**
* 直接注入mongoTemplate即可使用
*/
@Resource
private MongoTemplate mongoTemplate;
JeecgTestMongodbController.class
package org.jeecg.modules.cloud.mongodb;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.mongodb.MongodbMessage;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
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.stereotype.Repository;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;
@RestController
@RequestMapping("/sys/mongodbtest")
@Api(tags = "【微服务】单元测试")
@Repository("deviceStatusRepository")
public class JeecgTestMongodbController {
/**
* 直接注入mongoTemplate即可使用
*/
@Resource
private MongoTemplate mongoTemplate;
@PostMapping("saveMongodb")
@ApiOperation(value = "测试saveMongodb", notes = "测试saveMongodb")
public Result<String> saveMongodb() {
MongodbMessage message = new MongodbMessage();
message.setFrom("1");
message.setTo("2");
message.setMessage("第一个消息");
message.setDatetime(new Date());
message.setUid("111");
//存储操作
mongoTemplate.save(message);
//存储多个
// ArrayList<MongodbMessage> messageList = new ArrayList<>();
//
// MongodbMessage message1 = new MongodbMessage();
// message1.setFrom("1");
// message1.setTo("2");
// message1.setMessage("第一个消息");
// message1.setDatetime(new Date());
// message1.setUid("111");
// messageList.add(message1);
//
// MongodbMessage message2 = new MongodbMessage();
// message2.setFrom("1");
// message2.setTo("2");
// message2.setMessage("第二个消息");
// message2.setDatetime(new Date());
// message2.setUid("111");
// messageList.add(message2);
// mongoTemplate.save(messageList);
return Result.OK("mongodb保存成功");
}
@PostMapping("deleteMongodb")
@ApiOperation(value = "测试deleteMongodb", notes = "测试deleteMongodb")
public Result<String> deleteMongodb(@RequestParam(name = "from", required = true) String from) {
//mongoTemplate.remove("1");
//mongoTemplate.remove("1","MongodbMessage");
//构建查询
Query query = new Query();
//拼装删除数据
query.addCriteria(Criteria.where("from").is(from));
//mongoTemplate.remove(query, "记录表");//删除所有
mongoTemplate.remove(query, MongodbMessage.class);
return Result.OK("mongodb删除成功");
}
@PostMapping("editMongodb")
@ApiOperation(value = "测试editMongodb", notes = "测试editMongodb")
public Result<String> editMongodb(@RequestParam(name = "from", required = true) String from, @RequestParam(name = "message", required = true) String message) {
//拼装修改数据
//Query query = new Query(Criteria.where("from").is(from));
Update update = new Update();
update.set("message", message);
//mongoTemplate.updateFirst(query, update, MongodbMessage.class);
//批量修改
Query query = new Query(Criteria.where("from").is(from));
mongoTemplate.updateMulti(query, update, MongodbMessage.class);
return Result.OK("mongodb修改成功");
}
@PostMapping("queryMongodb")
@ApiOperation(value = "测试queryMongodb", notes = "测试queryMongodb")
public Result<?> 测试queryMongodb(@RequestParam(name = "from", required = true) String from, @RequestParam(name = "to", required = true) String to, @RequestParam(name = "page", required = true) int page, @RequestParam(name = "size", required = true) int size) {
//单条
//String id = "1";
//Query query = new Query(Criteria.where("id").is(id));//可累加条件 is是条件查,regex是模糊查
//Message message = mongoTemplate.findOne(query,Message.class);
//多条 比如查询来自from发给to的记录
/**
* mongoDB大于小于符号对应:
* > 大于 $gt
* < 小于 $lt
* >= 大于等于 $gte
* <= 小于等于 $lte
* 要查询同一个字段多个约束需要用andOperator:
*/
Query query2 = new Query(Criteria.where("from").is(from)
// //可累加条件 用and
.and("to").is(to)
// .andOperator(
// //大于这个时间
// Criteria.where("datetime").gt(new Date()),
// //小于这个时间
// Criteria.where("datetime").lt(new Date())
// )
//
)
//排序
.with(Sort.by(Sort.Order.asc("datetime")))
;
long count = mongoTemplate.count(query2, MongodbMessage.class);
System.out.println(count);
//分页
List<MongodbMessage> messages = mongoTemplate.find(query2.limit(size).skip((page - 1) * size), MongodbMessage.class);
HashMap<String, Object> hashMap = new HashMap<>(2);
hashMap.put("count", count);
hashMap.put("messages", messages);
return Result.OK(hashMap);
}
}