资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用springboot如何实现对mongodb进行整合

使用springboot如何实现对MongoDB进行整合?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

成都创新互联公司是一家专业提供广平企业网站建设,专注与网站设计、做网站、H5高端网站建设、小程序制作等业务。10年已为广平众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

环境依赖

在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']

看完上述内容,你们掌握使用springboot如何实现对mongodb进行整合的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


名称栏目:使用springboot如何实现对mongodb进行整合
网站网址:http://cdkjz.cn/article/jcdpjg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220