简体   繁体   English

定义e如何在spring boot项目上的application.properties中选择第二个数据源

[英]How define e chose a second datasource from application.properties on spring boot project

I'm new with spring specification. 我是弹簧规格的新手。 I've worked with JEE for a long time. 我在JEE工作了很长时间。 So I learned that we define datasources on application.properties file using spring. 因此,我了解到我们是使用spring在application.properties文件上定义数据源的。

How I define two or more datasources on application.properties and how I chose the second datasource? 如何在application.properties上定义两个或多个数据源,以及如何选择第二个数据源? If I try define a second datasource using spring.datasourceB.datasource.meta-datas the file show some flags saying unknown properties. 如果我尝试使用spring.datasourceB.datasource.meta-datas定义第二个数据源,则该文件将显示一些标志,表明未知属性。

My current case I built a project like this: 我目前的情况是建立一个像这样的项目:

application.properties application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://xx.xx.xx.xx:3306/dbA?useSSL=false
spring.datasource.username=username
spring.datasource.password=password

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

spring.DSB.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

# Enable to auto identify each datasource
spring.jpa.database=default

My repository class look like this: 我的存储库类如下所示:

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

@Repository
public class ClassDaoDSA {

    @PersistenceContext
    private EntityManager manager;

    public List<T> Records() {

        String stmt = "SELECT d FROM Data d ORDER BY d.id DESC";

        return manager.createQuery(stmt).getResultList();
    }

}

When I've worked with JEE projects I could used @PersistenceUnit tag and chose my datasource by there. 当我处理JEE项目时,可以使用@PersistenceUnit标记并在那里选择我的数据源。 How I achieve the same result using spring boot? 我如何使用Spring Boot达到相同的结果?

The naming convention you use isn't compulsory, it just spares you the need for configuration. 您使用的命名约定不是强制性的,它只是使您无需进行配置。 But you can also use a configuration class. 但是您也可以使用配置类。 This is an example from one of my projects: 这是我的一个项目的示例:

@Configuration
@PropertySource("app.properties")
public class DataConfig {
    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        Resource config = new ClassPathResource("hibernate.cfg.xml");
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setConfigLocation(config);
        sessionFactory.setPackagesToScan(env.getProperty("myapp.entity.package"));
        sessionFactory.setDataSource(dataSource());
        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(env.getProperty("myapp.db.driver"));
        ds.setUrl(env.getProperty("myapp.db.url"));
        ds.setUsername(env.getProperty("myapp.db.username"));
        ds.setPassword(env.getProperty("myapp.db.password"));

        return ds;
    }
}

You can have different properties files and specify the one you want to use in the @PropertySource annotation, or you could have additional properties like myapp.prod.db.url and myapp.test.db.url and set up your data source as needed. 您可以具有不同的属性文件,并在@PropertySource批注中指定要使用的属性文件,也可以具有其他属性,例如myapp.prod.db.url和myapp.test.db.url,并根据需要设置数据源。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring application.properties中的第二个数据源用于测试? - Second datasource in spring application.properties for tests? Spring 引导项目未绑定来自 application.properties 的 @Value - Spring Boot project not binding @Value from application.properties 如何从 spring boot 项目的 application.properties 文件中获取属性值? - How to get property value from application.properties file on spring boot project? 来自application.properties的Spring Boot配置 - Spring Boot configuration from application.properties Spring Boot - 如何将 application.yml 属性定义为 application.properties - Spring Boot - How to define application.yml properties as application.properties 如何从Spring Boot中的application.properties中分配注释值? - How to assign annotation value from application.properties in Spring Boot? 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? 在Spring Boot中在application.properties文件中定义映射列表 - define list of map in application.properties file in spring boot Spring Boot - 配置文件(application.properties)为 HTTP 请求定义 baseUrl - Spring Boot - Profiles (application.properties) to define baseUrl for HTTP requests 如何将Spring Boot中的application.properties文件外部化为外部依赖JAR? - How to externalize application.properties files in Spring Boot to e.g. external dependent JAR?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM