SpringBoot教程 | 第八篇: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’]

---------------------

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • MongoDB 一、安装 Windows下,下载安装包并安装 在安装盘的根目录下,新建data文件夹,里面新建lo...
    Plissmile阅读 10,794评论 0 4
  • 原链接:http://www.cnblogs.com/langtianya/p/3757993.html JDK各...
    把爱放下会走更远阅读 4,785评论 0 10
  • 大部分的学霸有的特点: 善于总结,提炼 善于提问,有好奇心,有求知欲 有目的性 善于抓重点,把握联系,发现本质 有...
    Purelife阅读 3,602评论 4 5
  • 天气凉爽,中午就拉着儿子花园跑步,楼下玩耍的小雅拉着我不让走。好吧,还是陪孩子更重要。只能回家做了几节垫上运动。 ...
    王悦yue阅读 1,310评论 0 4
  • 在茫茫人海中,有些人震撼了你的眼,而有些人却震撼了你的心,让你的心灵久久不能平静,有些事刷新了你的三观,,坐在列车...
    笑呵呵_ed3b阅读 1,369评论 0 1

友情链接更多精彩内容