转载请标明出处:

原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot8-mongodb/

本文出自方志朋的博客

这篇文章主要介绍springboot如何整合mongodb。

准备工作

环境依赖

在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

参考资料

accessing-data-mongodb




扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 方志朋的博客

SpringBoot非官方教程 | 第八篇:springboot整合mongodb的更多相关文章

  1. SpringBoot进阶教程 | 第四篇:整合Mybatis实现多数据源

    这篇文章主要介绍,通过Spring Boot整合Mybatis后如何实现在一个工程中实现多数据源.同时可实现读写分离. 准备工作 环境: windows jdk 8 maven 3.0 IDEA 创建 ...

  2. (转)SpringBoot非官方教程 | 第七篇:springboot开启声明式事务

    springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经默认对jpa.jdbc.mybatis开启了事事务,引入它们依赖的时候,事物就 ...

  3. SpringBoot非官方教程 | 第二十三篇: 异步方法

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot-ansy/ 本文出自方志朋的博客 这篇文章主要介绍 ...

  4. (转)SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql

    本文介绍springboot通过jdbc访问关系型MySQL,通过spring的JdbcTemplate去访问. 准备工作 jdk 1.8 maven 3.0 idea mysql 初始化mysql: ...

  5. SpringBoot非官方教程 | 第六篇:springboot整合mybatis

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-mybatis/ 本文出自方志朋的博客 本文主要 ...

  6. SpringBoot非官方教程 | 第五篇:springboot整合 beatlsql

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot5-beatlsql/ 本文出自方志朋的博客 Be ...

  7. SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot4-jpaJ/ 本文出自方志朋的博客 JPA全称J ...

  8. SpringBoot非官方教程 | 第七篇:springboot开启声明式事务

    转载请标明出处: http://blog.csdn.net/forezp/article/details/70833629 本文出自方志朋的博客 springboot开启事务很简单,只需要一个注解@T ...

  9. SpringBoot非官方教程 | 第二十篇: 处理表单提交

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-form/ 本文出自方志朋的博客 这篇文件主要介 ...

随机推荐

  1. 两个三汇API使用的坑

    最近呼叫中心走火入魔了,我的<一步一步开发呼叫中心>系列编写过程中,遇到各种的问题,今天晚上,来记录一下纠结了我N久的一个问题: 内线通过板卡外呼时,如果对方的呼叫中心需要发送按键响应(如 ...

  2. (三)HTML中的列表标签、框架集及表单标签

    一.HTML的列表标签 在网页中,经常可以看到,有的内容排列如同word里面的项目编号,这就是HTML的无序排列和有序排列起到的作用.. HTML之无序排列:<ul></ul> ...

  3. Java设计模式—责任链模式

    责任链模式的定义: 使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止. 责任链模式的重点是在"链& ...

  4. Web安全入门笔记-XSS

    windows 10 360浏览器 0x00.概述 1.什么是 XSS Cross-Site Scripting(跨站脚本攻击)简称 XSS,是一种代码注入攻击.攻击者通过在目标网站上注入恶意脚本,使 ...

  5. Daemon 自更新

    NSTask *task = [[NSTask alloc] init]; [task setLaunchPath:@"usr/bin/dpkg"]; [task setArgum ...

  6. jscode属性排序

    根据data中的value 对geCoorMap 进行排序,暂定降序排序. var data = [{name:"name1",value:29},{name:"name ...

  7. [工作积累点滴整理]虚拟化、云计算配置规划<一>

    目 录1. 服务器虚拟化的相关配置建议 11.1. 服务器的基本配置建议 11.1.1. CPU配置 11.1.2. 服务器内存配置 21.1.3. 物理网卡配置 21.1.4. 服务器磁盘配置 21 ...

  8. SQL Server ->> 生成代码把表字段从NULL改为NOT NULL

    一般我们用SELECT .... INTO语句生成的表字段都是允许为NULL.而如果我们需要改成NOT NULL呢 select 'ALTER TABLE dbo.XXXXXXX ALTER COLU ...

  9. Win10下使用VSCode配置python运行环境

    VSCode配置python运行环境 安装python 到官网下载python,直接安装即可,在安装过程中可以选择将python加入环境变量 安装VSCode 官网下载,直接安装 配置 安装pytho ...

  10. March 4 2017 Week 10 Saturday

    There is more to life than increasing its speed. 生活不仅仅是匆匆赶路. I always think I have walked very slowl ...