前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot非官方教程 | 第八篇:springboot整合mongodb

SpringBoot非官方教程 | 第八篇:springboot整合mongodb

作者头像
方志朋
发布2017-12-29 15:45:17
1.2K0
发布2017-12-29 15:45:17
举报

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

准备工作

环境依赖

在pom文件引入spring-boot-starter-data-mongodb依赖:

代码语言:javascript
复制
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

数据源配置

如果mongodb端口是默认端口,并且没有设置密码,可不配置,sprinboot会开启默认的。

代码语言:javascript
复制
spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db

mongodb设置了密码,这样配置:

代码语言:javascript
复制
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname

定义一个简单的实体

mongodb

代码语言:javascript
复制
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层

代码语言:javascript
复制
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已经帮你实现了。

测试

代码语言:javascript
复制
@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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-04-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 准备工作
  • 环境依赖
  • 数据源配置
  • 定义一个简单的实体
  • 数据操作dao层
  • 测试
  • 参考资料
相关产品与服务
云数据库 MongoDB
腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档