欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解springboot整合mongodb

程序员文章站 2024-02-26 13:48:34
这篇文章主要介绍springboot如何整合mongodb。 准备工作 安装 mongodb jdk 1.8 maven 3.0 idea...

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

准备工作

  1. 安装 mongodb
  2. jdk 1.8
  3. maven 3.0
  4. 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']

测试通过。

源码下载:https://github.com/forezp/springbootlearning

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。