日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

SpringBoot微服务 b2b2c 多用户商城系统(八)springboot整合mongodb

發布時間:2025/4/5 windows 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot微服务 b2b2c 多用户商城系统(八)springboot整合mongodb 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

準備工作

  • 安裝 MongoDB
  • jdk 1.8
  • maven 3.0
  • 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

定義一個簡單的實體

package com.forezp.entity;import org.springframework.data.annotation.Id;public class Customer {@Idpublic String id;public String firstName;public String lastName;public Customer() {}public Customer(String firstName, String lastName) {this.firstName = firstName;this.lastName = lastName;}@Overridepublic 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 {@Autowiredprivate CustomerRepository repository;public static void main(String[] args) {SpringApplication.run(SpringbootMongodbApplication.class, args);}@Overridepublic void run(String... args) throws Exception {repository.deleteAll();// save a couple of customersrepository.save(new Customer("Alice", "Smith"));repository.save(new Customer("Bob", "Smith"));// fetch all customersSystem.out.println("Customers found with findAll():");System.out.println("-------------------------------");for (Customer customer : repository.findAll()) {System.out.println(customer);}System.out.println();// fetch an individual customerSystem.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);}}

Spring Cloud大型企業分布式微服務云構建的B2B2C電子商務平臺源碼請加企鵝求求: 一零叁八七七四六貳六

轉載于:https://my.oschina.net/u/4045192/blog/3001020

總結

以上是生活随笔為你收集整理的SpringBoot微服务 b2b2c 多用户商城系统(八)springboot整合mongodb的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。