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

查看:268
本文介绍了mongodb - 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库里面的 , 到底怎么回事 ?

解决方案

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

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

这篇关于mongodb - spring-data-mongo 该如何连接到其他的库 而不是默认的test库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆