1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Spring Boot 2.x 多数据源配置之 JPA 篇

Spring Boot 2.x 多数据源配置之 JPA 篇

时间:2021-01-09 15:00:59

相关推荐

Spring Boot 2.x 多数据源配置之 JPA 篇

场景假设:现有电商业务,商品和库存分别放在不同的库

配置数据库连接

app:datasource:first:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1/product?useSSL=falseusername: rootpassword: rootconfiguration:maximum-pool-size: 10second:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1/stock?useSSL=falseusername: rootpassword: rootconfiguration:maximum-pool-size: 10

添加配置类

FirstConfig

@Configuration@EnableJpaRepositories(basePackages = {"com.karonda.springboot2datasourcesjpa.dao.first"},// 1. dao 层所在的包entityManagerFactoryRef = "firstEntityManagerFactory",transactionManagerRef = "firstTransactionManager")@EnableTransactionManagementpublic class FirstConfig {@Bean@Primarypublic PlatformTransactionManager firstTransactionManager() {return new JpaTransactionManager(firstEntityManagerFactory().getObject());}@Bean@Primarypublic LocalContainerEntityManagerFactoryBean firstEntityManagerFactory() {HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();factoryBean.setDataSource(firstDataSource());factoryBean.setJpaVendorAdapter(jpaVendorAdapter);factoryBean.setJpaProperties(hibernateProperties());factoryBean.setPackagesToScan("com.karonda.springboot2datasourcesjpa.entity.first");// 2. 实体类所在的包return factoryBean;}@Bean@Primary@ConfigurationProperties("app.datasource.first")public DataSourceProperties firstDataSourceProperties() {return new DataSourceProperties();}@Bean@Primary@ConfigurationProperties("app.datasource.first.configuration")public DataSource firstDataSource() {return firstDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class) // 3. 可以显示指定连接池,也可以不显示指定;即此行代码可以注释掉.build();}private Properties hibernateProperties() {final Properties hibernateProperties = new Properties();hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");// hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create");return hibernateProperties;}}

SecondConfig

@Configuration@EnableJpaRepositories(basePackages = {"com.karonda.springboot2datasourcesjpa.dao.second"},entityManagerFactoryRef = "secondEntityManagerFactory",transactionManagerRef = "secondTransactionManager")@EnableTransactionManagementpublic class SecondConfig {@Beanpublic PlatformTransactionManager secondTransactionManager() {return new JpaTransactionManager(secondEntityManagerFactory().getObject());}@Beanpublic LocalContainerEntityManagerFactoryBean secondEntityManagerFactory() {HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();factoryBean.setDataSource(secondDataSource());factoryBean.setJpaVendorAdapter(jpaVendorAdapter);factoryBean.setJpaProperties(hibernateProperties());factoryBean.setPackagesToScan("com.karonda.springboot2datasourcesjpa.entity.second");return factoryBean;}@Bean@ConfigurationProperties("app.datasource.second")public DataSourceProperties secondDataSourceProperties() {return new DataSourceProperties();}@Bean@ConfigurationProperties("app.datasource.second.configuration")public DataSource secondDataSource() {return secondDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();}private Properties hibernateProperties() {final Properties hibernateProperties = new Properties();hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");// hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create");return hibernateProperties;}}

dao

public interface ProductDao extends JpaRepository<Product, Integer> {@Query(value = "select p from Product p where p.id = id")Product findById(@Param("id") int id);}

public interface StockDao extends JpaRepository<Stock, Integer> {@Query(value = "select s from Stock s where s.productId = productId")Stock findByProductId(@Param("productId") int productId);}

使用示例

@Componentpublic class Task {@Autowiredprivate ProductDao productDao;@Autowiredprivate StockDao stockDao;@Scheduled(cron = "0/5 * * * * ? ")public void job(){final int productId = 1;Product product = productDao.findById(productId);Stock stock = stockDao.findByProductId(productId);System.out.println("产品名称: " + product.getName() + ", 库存: " + stock.getStockCount());}}

注意事项

使用多数据源,其中一个配置类需要添加 @Primary 注解 (有且仅有一个配置类需要添加)在配置类中需要同时配置 dao 层和实体类所在的包

参考:

Configure Two DataSourcesspring+Jpa多数据源配置的方法示例

完整代码:GitHub

本人 C# 转 Java 的 newbie, 如有错误或不足欢迎指正,谢谢

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。