多数据源配置参考,jpa+springboot
1.依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring-boot-starter-data-jpa.version}</version>
</dependency>
2.配置
#数据源1
spring.datasource.1.jdbc-url = jdbc:mysql://ip:3306/my?charset=utf8mb4&useSSL=false&serverTimezone=CTT&zeroDateTimeBehavior=convertToNull
spring.datasource.1.username = root
spring.datasource.1.password =
#数据源2
spring.datasource.2.jdbc-url = jdbc:mysql://ip:3306/user?charset=utf8mb4&useSSL=false&serverTimezone=CTT&zeroDateTimeBehavior=convertToNull
spring.datasource.2.username = root
spring.datasource.2.password =
#通用数据源配置
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
# Hikari 数据源专用配置
spring.datasource.hikari.maximum-pool-size = 20
spring.datasource.hikari.minimum-idle = 5
# JPA 相关配置
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.database=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
3.配置类
数据源配置类
@Configuration public class DataSourceConfig { @Autowired private JpaProperties jpaProperties; @Autowired private HibernateProperties hibernateProperties; @Bean(name = "DataSource1") @Primary @ConfigurationProperties("spring.datasource.1") public DataSource DataSource1() { return DataSourceBuilder.create().build(); } @Bean(name = "DataSource2") @ConfigurationProperties("spring.datasource.2") public DataSource DataSource2() { return DataSourceBuilder.create().build(); } @Bean(name = "vendorProperties") public Map<String, Object> getVendorProperties() { return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings()); } }
jpa配置类1
/** * @author: zhaobin * @date: 2021/8/11 14:41 * @description: open数据库连接配置 */ @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactory1", transactionManagerRef="transactionManager1", basePackages= { "com.my.dao" }) public class OpenJpaManagerConfig { @Autowired @Qualifier("DataSource1") private DataSource DataSource1; @Autowired @Qualifier("vendorProperties") private Map<String, Object> vendorProperties; @Bean(name = "entityManagerFactory1") @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactory1(EntityManagerFactoryBuilder builder) { return builder .dataSource(DataSource1) .properties(vendorProperties) .packages("com.my.dto") .persistenceUnit("PersistenceUnit1") .build(); } @Bean(name = "entityManager1") @Primary public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactory1(builder).getObject().createEntityManager(); } @Bean(name = "transactionManager1") @Primary PlatformTransactionManager transactionManagerOpen(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactory1(builder).getObject()); } }
jpa配置类2
/** * @author: zhaobin * @date: 2022/8/22 24:42 * @description: open数据库连接配置 */ @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactory2", transactionManagerRef="transactionManager2", basePackages= { "com.my2.dao" }) public class OpenJpaManagerConfig { @Autowired @Qualifier("DataSource2") private DataSource DataSource2; @Autowired @Qualifier("vendorProperties") private Map<String, Object> vendorProperties; @Bean(name = "entityManagerFactory2") @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactory2(EntityManagerFactoryBuilder builder) { return builder .dataSource(DataSource2) .properties(vendorProperties) .packages("com.my2.dto") .persistenceUnit("PersistenceUnit2") .build(); } @Bean(name = "entityManager2") @Primary public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactory2(builder).getObject().createEntityManager(); } @Bean(name = "transactionManager2") @Primary PlatformTransactionManager transactionManagerOpen(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactory2(builder).getObject()); } }
4.Dao层的写法
package com.my1.dao
@Transactional//不加此注解,进行修改的时候会报错,jpa修改数据必须加事务 public interface My1Dao extends JpaRepository<My1, String>, CrudRepository<My1, String> { @Modifying @Query("update 表1 t set t.status = :status, t.successCount = :successNum where t.jobId = :Id") int update1(int status, int successNum, String Id); }
5.pojo的写法
package com.my1.dto;
@Data @Entity(name = "My1") @Table(name = "My1") public class My1 { @Id //主键id @GeneratedValue(strategy = GenerationType.IDENTITY)//主键生成策略 @Column(name = "id")//数据库字段名 private String id; @Column(name = "status") private String status; @Column(name = "success_num") private String successNum; }
6.注意事项:
配置类扫描的包不要写错,一个数据源对应一个dao和一个pojo,多个数据源扫描的包要注意拆分开