SpringBoot整合Mongodb4.0
本品文章只做学习使用;
安装mongodb推荐博客:https://www.jianshu.com/p/a75e26e5f635
1:如何在外网环境下开放mongodb
服务器版本:centos7.6
(1) 保证mongodb的启动配置文件中使用了bind_ip=0.0.0.0

(2) 保证服务器开放了27017端口
(3) 保证云服务器的安全组中开放了27017
可参考我之前的博客:https://www.cnblogs.com/zgq7/p/11564860.html
自述:昨天我在学习的时候使用mongodb桌面工具robo 3T连接我的外网mongodb时可以稳定快速的连接,
但是在我的SpringBoot项目中却一直连接超时,经过重复的实验及思考后分析出了问题所在:
1:我的robo 3T 采用的是SSH连接,让我一直以为mongodb处于外网开放状态
2:我的云服务器没有开放27017端口
3:我的安全组也没有开放27017端口
彻底开放之后可以在服务器上做一下测试:

如出现上图则表示mongodb外网彻底放通了。
2:如何在SpringBoot中整合
我的版本:SpringBoot2.1.7+Java8+Mongodb 4.0
(1): springboot中的依赖
1 <dependencies>
2 <dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter</artifactId>
5 </dependency>
6
7 <dependency>
8 <groupId>org.springframework.boot</groupId>
9 <artifactId>spring-boot-starter-web</artifactId>
10 </dependency>
11
12 <dependency>
13 <groupId>org.springframework.boot</groupId>
14 <artifactId>spring-boot-starter-data-mongodb</artifactId>
15 </dependency>
16
17 <dependency>
18 <groupId>org.springframework.boot</groupId>
19 <artifactId>spring-boot-starter-test</artifactId>
20 <scope>test</scope>
21 </dependency>
22
23 </dependencies>
(2): model
package com.boot.mongodb.model; import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "test")
public class UserModel { @Id
private Object id; private String name; private Integer age; private String from; public UserModel() {
} public UserModel(String name, Integer age) {
this.name = name;
this.age = age;
} public UserModel(Object id, String name, String from) {
this.id = id;
this.name = name;
this.from = from;
} public UserModel(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
} public UserModel(Object id, String name, Integer age, String from) {
this.id = id;
this.name = name;
this.age = age;
this.from = from;
} public Object getId() {
return id;
} public void setId(Object id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "UserModel{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", from='" + from + '\'' +
'}';
}
}
1:@Document(collection = "test") 代表该实体映射到该库的test集合
2:@ID 指该字段映射到test集合的ID
(3): 作为持久化有两种方式:
1:使用Repostory的方式
package com.boot.mongodb.repository; import com.boot.mongodb.model.UserModel;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository; @Repository
public interface UserRepository extends MongoRepository<UserModel, Integer> {
}
增删改查:
//==============================基于Repository的增删改查
@Test
public void add() {
UserModel userModel = new UserModel(1, "lzq", 23, "Repository");
System.out.println(userRepository.insert(userModel));
} @Test
public void remove() {
UserModel userModel = new UserModel(1, "lzq", 23, "Repository");
userRepository.delete(userModel);
} @Test
public void update() {
UserModel userModel = new UserModel(2, "lzq", 24, "Repository");
//有则修改,无则新增
System.out.println(userRepository.save(userModel));
} @Test
public void find() {
System.out.println(userRepository.findAll());
UserModel userModel = new UserModel(2, "lzq", 24, "Repository");
Example example = Example.of(userModel);
System.out.println(userRepository.findOne(example));
System.out.println(userRepository.findAll(example));
}
2:使用MongoTemplate的方式,不用创建对应的Repostroy
增删改查
//===============================基于MongoTemplate的增删改查
@Test
public void tadd() {
UserModel userModel = new UserModel(3, "lzq", 24, "MongoTemplate");
logger.info("{}", mongoTemplate.insert(userModel));
} @Test
public void tdelete() {
//UserModel userModel = new UserModel(3, "lzq", 24, "MongoTemplate");
//输出删除的行数
//System.out.println(mongoTemplate.remove(userModel).getDeletedCount());
Query query = new Query(Criteria.where("id").is(3));
logger.info("{}", mongoTemplate.remove(query, UserModel.class).getDeletedCount());
} @Test
public void tupdate() {
Query query = new Query(Criteria.where("id").is(3));
Update update = new Update().set("name", "2333333");
logger.info("{}", mongoTemplate.updateFirst(query, update, UserModel.class));
} @Test
public void tfind() {
Query query = new Query(Criteria.where("id").is(3));
logger.info("{}", mongoTemplate.find(query, UserModel.class));
}
3:完整项目github地址:https://github.com/zgq7/boot_collections/tree/master/boot_mongodb
欢迎私聊互相学习。
SpringBoot整合Mongodb4.0的更多相关文章
- SpringBoot整合Graylog3.0
Graylog简介 Graylog是一个开源的完整的日志管理工具,功能和ELK类似,安装部署更方便. 官方网站 https://www.graylog.org 搭建 使用docker快速搭建 参考 h ...
- springboot 整合 mybatis 入门
springboot整合mybatis 0.yml 配置文件 1.创建数据库表. 2.创建实体类. 3.创建 Mapper 接口 ,添加 @Mapper 注解. 4.创建 Mapper 映射文件. & ...
- solr8.0 springboot整合solr(四)
引言: solr搭建起后,就该应用到java后台开发里了,接下来就用springboot整合应用solr 一:引入jar包 <!--solr--> <dependency> & ...
- SpringBoot整合MyBatis与MySql8.0
一.前言 之前已经有一篇文章讨论过SpringBoot整合MyBatis,因而此篇不在重复累赘,本文主要是最新版的SpringBoot2.0与MyBatis.最新MySQL8.0整合过程中遇到的问题进 ...
- Springboot(2.0.0.RELEASE)+spark(2.1.0)框架整合到jar包成功发布(原创)!!!
一.前言 首先说明一下,这个框架的整合可能对大神来说十分容易,但是对我来说十分不易,踩了不少坑.虽然整合的时间不长,但是值得来纪念下!!!我个人开发工具比较喜欢IDEA,创建的springboot的j ...
- Activiti工作流学习之SpringBoot整合Activiti5.22.0实现在线设计器(二)
一.概述 网上有很多关于Eclipse.IDEA等IDE插件通过拖拽的方式来画工作流程图,个人觉得还是不够好,所以花点时间研究了一下Activiti在线设计器,并与SpringBoot整合. 二.实现 ...
- SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂)
SpringBoot第十一集:整合Swagger3.0与RESTful接口整合返回值(2020最新最易懂) 一,整合Swagger3.0 随着Spring Boot.Spring Cloud等微服务的 ...
- Springboot:SpringBoot2.0整合WebSocket,实现后端数据实时推送!
一.什么是WebSocket? B/S结构的软件项目中有时客户端需要实时的获得服务器消息,但默认HTTP协议只支持请求响应模式,这样做可以简化Web服务器,减少服务器的负担,加快响应速度,因为服务器不 ...
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
随机推荐
- MIT 6.S081 聊聊xv6中的文件系统(上)
前言 Lab一做一晚上,blog一写能写两天,比做Lab的时间还长( 这篇博文是半夜才写完的,本来打算写完后立刻发出来,但由于今天发现白天发博点击量会高点,就睡了一觉后才发(几十的点击量也是点击量啊T ...
- WPS Excel启用正则表达式
WPS Excel启用正则表达式 新建一个空白表格文件 进入VB编辑器 插入模块 工具-引用-勾选正则表达式 (Microsoft VBScript Regular Express 5.5) 复制代码 ...
- 给jekyll博客添加天气插件
layout: post title: 给博客添加天气插件 date: 2020-04-26 author: Dapenson header-img: img/post-bg-debug.png ca ...
- Codeforces Round #651 (Div. 2) C. Number Game (博弈,数学)
题意:对于正整数\(n\),每次可以选择使它变为\(n-1\)或者\(n/t\) (\(n\ mod\ t=0\)且\(t\)为奇数),当\(n=1\)时便不可以再取,问先手赢还是后手赢. 题解:首先 ...
- EFCore学习记录--数据访问技术人门
1.安装Microsoft.EntityFrameworkCore.Sqlite.Microsoft.EntityFrameworkCore.Tools包2.创建模型 数据库上下文模型:Bloggin ...
- leetcode一些细节
取数组中点时不要写 int mid = (left + right) // 2;,「这么写有一个问题:数值越界,例如left和right都是最大int,这么操作就越界了,在二分法中尤其需要注意!」 所 ...
- 微服务架构Day03-SpringBoot之web开发配置
概述 SpringBoot开发: 1.创建SpringBoot应用,选中需要的场景模块. 2.SpringBoot已经默认将场景模块配置好,只需要在配置文件中指定少量的配置(数据库地址,用户名,密码) ...
- c++大整数
这里不是必须用c++的话不推荐用c++大整数,py和java的支持要好得多. 大整数类 (非负) #include <iostream> #include <vector> ...
- python argparse (更新中)
action='store_true' 例如 parser.add_argument("--generate_text_embedding", action='store_true ...
- 鸟哥的linux私房菜——第十六章学习(程序管理与 SELinux 初探)
第十六章.程序管理与 SE Linux 初探 在 Linux 系统当中:"触发任何一个事件时,系统都会将他定义成为一个程序,并且给予这个程序一个 ID ,称为 PID,同时依据启发这个程序的 ...