简体   繁体   中英

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. So I learned that we define datasources on application.properties file using spring.

How I define two or more datasources on application.properties and how I chose the second datasource? If I try define a second datasource using spring.datasourceB.datasource.meta-datas the file show some flags saying unknown properties.

My current case I built a project like this:

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. How I achieve the same result using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM