SpringBooot整合mongoDB
2020-09-29 本文已影响0人
黑质白章
1、导入jar包依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2、配置文件
这里配置的是无身份验证
#yml配置
data:
mongodb:
uri: mongodb://10.20.37.211:27017,10.20.37.212:27017,10.20.37.213:27017
database: signal
这里是有身份验证
mongodb://username:password//ip:port
@Getter
@Setter
@ToString
public class Students {
private String id ;
private String name ;
private String clazz ;
}
@Service
public class TestMongoService {
private MongoTemplate mongoTemplate ;
public TestMongoService(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
public String findAll(){
List<Students> list = mongoTemplate.findAll(Students.class,"students");
for (Students st : list){
System.out.println(st);
}
return list.toString() ;
}
1599668678075.png