如何在 Spring-data 中更改/定义 Mongodb 的默认数据库? [英] How can I change/define default database of Mongodb in Spring-data?

查看:59
本文介绍了如何在 Spring-data 中更改/定义 Mongodb 的默认数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从 MongoRepository 扩展的接口.他们正在使用 mongodb 的默认数据库.我想为类定义数据库名称.

I have interfaces extends from MongoRepository. They are using default database of mongodb. I would like to define the database name for the classes.

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

我该如何定义它?

推荐答案

你只需要在 application.properties 文件中定义相应的 mongobd 属性,或者如果你想要 yml 语法然后在 application.yml.在 src/main/resources 下,application.properties 应该已经存在了.

You just need to define respective mongobd properties in application.properties file or if you want to yml syntax then define props in application.yml. Under src/main/resources, application.properties should be there already.

application.properties :

spring.data.mongodb.host=<hostname> 
spring.data.mongodb.port=27017 
spring.data.mongodb.database=<dbname>
spring.data.mongodb.username=<usernamr>
spring.data.mongodb.password=******

或者

application.yml :

spring: 
    data: 
        mongodb: 
            host: <hostname> 
            port: 27017 
            database: <dbname>
            username: <usernamr>
            password: ******

这篇关于如何在 Spring-data 中更改/定义 Mongodb 的默认数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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