我正在尝试使用 Spring Boot 2.0.3 版连接两个不同的数据源。一个指向 MySQL,另一个指向 PostgreSQL。MySQL 通过了所有测试。但是PostgreSQL的问题。
这是应该映射到 PostgreSQL 的实体类。
@Entity
@Table(name = "order")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String productName;
}
这是 PostgreSQL 的配置类。
@Configuration
@EnableJpaRepositories(
basePackageClasses = Order.class,
entityManagerFactoryRef = "orderEntityManager",
transactionManagerRef = "orderTransactionManager"
)
@PropertySource("classpath:application.properties")
public class OrderConfig {
@Autowired
Environment env;
@Bean
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("order.datasource.class"));
dataSource.setUrl(env.getProperty("order.datasource.url"));
dataSource.setUsername(env.getProperty("order.datasource.username"));
dataSource.setPassword(env.getProperty("order.datasource.password"));
return dataSource;
}
@Bean(name = "orderEntityManager")
public LocalContainerEntityManagerFactoryBean getEntityManager() {
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean =
new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(this.getDataSource());
localContainerEntityManagerFactoryBean.setPackagesToScan("com.seal.multipledatasource.entity.order");
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(hibernateJpaVendorAdapter);
Map<String, String> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("order.jpa.hibernate.ddl-auto"));
properties.put("hibernate.dialect", env.getProperty("order.jpa.hibernate.dialect"));
localContainerEntityManagerFactoryBean.setJpaPropertyMap(properties);
}
相关分类