这里有新鲜出炉的 MongoDB 教程, 程序狗速度看过来!
MongoDB 分布式文件存储的数据库
MongoDB 是一个基于分布式文件存储的数据库. 由 C++ 语言编写. 旨在为 web 应用提供可扩展的高性能数据存储解决方案.
本篇文章主要介绍了详解 springboot 整合 mongodb, 小编觉得挺不错的, 现在分享给大家, 也给大家做个参考. 一起跟随小编过来看看吧
这篇文章主要介绍 springboot 如何整合 MongoDB.
准备工作
安装 MongoDB
jdk 1.8
maven 3.0
idea
环境依赖
在 pom 文件引入 spring-boot-starter-data-mongodb 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
数据源配置
如果 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<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
写一个接口, 继承 MongoRepository, 这个接口有了几本的 CURD 的功能. 如果你想自定义一些查询, 比如根据 firstName 来查询, 获取根据 lastName 来查询, 只需要定义一个方法即可. 注意 firstName 严格按照存入的 mongodb 的字段对应. 在典型的 Java 的应用程序, 写这样一个接口的方法, 需要自己实现, 但是在 springboot 中, 你只需要按照格式写一个接口名和对应的参数就可以了, 因为 springboot 已经帮你实现了.
测试
@SpringBootApplication
public 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
来源: http://www.phperz.com/article/18/0130/357190.html