使用Spring Boot配置MongoDB



我在配置了Spring Boot的项目中使用ReactiveMongo

服务

@Document(collection = "tbl_created_service")
public class Services {
@Id
private ObjectId objectId;
private Long id;
// Getters and setters
}

ServiceReactiveRepository

public interface ServiceReactiveRepository extends ReactiveMongoRepository<Services, String> 
{
}

服务服务

public interface ServicesService {
Long addNewService(Services services);
}

ServiceImpl

@Service
public class ServicesServiceImpl implements ServicesService {
@Autowired
ServiceReactiveRepository serviceReactiveRepository;
@Override
public Long addNewService(Services servicesTypeDto)  {
serviceReactiveRepository.save(servicesTypeDto);
return null;
}

application.yml

spring:
profiles:
active: dev
---
spring:
profiles: dev
data.mongodb:
host: 127.0.0.1
port: 27017
database: dev
---

主分类

@SpringBootApplication
public class PartnersApplication {
public static void main(String[] args) {
SpringApplication.run(PartnersApplication.class, args);
}
}

当我到达端点时,我可以看到数据正在绑定并到达ServicesServiceImpl.addNewService(),但不知何故,它没有在DB中创建新的集合(如果不存在(,即tbl_created_service

但是,如果我手动添加集合并点击服务,那么它将在"tbl_created_service"中创建一个新文档。

这样做正确吗?或者我错过了什么?

任何提示都是可观的:(

只需将其放在application.properties中即可spring.data.mongodb.uri=uri连接

当您使用MongoDb图谱时,这将起作用。只需创建集群并连接即可获得Java URI链接,然后将其放入您的属性中。当您运行应用程序时,它将自动生成在Entity类中声明的集合。

最新更新