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

 U友50096560_359 发布于 2022-10-26 10:10

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

1 个回答
  • 在你的application.yml或者prop文件里加上配置
    data:

    mongodb:
      database: tara
      host: localhost
      port: 27017
      username: tara
      password: tara
      authentication-database: tara
      repositories:
        enabled: true
    2022-10-27 00:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有