当前位置: 首页 > news >正文

【MongoDB】Springboot中MongoDB简单使用

1. docker安装MongoDB

拉取镜像

docker pull mongo

创建容器

docker run -di --name mongo-service --restart=always -p 27017:27017 -v ~/data/mongodata:/data mongo

2. 导入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

3. 配置文件

application.yml

server:port: 9998
spring:data:mongodb:host: 192.168.200.130port: 27017database: leadnews-history

4. 实体类映射

package com.mongo.pojo;import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;import java.io.Serializable;
import java.util.Date;/*** <p>* 联想词表* </p>** @author zsx*/
@Data
@Document("ap_associate_words")
public class ApAssociateWords implements Serializable {private static final long serialVersionUID = 1L;private String id;/*** 联想词*/private String associateWords;/*** 创建时间*/private Date createdTime;}

5. 测试

package com.zsx.mongo.test;import com.zsx.mongo.MongoApplication;
import com.zsx.mongo.pojo.ApAssociateWords;
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.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.test.context.junit4.SpringRunner;import java.util.Date;
import java.util.List;@SpringBootTest(classes = MongoApplication.class)
@RunWith(SpringRunner.class)
public class MongoTest {@Autowiredprivate MongoTemplate mongoTemplate;//保存@Testpublic void saveTest(){/*for (int i = 0; i < 10; i++) {ApAssociateWords apAssociateWords = new ApAssociateWords();apAssociateWords.setAssociateWords("黑马头条");apAssociateWords.setCreatedTime(new Date());mongoTemplate.save(apAssociateWords);}*/ApAssociateWords apAssociateWords = new ApAssociateWords();apAssociateWords.setAssociateWords("黑马直播");apAssociateWords.setCreatedTime(new Date());mongoTemplate.save(apAssociateWords);}//查询一个@Testpublic void saveFindOne(){ApAssociateWords apAssociateWords = mongoTemplate.findById("60bd973eb0c1d430a71a7928", ApAssociateWords.class);System.out.println(apAssociateWords);}//条件查询@Testpublic void testQuery(){Query query = Query.query(Criteria.where("associateWords").is("黑马头条")).with(Sort.by(Sort.Direction.DESC,"createdTime"));//query.with(Sort.by(Sort.Direction.DESC,"createdTime"));  //按createdTime倒序排序//query.limit(userSearchDto.getPageSize());  //查询多少条List<ApAssociateWords> apAssociateWordsList = mongoTemplate.find(query, ApAssociateWords.class);System.out.println(apAssociateWordsList);}@Testpublic void testDel(){mongoTemplate.remove(Query.query(Criteria.where("associateWords").is("黑马头条")),ApAssociateWords.class);}
}

tips: 使用navicat Premium 15以上版本,可以连接MongoDB 查看数据

http://www.lryc.cn/news/153080.html

相关文章:

  • Python 面试:单元测试unit testing 使用pytest
  • 螺旋矩阵、旋转矩阵、矩阵Z字打印
  • Seaborn绘制热力图的子图
  • C++二级题目4
  • Tomcat 部署时 war 和 war exploded区别
  • Delphi IdTcpServer IdTcpClient 传输简单文本
  • 界面控件Telerik UI for WPF——Windows 11主题精简模式提升应用体验
  • PoseC3D 基于人体姿态的动作识别新范式
  • html2canvas 截图空白 或出现toDataURL‘ on ‘HTMLCanvasElement或img标签没截下来 的所有解决办法
  • Eclipse错误提示: Symbol ‘xxxx‘ could not be resolved
  • 基于Java的OA办公管理系统,Spring Boot框架,vue技术,mysql数据库,前台+后台,完美运行,有一万一千字论文。
  • 正则表达式(JAVA)
  • 264_BOOST中的Json库解析_BOOST_AUTO(itrpromodel, doc.FindMember(“productmodel“));
  • linux rpm 离线安装 nginx 自用,仅供参考
  • 第十二章 YOLO的部署实战篇(上篇)
  • 无涯教程-Android - List View函数
  • stable diffusion实践操作-重绘
  • C# 静态构造函数未执行 .net core框架
  • Java 复习笔记 - 学生管理系统篇
  • 【UIPickerView-UIDatePicker-应用程序对象 Objective-C语言】
  • 仿京东 项目笔记1
  • huggingface transformers库中LlamaForCausalLM
  • 04-过滤器和拦截器有什么区别?【Java面试题总结】
  • 如何用selenium或pyppeteer来启动多个AdsPower窗口
  • 京东店铺所有商品API接口数据
  • stm32之27.iic协议oled显示
  • paddle 1-高级
  • ChatGPT帮助高职院校学生实现个性化自适应学习与对话式学习
  • 如何通过python写接口自动化脚本对一个需要调用第三方支付的报名流程进行测试?
  • 将OSGB格式数据转换为3d tiles的格式