资讯专栏INFORMATION COLUMN

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

jaysun / 825人阅读

摘要:如果你想自定义一些查询,比如根据来查询,获取根据来查询,只需要定义一个方法即可。注意严格按照存入的的字段对应。测试在的应用程序,加入测试代码。启动程序,控制台打印了测试通过。

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

准备工作
安装 MongoDB
jdk 1.8
maven 3.0
idea
环境依赖

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


    org.springframework.boot
    spring-boot-starter-data-mongodb

数据源配置

如果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 {

    public Customer findByFirstName(String firstName);
    public List 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/Spr...

参考资料

accessing-data-mongodb

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70381.html

相关文章

  • 一起来学SpringBoot | 八篇:通用Mapper与分页插件的集成

    摘要:通用是为了解决使用中的基本操作,使用它可以很方便的进行开发,可以节省开发人员大量的时间。当该参数设置为时,时会查询第一页,超过总数时,会查询最后一页。 SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工...

    韩冰 评论0 收藏0
  • SpringBoot官方教程 | 第十八篇: 定时任务(Scheduling Tasks)

    摘要:构建工程创建一个工程,在它的程序入口加上开启调度任务。创建定时任务创建一个定时任务,每过在控制台打印当前时间。通过在方法上加注解,表明该方法是一个调度任务。 这篇文章将介绍怎么通过spring去做调度任务。 构建工程 创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务。 @SpringBootApplication @EnableSch...

    BingqiChen 评论0 收藏0
  • 两年了,我写了这些干货!

    摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...

    huayeluoliuhen 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<