mongodb - spring-data-mongo 该如何连接到其他的库 而不是默认的test库?

浏览:47日期:2023-06-27

问题描述

我现在通过spring-boot + spring-mongo 做一个小demo ,都是最新版本 , 现在出现了一个问题就是无论如何都不能连接到其他的库 ,只能连接到mongodb默认的test库 , 是什么原因 ?

配置 application.yml:

spring.data.mongodb.uri: mongodb://localhost:27017/mongo

按理说这里应该连接到mongo库才对 。我的mongodb没有配置权限验证,及不需要密码和用户名

gradle :

compile('org.springframework.boot:spring-boot-starter-data-mongodb') compile('org.springframework.boot:spring-boot-starter-web')

entity :

@Document(collection='customer')public class Customer { @Id private String id; private String firstName; private String lastName;

repository :

public interface CustomerRepository extends MongoRepository<Customer, String>,QueryDslPredicateExecutor<Customer> {}

controller :

//http://localhost:8080/id @RequestMapping('/{id}') public Object getmongodbID(@PathVariable String id){ return customerRepository.getIdCustomer(id); }

然后访问拿到的内容是test库里面的 , 到底怎么回事 ?

问题解答

回答1:

在你的application.yml或者prop文件里加上配置 data:

mongodb: database: tara host: localhost port: 27017 username: tara password: tara authentication-database: tara repositories: enabled: true

相关文章: