SpringBoot整合MongoDB
来源:互联网 发布:网络推广教程视频 编辑:程序博客网 时间:2024/05/20 18:15
一直想学MongoDB,正好碰上项目需要,故花了一天时间去研究MongoDB,在这做个总结。
下载MongoDB
首先面对的就是下载MongoDB,首先想到的是到官网上去下载,但是官网不知道是什么原因,一点击下载就是空白页,虽然一开始还填了一些个人资料上去。
MongoDB官网下载
后面在网上找资料发现了各个版本的下载地址:
http://dl.mongodb.org/dl/linux/x86_64
http://dl.mongodb.org/dl/win32/x86_64
windows本地安装
我下载的是mongodb-win32-x86_64-2008plus-ssl-3.6.0-rc3-69-gefea76a.zip这个版本,然后解压到本地目录,再在环境变量path中配置好D:\mongodb\bin。在“D:\Mongodb”目录下新建“data”文件夹,它将会成为数据存放的根目录。
C:\Users\LX>d:d:\>cd d:\mongodb\bind:\mongodb\bin>mongod --dbpath d:\mongodb\data
然后新开cmd窗口,原有窗口勿关闭,如果窗口关闭则服务也关闭。
dos代码如下:
C:\Users\LX>d:D:\>cd d:\mongodb\bind:\mongodb\bin>mongo 127.0.0.1:27017
则可看到如下效果:
C:\Users\LX>d:D:\>cd d:\mongodb\bind:\mongodb\bin>mongo 127.0.0.1:27017MongoDB shell version v3.6.0-rc3-69-gefea76aconnecting to: mongodb://127.0.0.1:27017/testMongoDB server version: 3.6.0-rc3-69-gefea76aWelcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, see http://docs.mongodb.org/Questions? Try the support group http://groups.google.com/group/mongodb-userServer has startup warnings:2017-11-20T15:12:36.768+0800 I CONTROL [initandlisten]2017-11-20T15:12:36.769+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.2017-11-20T15:12:36.769+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.2017-11-20T15:12:36.770+0800 I CONTROL [initandlisten]2017-11-20T15:12:36.770+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.2017-11-20T15:12:36.771+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.2017-11-20T15:12:36.771+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP2017-11-20T15:12:36.772+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to2017-11-20T15:12:36.773+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the2017-11-20T15:12:36.774+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.2017-11-20T15:12:36.774+0800 I CONTROL [initandlisten]2017-11-20T15:12:36.775+0800 I CONTROL [initandlisten] Hotfix KB2731284 or later update is not installed, will zero-out data files.2017-11-20T15:12:36.776+0800 I CONTROL [initandlisten]2017-11-20T15:12:36.777+0800 I CONTROL [initandlisten] ** WARNING: The file system cache of this machine is configured to be greater than 40% of the total memory. This can lead to increased memory pressure and poor performance.2017-11-20T15:12:36.777+0800 I CONTROL [initandlisten] See http://dochub.mongodb.org/core/wt-windows-system-file-cache2017-11-20T15:12:36.778+0800 I CONTROL [initandlisten]2017-11-20T15:20:39.680+0800 E - [main] Error loading history file: FileOpenFailed: Unable to fopen() file C:\Users\LX/.dbshell: 系统找不到指定的文件。> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB> use adminswitched to db admin> show collectionssystem.version>
如上,则表示安装成功。
SpringBoot集成
新建一个SpringBoot项目,pom文件如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> </dependency>
yml配置文件添加mongodb配置如下:
spring: data: mongodb: uri: mongodb://localhost:27017 database: test
综上,springBoot就算是整合好MongoDB了,为验证整合成功,特写代码如下:
User.java(引用了 lombok,具体请百度)
@Data@AllArgsConstructor@ToStringpublic class User { @Id private Integer id; private String name; private Integer age;}
UserRepository.java
public interface UserRepository extends MongoRepository<User, String>{ User findByName(String name);}
UserService.java
@Repositorypublic interface UserService { void save(User user); User findByName(String name);}
UserServiceImpl.java
@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public void save(User user) { userRepository.save(user); } @Override public User findByName(String name) { return this.findByName(name); }}
Application.java
@SpringBootApplicationpublic class Application implements CommandLineRunner { @Autowired private UserRepository repository; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { repository.deleteAll(); repository.save(new User(1,"name1",19)); repository.save(new User(2,"name2",20)); System.out.println("User found with findAll():"); System.out.println("-------------------------------"); for (User user : repository.findAll()) { System.out.println(user); } System.out.println("------------------------"); System.out.println("User found with findByName('name1'):"); System.out.println("--------------------------------"); System.out.println(repository.findByName("name1")); }}
测试结果如下:
Customers found with findAll():-------------------------------User(id=1, name=name1, age=19)User(id=2, name=name2, age=20)------------------------Customer found with findByName('name1'):--------------------------------User(id=1, name=name1, age=19)
测试通过。
阅读全文
0 0
- springboot整合mongodb
- SpringBoot整合MongoDB
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
- 使用IDEA搭建SpringBoot项目且整合mongoDB和mysql
- SpringBoot 入门:整合MongoDB,做简单查找功能
- springboot学习笔记-3 整合redis&mongodb【转载】
- SpringBoot-MongoDB
- SpringBoot学习:SpringBoot整合mybatis
- SpringBoot整合Mybatis(分层整合)
- springboot副本集mongoDB
- SpringBoot -- 集成MongoDB
- SpringBoot -- 集成MongoDB
- springboot连接mongodb
- SpringBoot中集成MongoDB
- springboot+mongodb最简。。
- Springboot+MongoDB例子
- Less运算和函数
- 【Git】gitk图形界面中文乱码情况
- fastjson将java转换成json数据;指定日期格式,取消循环引用
- 幢库
- Spring in Action — 事务管理
- SpringBoot整合MongoDB
- There is a cycle in the hierarchy!
- unity shader学习笔记(七)——Unity中的基础纹理之遮罩纹理
- SVN的学习之路八(Eclipse安装SVN)
- tcpclient和tcplistener通信
- 详解 Too many open files
- 23种设计模式(三)
- 面试前到底该不该刷题以及面试前该如何准备
- FCN全卷积语义分割网络