欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解springboot整合mongodb

 更新時(shí)間:2017年05月04日 17:10:48   作者:方志朋  
本篇文章主要介紹了詳解springboot整合mongodb,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

這篇文章主要介紹springboot如何整合MongoDB。

準(zhǔn)備工作

  1. 安裝 MongoDB
  2. jdk 1.8
  3. maven 3.0
  4. idea

環(huán)境依賴

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

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

數(shù)據(jù)源配置

如果mongodb端口是默認(rèn)端口,并且沒有設(shè)置密碼,可不配置,sprinboot會(huì)開啟默認(rèn)的。

spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db

mongodb設(shè)置了密碼,這樣配置:

spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname

定義一個(gè)簡單的實(shí)體

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);
  }

}

數(shù)據(jù)操作dao層

public interface CustomerRepository extends MongoRepository<Customer, String> {

  public Customer findByFirstName(String firstName);
  public List<Customer> findByLastName(String lastName);

}

寫一個(gè)接口,繼承MongoRepository,這個(gè)接口有了幾本的CURD的功能。如果你想自定義一些查詢,比如根據(jù)firstName來查詢,獲取根據(jù)lastName來查詢,只需要定義一個(gè)方法即可。注意firstName嚴(yán)格按照存入的mongodb的字段對應(yīng)。在典型的Java的應(yīng)用程序,寫這樣一個(gè)接口的方法,需要自己實(shí)現(xiàn),但是在springboot中,你只需要按照格式寫一個(gè)接口名和對應(yīng)的參數(shù)就可以了,因?yàn)閟pringboot已經(jīng)幫你實(shí)現(xiàn)了。

測試

@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的應(yīng)用程序,加入測試代碼。啟動(dòng)程序,控制臺(tái)打印了:

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問題詳解

    SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問題詳解

    這篇文章主要介紹了SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問題詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • java結(jié)束進(jìn)程的實(shí)例代碼

    java結(jié)束進(jìn)程的實(shí)例代碼

    java結(jié)束程序進(jìn)程的方法很簡單,只要一句代碼就行,大家參考使用吧
    2013-12-12
  • 詳解Spring如何更簡單的讀取和存儲(chǔ)對象

    詳解Spring如何更簡單的讀取和存儲(chǔ)對象

    這篇文章主要為大家詳細(xì)介紹了Spring中如何更簡單的實(shí)現(xiàn)讀取和存儲(chǔ)對象,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-07-07
  • java遞歸與非遞歸實(shí)現(xiàn)掃描文件夾下所有文件

    java遞歸與非遞歸實(shí)現(xiàn)掃描文件夾下所有文件

    這篇文章主要為大家詳細(xì)介紹了java遞歸與非遞歸實(shí)現(xiàn)掃描文件夾下所有文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • java poi判斷excel是xlsx還是xls類型

    java poi判斷excel是xlsx還是xls類型

    這篇文章主要為大家詳細(xì)介紹了如何利用java poi來判斷excel是xlsx還是xls類型,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下
    2024-10-10
  • SpringBoot之QueryDsl嵌套子查詢問題

    SpringBoot之QueryDsl嵌套子查詢問題

    這篇文章主要介紹了SpringBoot之QueryDsl嵌套子查詢問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java如何通過FileOutputStream字節(jié)流向文件中寫數(shù)據(jù)

    java如何通過FileOutputStream字節(jié)流向文件中寫數(shù)據(jù)

    這篇文章主要介紹了java如何通過FileOutputStream字節(jié)流向文件中寫數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Spring Boot Admin 快速入門詳解

    Spring Boot Admin 快速入門詳解

    這篇文章主要介紹了SpringBoot Admin 使用指南(推薦),Spring Boot Admin 是一個(gè)管理和監(jiān)控你的 Spring Boot 應(yīng)用程序的應(yīng)用程序,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2021-11-11
  • spring @Transactional 無效的解決方案

    spring @Transactional 無效的解決方案

    這篇文章主要介紹了spring @Transactional 無效的解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • IDEA?2022?中的Lombok?使用基礎(chǔ)教程

    IDEA?2022?中的Lombok?使用基礎(chǔ)教程

    ? Lombok是使用java編寫的一款開源類庫。其主作用是使用注解來代替一些具有格式固定,沒有過多技術(shù)含量的編碼工作,這篇文章主要介紹了IDEA?2022?中的Lombok?使用基礎(chǔ)教程,需要的朋友可以參考下
    2022-12-12

最新評論