Hibernate5+Spring Boot连接多个数据库

l2osamch  于 2022-11-14  发布在  Spring
关注(0)|答案(2)|浏览(198)

我想使用Hibernate连接两个不同的数据库,并使用不同的会话工厂来操作数据
在下面的示例中,另一个数据源和LocalSessionFactoryBean将被添加到@Configuration类中,但是我如何为这两个会话工厂指定名称呢?

参考:

https://www.baeldung.com/hibernate-5-spring

@Configuration
@EnableTransactionManagement
public class HibernateConf {

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(
          {"com.baeldung.hibernate.bootstrap.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());

        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
        dataSource.setUsername("sa");
        dataSource.setPassword("sa");

        return dataSource;
    }

    @Bean
    public PlatformTransactionManager hibernateTransactionManager() {
        HibernateTransactionManager transactionManager
          = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    private final Properties hibernateProperties() {
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty(
          "hibernate.hbm2ddl.auto", "create-drop");
        hibernateProperties.setProperty(
          "hibernate.dialect", "org.hibernate.dialect.H2Dialect");

        return hibernateProperties;
    }
}

想要达到效果:

@Service
@Transactional
public class TestRepositoryImpl implements TestRepository{

    @Qualifier("connectionA")
    @Autowired
    private SessionFactory sessionFactory;

    @Qualifier("connectionB")
    @Autowired
    private SessionFactory sessionFactory;
...
...
}

另一个工程示例:

@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    public DataSource test1DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSource test2DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Autowired
    @Bean(name = "test1Connection")
    public NamedParameterJdbcTemplate test1JdbcTemplate(@Qualifier("test1DataSource") DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Autowired
    @Bean(name ="test2Connection")
    public NamedParameterJdbcTemplate test2JdbcTemplate(@Qualifier("test2DataSource") DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }
}

如有任何帮助,不胜感激!

eqqqjvef

eqqqjvef1#

如果您想用两个数据库实现Hibernate:使用如下所示的会话工厂。
但我建议使用Spring data JPA,它将提供更多的规范并且更容易理解。

public class Transfer {
    public static void main(String[] args) {
        // get the SessionFacotry
        SessionFactory connectionAFactory = HibernateConf.getSessionFactory();
        SessionFactory connectionBFactory= DataSourceConfig.getSessionFactory();
        // get The Session
        Session connectionASes = HibernateConf.getSession();
        Session connectionBSes = DataSourceConfig.getSession();
hgc7kmma

hgc7kmma2#

为此,您可以这样配置您的数据连接Bean:

@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    public DataSource test1DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSource test2DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Autowired
    @Bean(name = "test1Connection")
    public NamedParameterJdbcTemplate test1JdbcTemplate(@Qualifier("test1DataSource") DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Autowired
    @Bean(name ="test2Connection")
    public NamedParameterJdbcTemplate test2JdbcTemplate(@Qualifier("test2DataSource") DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }
}

之后,您可以通过在您的服务类中通过以下方式使用@QUALIFIER来分别自动绑定它们:

@Service
@Transactional
public class TestRepositoryImpl implements TestRepository{

    @Qualifier("test1Connection")
    @Autowired
    private SessionFactory sessionFactory1;

    @Qualifier("test2Connection")
    @Autowired
    private SessionFactory sessionFactory2;
...
...
}

相关问题