我现在通过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,QueryDslPredicateExecutor { }
controller :
//http://localhost:8080/id @RequestMapping("/{id}") public Object getmongodbID(@PathVariable String id){ return customerRepository.getIdCustomer(id); }
然后访问拿到的内容是test库里面的 , 到底怎么回事 ?
在你的application.yml或者prop文件里加上配置
data:
mongodb: database: tara host: localhost port: 27017 username: tara password: tara authentication-database: tara repositories: enabled: true