外网开放Mongodb及在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>View Code
(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
欢迎私聊互相学习。
标签:name,Mongodb4.0,Mongodb,age,id,mongodb,UserModel,public,SpringBoot 来源: https://www.cnblogs.com/zgq7/p/12336016.html