开发者社区> 问答> 正文

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

我现在通过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库里面的 , 到底怎么回事 ?

展开
收起
蛮大人123 2016-06-15 15:19:46 2506 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

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

    mongodb:
      database: tara
      host: localhost
      port: 27017
      username: tara
      password: tara
      authentication-database: tara
      repositories:
        enabled: true
    2019-07-17 19:39:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多