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](https://my.oschina.net/u/570656):27017/dbname

定义一个简单的实体

mongodb

package com.forezp.entity;

import org.springframework.data.annotation.Id;


public class Customer {

    [@Id](https://my.oschina.net/u/3451001)
    public String id;

    public String firstName;
    public String lastName;

    public Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    [@Override](https://my.oschina.net/u/1162528)
    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

写在最后

欢迎关注喜欢、和点赞后续将推出更多的spring cloud教程,敬请期待。 欢迎关注我的微信公众号获取更多更全的学习资源,视频资料,技术干货! 欢迎扫码关注 公众号回复“学习”,拉你进程序员技术讨论群干货资源第一时间分享。

公众号回复“视频”,领取800GJava视频学习资源。 java学习全套820G资源

公众号回复“全栈”,领取1T前端Java产品经理微信小程序Python等资源合集大放送。 全栈资料接近1T资源

公众号回复“慕课”,领取1T慕课实战学习资源。 慕课实战大全1061G资源

公众号回复“实战”,领取750G项目实战学习资源。 前后端实战项目750实战资源

公众号回复“面试”,领取8G面试实战学习资源。 JAVA面试实战视频8G面试资源

https://blog.csdn.net/forezp/article/details/70341818 本文出自方志朋的博客

转载于:https://my.oschina.net/u/3529959/blog/3040100

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值