一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解springboot整合mongodb

詳解springboot整合mongodb

2020-09-24 15:46方志朋 Java教程

本篇文章主要介紹了詳解springboot整合mongodb,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

準備工作

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

環境依賴

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

?
1
2
3
4
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

數據源配置

如果mongodb端口是默認端口,并且沒有設置密碼,可不配置,sprinboot會開啟默認的。

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

mongodb設置了密碼,這樣配置:

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

定義一個簡單的實體

mongodb

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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層

?
1
2
3
4
5
6
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已經幫你實現了。

測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@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的應用程序,加入測試代碼。啟動程序,控制臺打印了:

?
1
2
3
4
5
6
7
8
9
10
11
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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/forezp/article/details/70941577

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 糖心在线观看网 | 色哟哟在线资源 | 久久精品视频在线看 | 亚洲天堂精品在线观看 | 91粉色视频在线导航 | 性欧美金发洋妞xxxxbbbb | blackedhd 18sex| 亚洲男人天堂 | 精品老司机在线视频香蕉 | 500福利第一导航 | 欧美日日操 | 好大好深好涨好烫还要 | 天堂a视频 | 国产精品久线观看视频 | 久久亚洲精品AV无码四区 | 五月天色网站 | 日本老头4569gay | 小舞同人18av黄漫网站 | 99自拍视频在线观看 | 天天中文 | 亚洲成A人片在线观看中文L | 午夜尤物 | 国产精品久久久久久吹潮 | 草莓永久地域网名入2022 | 成人18视频在线观看 | 男人亚洲天堂 | 五月色天在线视频综合观看 | 精品一久久香蕉国产线看播放 | 天天白天天谢天天啦 | 99久久精品免费看国产一区二区 | 亚洲人成网站在线观看青青 | 欧美一级视频在线观看 | 精品高潮呻吟99AV无码 | 花房乱爱在线观看 | 色综合视频在线观看 | 视频亚洲一区 | 色综合色狠狠天天综合色 | 青草影院在线观看 | 久久AV国产麻豆HD真实 | 火影小南被爆羞羞网站 | 亚洲第一区欧美日韩精品 |