SpringBoot非官方教程 | 第八篇:springboot整合mongodb
来源:互联网 发布:linux查看ftp用户权限 编辑:程序博客网 时间:2024/06/07 05:33
转载请标明出处:
http://blog.csdn.net/forezp/article/details/70941577
本文出自方志朋的博客
这篇文章主要介绍springboot如何整合MongoDB。
准备工作
- 安装 MongoDB
- jdk 1.8
- maven 3.0
- idea
环境依赖
在pom文件引入spring-boot-starter-data-mongodb依赖:
org.springframework.boot spring-boot-starter-data-mongodb
数据源配置
如果mongodb端口是默认端口,并且没有设置密码,可不配置,sprinboot会开启默认的。
spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db
mongodb设置了密码,这样配置:
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname
定义一个简单的实体
mongodb
package com.forezp.entity;import org.springframework.data.annotation.Id;public class Customer { @Id public String id; public String firstName; public String lastName; public Customer() {} public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return String.format( "Customer[id=%s, firstName='%s', lastName='%s']", id, firstName, lastName); }}
数据操作dao层
public interface CustomerRepository extends MongoRepository { public Customer findByFirstName(String firstName); public List findByLastName(String lastName);}
写一个接口,继承MongoRepository,这个接口有了几本的CURD的功能。如果你想自定义一些查询,比如根据firstName来查询,获取根据lastName来查询,只需要定义一个方法即可。注意firstName严格按照存入的mongodb的字段对应。在典型的Java的应用程序,写这样一个接口的方法,需要自己实现,但是在springboot中,你只需要按照格式写一个接口名和对应的参数就可以了,因为springboot已经帮你实现了。
测试
@SpringBootApplicationpublic class SpringbootMongodbApplication implements CommandLineRunner { @Autowired private CustomerRepository repository; public static void main(String[] args) { SpringApplication.run(SpringbootMongodbApplication.class, args); } @Override public void run(String... args) throws Exception { repository.deleteAll(); // save a couple of customers repository.save(new Customer("Alice", "Smith")); repository.save(new Customer("Bob", "Smith")); // fetch all customers System.out.println("Customers found with findAll():"); System.out.println("-------------------------------"); for (Customer customer : repository.findAll()) { System.out.println(customer); } System.out.println(); // fetch an individual customer System.out.println("Customer found with findByFirstName('Alice'):"); System.out.println("--------------------------------"); System.out.println(repository.findByFirstName("Alice")); System.out.println("Customers found with findByLastName('Smith'):"); System.out.println("--------------------------------"); for (Customer customer : repository.findByLastName("Smith")) { System.out.println(customer); } }
在springboot的应用程序,加入测试代码。启动程序,控制台打印了:
Customers found with findAll():
——————————-
Customer[id=58f880f589ffb696b8a6077e, firstName=’Alice’, lastName=’Smith’]
Customer[id=58f880f589ffb696b8a6077f, firstName=’Bob’, lastName=’Smith’]
Customer found with findByFirstName(‘Alice’):
——————————–
Customer[id=58f880f589ffb696b8a6077e, firstName=’Alice’, lastName=’Smith’]
Customers found with findByLastName(‘Smith’):
——————————–
Customer[id=58f880f589ffb696b8a6077e, firstName=’Alice’, lastName=’Smith’]
Customer[id=58f880f589ffb696b8a6077f, firstName=’Bob’, lastName=’Smith’]
测试通过。
源码下载:https://github.com/forezp/SpringBootLearning
参考资料
accessing-data-mongodb
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
- SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA
- SpringBoot非官方教程 | 第五篇:springboot整合 beatlsql
- SpringBoot非官方教程 | 第六篇:springboot整合mybatis
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ
- SpringBoot非官方教程 | 第二十四篇: springboot整合docker
- SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA
- SpringBoot非官方教程 | 第五篇:springboot整合 beatlsql
- SpringBoot非官方教程 | 第六篇:springboot整合mybatis
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ
- SpringBoot非官方教程 | 第二十四篇: springboot整合docker
- SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA
- SpringBoot非官方教程 | 第六篇:springboot整合mybatis
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- join查询要点整合
- Controller的使用
- 大多数的愤怒源于自己的无知——《Excel受保护视图》
- 463. Island Perimeter
- 图的遍历
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
- Shiro框架学习-身份验证
- Android 动画简单实现
- CF
- 【算法】将一维数组arr中的元素循环左移p个位置
- 数据库特性与隔离级别
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- 常用的免费英文电子书
- spring boot整合redis