当前位置: 首页 > news >正文

Java springcloud B2B2C o2o多用户商城 springcloud架构(八)springboot整合mongodb

准备工作

  • 安装MongoDB
  • jdk 1.8
  • maven 3.0
  • 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);
}
}

Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六 


转载于:https://juejin.im/post/5c91eef46fb9a07102503e4b

相关文章:

  • 扩展资源服务器解决oauth2 性能瓶颈
  • 云端服务器与传统的独立主机服务器有什么区别
  • 中台之上(十三):探讨支持组装式开发的业务架构设计方法
  • 阿里CTO:阿里所有技术和产品输出都将必须通过阿里云进行
  • python -迭代器与生成器 以及 iterable(可迭代对象)、yield语句
  • LeetCode.914 卡牌分组
  • 本周学习小结(25/03 - 31/03)
  • Beetl 提供俩种方式来显示实现局部更新
  • core_framework —— 基于libev的轻量级lua网络开发框架
  • MyCAT水平分库
  • 织梦CMS模板中dede标签使用php和if判断语句的方法
  • 2019第十四届中国竞争情报国际年会将于4月在上海召开
  • 移动互联网下半场,iOS开发者如何“高薪”成长?
  • 判断是手机端还是PC端的代码
  • linux分区方案
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • extjs4学习之配置
  • javascript 哈希表
  • JS变量作用域
  • Laravel核心解读--Facades
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • Otto开发初探——微服务依赖管理新利器
  • React-redux的原理以及使用
  • spring-boot List转Page
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 编写符合Python风格的对象
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 浮动相关
  • 入门到放弃node系列之Hello Word篇
  • 使用API自动生成工具优化前端工作流
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 微信小程序设置上一页数据
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 自制字幕遮挡器
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • # .NET Framework中使用命名管道进行进程间通信
  • #14vue3生成表单并跳转到外部地址的方式
  • #if 1...#endif
  • #图像处理
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (附源码)ssm学生管理系统 毕业设计 141543
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • .CSS-hover 的解释
  • .NET 5种线程安全集合
  • .net6+aspose.words导出word并转pdf
  • @vue/cli 3.x+引入jQuery
  • [ Linux ] Linux信号概述 信号的产生
  • [14]内置对象
  • [23] GaussianAvatars: Photorealistic Head Avatars with Rigged 3D Gaussians
  • [Arduino学习] ESP8266读取DHT11数字温湿度传感器数据