精品专区-精品自拍9-精品自拍三级乱伦-精品自拍视频-精品自拍视频曝光-精品自拍小视频

網(wǎng)站建設(shè)資訊

NEWS

網(wǎng)站建設(shè)資訊

springboot如何進(jìn)行整合mongodb

springboot如何進(jìn)行整合MongoDB,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)客戶idc服務(wù)中心,提供資陽移動(dòng)機(jī)房、成都服務(wù)器、成都主機(jī)托管、成都雙線服務(wù)器等業(yè)務(wù)的一站式服務(wù)。通過各地的服務(wù)中心,我們向成都用戶提供優(yōu)質(zhì)廉價(jià)的產(chǎn)品以及開放、透明、穩(wěn)定、高性價(jià)比的服務(wù),資深網(wǎng)絡(luò)工程師在機(jī)房提供7*24小時(shí)標(biāo)準(zhǔn)級(jí)技術(shù)保障。

準(zhǔn)備工作

  • 安裝 MongoDB

  • jdk 1.8

  • maven 3.0

  • idea

環(huán)境依賴

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


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

數(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è)簡(jiǎn)單的實(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 {

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

}

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

測(cè)試

@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)用程序,加入測(cè)試代碼。啟動(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']

測(cè)試通過。

關(guān)于springboot如何進(jìn)行整合mongodb問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。


本文題目:springboot如何進(jìn)行整合mongodb
URL地址:http://m.jcarcd.cn/article/ipdigg.html
主站蜘蛛池模板: 国产精品欧美专区 | 91福利一区二区 | 国产女主播精品 | 日韩卡1 | 国自产拍在线视频天 | 精品成人欧美大片 | 日韩欧美字 | 青青草色导航 | 日韩在线二区全免费 | 亚洲无码久久影视 | 日韩A免费| 国产精品手 | 天美麻花星空视 | 国产偷国产偷精 | 欧美午夜网 | 青青青在线播 | 欧美日韩成人在看 | www.成人导航 | 精品国产午夜肉伦 | 国产一区欧美二区 | 福利视屏网站 | 欧美日韩国产第一页 | 欧洲番茄影院 | 日韩在线视频二 | 成人精品网 | 乱婬真视频 | 国内自拍视频97 | 国产91影院 | 99热在线国产 | 欧美在线一区二区 | 91制片一二三 | www.91色| 精品伊人| 福利一区二区在线 | 国产欧美日韩一级 | 国产亚洲精品在天 | 福利小视频在线 | 午夜影院| 国产一区二区免费 | 中文字幕国产 | 中文字幕第23页 |