博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java springcloud B2B2C o2o多用户商城 springcloud架构(八)springboot整合mongodb
阅读量:7234 次
发布时间:2019-06-29

本文共 2795 字,大约阅读时间需要 9 分钟。

准备工作

  • 安装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

你可能感兴趣的文章
sizeof()解析(原)
查看>>
poj 2226 Muddy Fields(合理建图+二分匹配)
查看>>
[C++再学习系列] 变量与声明时初始化
查看>>
使用DNSCrypt解决DNS污染问题
查看>>
bootstrap-进度条
查看>>
Format、FormatDateTime、FormatFloat 的用法
查看>>
[J2EE]java本地文件操作
查看>>
springCloud(2):Spirng Cloud简介
查看>>
js学习笔记
查看>>
Python 深浅拷贝
查看>>
nfs服务
查看>>
Linux学习之CentOS(十一)--CentOS6.4下Samba服务器的安装与配置
查看>>
nagios监控插件 nagios_oracle_health+check_linux_stats.pl
查看>>
Linux测试时加速系统时间脚本
查看>>
实现 标砖库中【strcpy】【strcmp】【strcat】
查看>>
第四单元练习题
查看>>
Memcached常用操作
查看>>
Redis实现微博后台业务逻辑系列(七)
查看>>
CSS Reset
查看>>
Python图像处理常用操作技巧总结
查看>>