diff --git a/spring-boot-demo-multi-datasource-jpa/pom.xml b/spring-boot-demo-multi-datasource-jpa/pom.xml index 5b4a2fc..b689da9 100644 --- a/spring-boot-demo-multi-datasource-jpa/pom.xml +++ b/spring-boot-demo-multi-datasource-jpa/pom.xml @@ -25,7 +25,7 @@ org.springframework.boot - spring-boot-starter + spring-boot-starter-web @@ -33,6 +33,32 @@ spring-boot-starter-test test + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + mysql + mysql-connector-java + + + + cn.hutool + hutool-all + + + + com.google.guava + guava + + + + org.projectlombok + lombok + true + diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/PrimaryDataSourceConfig.java b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/PrimaryDataSourceConfig.java new file mode 100644 index 0000000..3d3825e --- /dev/null +++ b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/PrimaryDataSourceConfig.java @@ -0,0 +1,65 @@ +package com.xkcoding.multi.datasource.jpa.config; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.sql.DataSource; + +/** + *

+ * JPA多数据源配置 - 主数据源 + *

+ * + * @package: com.xkcoding.multi.datasource.jpa.config + * @description: JPA多数据源配置 - 主数据源 + * @author: yangkai.shen + * @date: Created in 2019-01-17 15:58 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +public class PrimaryDataSourceConfig { + + /** + * 扫描spring.datasource.primary开头的配置信息 + * + * @return 数据源配置信息 + */ + @Primary + @Bean(name = "primaryDataSourceProperties") + @ConfigurationProperties(prefix = "spring.datasource.primary") + public DataSourceProperties dataSourceProperties() { + return new DataSourceProperties(); + } + + /** + * 获取主库数据源对象 + * + * @param dataSourceProperties 注入名为primaryDataSourceProperties的bean + * @return 数据源对象 + */ + @Primary + @Bean(name = "primaryDataSource") + public DataSource dataSource(@Qualifier("primaryDataSourceProperties") DataSourceProperties dataSourceProperties) { + return dataSourceProperties.initializeDataSourceBuilder().build(); + } + + /** + * 该方法仅在需要使用JdbcTemplate对象时选用 + * + * @param dataSource 注入名为primaryDataSource的bean + * @return 数据源JdbcTemplate对象 + */ + @Primary + @Bean(name = "primaryJdbcTemplate") + public JdbcTemplate jdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + +} diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/PrimaryJpaConfig.java b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/PrimaryJpaConfig.java new file mode 100644 index 0000000..ba9fd5d --- /dev/null +++ b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/PrimaryJpaConfig.java @@ -0,0 +1,106 @@ +package com.xkcoding.multi.datasource.jpa.config; + +import com.zaxxer.hikari.HikariDataSource; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +/** + *

+ * JPA多数据源配置 - 主 JPA 配置 + *

+ * + * @package: com.xkcoding.multi.datasource.jpa.config + * @description: JPA多数据源配置 - 主 JPA 配置 + * @author: yangkai.shen + * @date: Created in 2019-01-17 16:54 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +@EnableTransactionManagement +@EnableJpaRepositories( + // repository包名 + basePackages = PrimaryJpaConfig.REPOSITORY_PACKAGE, + // 实体管理bean名称 + entityManagerFactoryRef = "primaryEntityManagerFactory", + // 事务管理bean名称 + transactionManagerRef = "primaryTransactionManager") +public class PrimaryJpaConfig { + static final String REPOSITORY_PACKAGE = "com.xkcoding.multi.datasource.jpa.repository.primary"; + private static final String ENTITY_PACKAGE = "com.xkcoding.multi.datasource.jpa.entity.primary"; + + + /** + * 扫描spring.jpa.primary开头的配置信息 + * + * @return jpa配置信息 + */ + @Primary + @Bean(name = "primaryJpaProperties") + @ConfigurationProperties(prefix = "spring.jpa.primary") + public JpaProperties jpaProperties() { + return new JpaProperties(); + } + + /** + * 获取主库实体管理工厂对象 + * + * @param primaryDataSource 注入名为primaryDataSource的数据源 + * @param jpaProperties 注入名为primaryJpaProperties的jpa配置信息 + * @param builder 注入EntityManagerFactoryBuilder + * @return 实体管理工厂对象 + */ + @Primary + @Bean(name = "primaryEntityManagerFactory") + public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource, @Qualifier("primaryJpaProperties") JpaProperties jpaProperties, EntityManagerFactoryBuilder builder) { + return builder + // 设置数据源 + .dataSource(primaryDataSource) + // 设置jpa配置 + .properties(jpaProperties.getProperties()) + // 设置实体包名 + .packages(ENTITY_PACKAGE) + // 设置持久化单元名,用于@PersistenceContext注解获取EntityManager时指定数据源 + .persistenceUnit("primaryPersistenceUnit").build(); + } + + /** + * 获取实体管理对象 + * + * @param factory 注入名为primaryEntityManagerFactory的bean + * @return 实体管理对象 + */ + @Primary + @Bean(name = "primaryEntityManager") + public EntityManager entityManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory factory) { + return factory.createEntityManager(); + } + + /** + * 获取主库事务管理对象 + * + * @param factory 注入名为primaryEntityManagerFactory的bean + * @return 事务管理对象 + */ + @Primary + @Bean(name = "primaryTransactionManager") + public PlatformTransactionManager transactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory factory) { + return new JpaTransactionManager(factory); + } + +} diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/SecondDataSourceConfig.java b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/SecondDataSourceConfig.java new file mode 100644 index 0000000..66afec3 --- /dev/null +++ b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/SecondDataSourceConfig.java @@ -0,0 +1,62 @@ +package com.xkcoding.multi.datasource.jpa.config; + +import com.zaxxer.hikari.HikariDataSource; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.sql.DataSource; + +/** + *

+ * JPA多数据源配置 - 次数据源 + *

+ * + * @package: com.xkcoding.multi.datasource.jpa.config + * @description: JPA多数据源配置 - 次数据源 + * @author: yangkai.shen + * @date: Created in 2019-01-17 15:58 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +public class SecondDataSourceConfig { + + /** + * 扫描spring.datasource.second开头的配置信息 + * + * @return 数据源配置信息 + */ + @Bean(name = "secondDataSourceProperties") + @ConfigurationProperties(prefix = "spring.datasource.second") + public DataSourceProperties dataSourceProperties() { + return new DataSourceProperties(); + } + + /** + * 获取主库数据源对象 + * + * @param dataSourceProperties 注入名为secondDataSourceProperties的bean + * @return 数据源对象 + */ + @Bean(name = "secondDataSource") + public DataSource dataSource(@Qualifier("secondDataSourceProperties") DataSourceProperties dataSourceProperties) { + return dataSourceProperties.initializeDataSourceBuilder().build(); + } + + /** + * 该方法仅在需要使用JdbcTemplate对象时选用 + * + * @param dataSource 注入名为secondDataSource的bean + * @return 数据源JdbcTemplate对象 + */ + @Bean(name = "secondJdbcTemplate") + public JdbcTemplate jdbcTemplate(@Qualifier("secondDataSource") DataSource dataSource) { + return new JdbcTemplate(dataSource); + } + +} diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/SecondJpaConfig.java b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/SecondJpaConfig.java new file mode 100644 index 0000000..1a4f1fe --- /dev/null +++ b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/SecondJpaConfig.java @@ -0,0 +1,100 @@ +package com.xkcoding.multi.datasource.jpa.config; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +/** + *

+ * JPA多数据源配置 - 次 JPA 配置 + *

+ * + * @package: com.xkcoding.multi.datasource.jpa.config + * @description: JPA多数据源配置 - 次 JPA 配置 + * @author: yangkai.shen + * @date: Created in 2019-01-17 16:54 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +@EnableTransactionManagement +@EnableJpaRepositories( + // repository包名 + basePackages = SecondJpaConfig.REPOSITORY_PACKAGE, + // 实体管理bean名称 + entityManagerFactoryRef = "secondEntityManagerFactory", + // 事务管理bean名称 + transactionManagerRef = "secondTransactionManager") +public class SecondJpaConfig { + static final String REPOSITORY_PACKAGE = "com.xkcoding.multi.datasource.jpa.repository.second"; + private static final String ENTITY_PACKAGE = "com.xkcoding.multi.datasource.jpa.entity.second"; + + + /** + * 扫描spring.jpa.second开头的配置信息 + * + * @return jpa配置信息 + */ + @Bean(name = "secondJpaProperties") + @ConfigurationProperties(prefix = "spring.jpa.second") + public JpaProperties jpaProperties() { + return new JpaProperties(); + } + + /** + * 获取主库实体管理工厂对象 + * + * @param secondDataSource 注入名为secondDataSource的数据源 + * @param jpaProperties 注入名为secondJpaProperties的jpa配置信息 + * @param builder 注入EntityManagerFactoryBuilder + * @return 实体管理工厂对象 + */ + @Bean(name = "secondEntityManagerFactory") + public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("secondDataSource") DataSource secondDataSource, @Qualifier("secondJpaProperties") JpaProperties jpaProperties, EntityManagerFactoryBuilder builder) { + return builder + // 设置数据源 + .dataSource(secondDataSource) + // 设置jpa配置 + .properties(jpaProperties.getProperties()) + // 设置实体包名 + .packages(ENTITY_PACKAGE) + // 设置持久化单元名,用于@PersistenceContext注解获取EntityManager时指定数据源 + .persistenceUnit("secondPersistenceUnit").build(); + } + + /** + * 获取实体管理对象 + * + * @param factory 注入名为secondEntityManagerFactory的bean + * @return 实体管理对象 + */ + @Bean(name = "secondEntityManager") + public EntityManager entityManager(@Qualifier("secondEntityManagerFactory") EntityManagerFactory factory) { + return factory.createEntityManager(); + } + + /** + * 获取主库事务管理对象 + * + * @param factory 注入名为secondEntityManagerFactory的bean + * @return 事务管理对象 + */ + @Bean(name = "secondTransactionManager") + public PlatformTransactionManager transactionManager(@Qualifier("secondEntityManagerFactory") EntityManagerFactory factory) { + return new JpaTransactionManager(factory); + } + +} diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/SnowflakeConfig.java b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/SnowflakeConfig.java new file mode 100644 index 0000000..c143ed0 --- /dev/null +++ b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/config/SnowflakeConfig.java @@ -0,0 +1,27 @@ +package com.xkcoding.multi.datasource.jpa.config; + +import cn.hutool.core.lang.Snowflake; +import cn.hutool.core.util.IdUtil; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + *

+ * 雪花算法生成器 + *

+ * + * @package: com.xkcoding.multi.datasource.jpa.config + * @description: 雪花算法生成器 + * @author: yangkai.shen + * @date: Created in 2019-01-18 15:50 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +public class SnowflakeConfig { + @Bean + public Snowflake snowflake(){ + return IdUtil.createSnowflake(1,1); + } +} diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/entity/primary/PrimaryMultiTable.java b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/entity/primary/PrimaryMultiTable.java new file mode 100644 index 0000000..414db78 --- /dev/null +++ b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/entity/primary/PrimaryMultiTable.java @@ -0,0 +1,44 @@ +package com.xkcoding.multi.datasource.jpa.entity.primary; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.GenericGenerator; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + *

+ * 多数据源测试表 + *

+ * + * @package: com.xkcoding.multi.datasource.jpa.entity.primary + * @description: 多数据源测试表 + * @author: yangkai.shen + * @date: Created in 2019-01-18 10:06 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@Entity +@Table(name = "multi_table") +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class PrimaryMultiTable { + /** + * 主键 + */ + @Id + private Long id; + + /** + * 名称 + */ + private String name; +} diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/entity/second/SecondMultiTable.java b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/entity/second/SecondMultiTable.java new file mode 100644 index 0000000..b9e152b --- /dev/null +++ b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/entity/second/SecondMultiTable.java @@ -0,0 +1,41 @@ +package com.xkcoding.multi.datasource.jpa.entity.second; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.GenericGenerator; + +import javax.persistence.*; + +/** + *

+ * 多数据源测试表 + *

+ * + * @package: com.xkcoding.multi.datasource.jpa.entity.second + * @description: 多数据源测试表 + * @author: yangkai.shen + * @date: Created in 2019-01-18 10:06 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@Entity +@Table(name = "multi_table") +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class SecondMultiTable { + /** + * 主键 + */ + @Id + private Long id; + + /** + * 名称 + */ + private String name; +} diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/repository/primary/PrimaryMultiTableRepository.java b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/repository/primary/PrimaryMultiTableRepository.java new file mode 100644 index 0000000..9c8fe05 --- /dev/null +++ b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/repository/primary/PrimaryMultiTableRepository.java @@ -0,0 +1,22 @@ +package com.xkcoding.multi.datasource.jpa.repository.primary; + +import com.xkcoding.multi.datasource.jpa.entity.primary.PrimaryMultiTable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + *

+ * 多数据源测试 repo + *

+ * + * @package: com.xkcoding.multi.datasource.jpa.repository.primary + * @description: 多数据源测试 repo + * @author: yangkai.shen + * @date: Created in 2019-01-18 10:11 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Repository +public interface PrimaryMultiTableRepository extends JpaRepository { +} diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/repository/second/SecondMultiTableRepository.java b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/repository/second/SecondMultiTableRepository.java new file mode 100644 index 0000000..5752f55 --- /dev/null +++ b/spring-boot-demo-multi-datasource-jpa/src/main/java/com/xkcoding/multi/datasource/jpa/repository/second/SecondMultiTableRepository.java @@ -0,0 +1,22 @@ +package com.xkcoding.multi.datasource.jpa.repository.second; + +import com.xkcoding.multi.datasource.jpa.entity.second.SecondMultiTable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + *

+ * 多数据源测试 repo + *

+ * + * @package: com.xkcoding.multi.datasource.jpa.repository.second + * @description: 多数据源测试 repo + * @author: yangkai.shen + * @date: Created in 2019-01-18 10:11 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Repository +public interface SecondMultiTableRepository extends JpaRepository { +} diff --git a/spring-boot-demo-multi-datasource-jpa/src/main/resources/application.yml b/spring-boot-demo-multi-datasource-jpa/src/main/resources/application.yml index e69de29..b6d23fe 100644 --- a/spring-boot-demo-multi-datasource-jpa/src/main/resources/application.yml +++ b/spring-boot-demo-multi-datasource-jpa/src/main/resources/application.yml @@ -0,0 +1,56 @@ +spring: + datasource: + primary: + url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 + username: root + password: root + driver-class-name: com.mysql.cj.jdbc.Driver + type: com.zaxxer.hikari.HikariDataSource + hikari: + minimum-idle: 5 + connection-test-query: SELECT 1 FROM DUAL + maximum-pool-size: 20 + auto-commit: true + idle-timeout: 30000 + pool-name: PrimaryHikariCP + max-lifetime: 60000 + connection-timeout: 30000 + second: + url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo-2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 + username: root + password: root + driver-class-name: com.mysql.cj.jdbc.Driver + type: com.zaxxer.hikari.HikariDataSource + hikari: + minimum-idle: 5 + connection-test-query: SELECT 1 FROM DUAL + maximum-pool-size: 20 + auto-commit: true + idle-timeout: 30000 + pool-name: SecondHikariCP + max-lifetime: 60000 + connection-timeout: 30000 + jpa: + primary: + show-sql: true + generate-ddl: true + hibernate: + ddl-auto: update + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL57InnoDBDialect + open-in-view: true + second: + show-sql: true + generate-ddl: true + hibernate: + ddl-auto: update + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL57InnoDBDialect + open-in-view: true +logging: + level: + com.xkcoding: debug + org.hibernate.SQL: debug + org.hibernate.type: trace \ No newline at end of file diff --git a/spring-boot-demo-multi-datasource-jpa/src/test/java/com/xkcoding/multi/datasource/jpa/SpringBootDemoMultiDatasourceJpaApplicationTests.java b/spring-boot-demo-multi-datasource-jpa/src/test/java/com/xkcoding/multi/datasource/jpa/SpringBootDemoMultiDatasourceJpaApplicationTests.java index cf05514..dd33ac9 100644 --- a/spring-boot-demo-multi-datasource-jpa/src/test/java/com/xkcoding/multi/datasource/jpa/SpringBootDemoMultiDatasourceJpaApplicationTests.java +++ b/spring-boot-demo-multi-datasource-jpa/src/test/java/com/xkcoding/multi/datasource/jpa/SpringBootDemoMultiDatasourceJpaApplicationTests.java @@ -1,16 +1,67 @@ package com.xkcoding.multi.datasource.jpa; +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.lang.Snowflake; +import com.xkcoding.multi.datasource.jpa.entity.primary.PrimaryMultiTable; +import com.xkcoding.multi.datasource.jpa.entity.second.SecondMultiTable; +import com.xkcoding.multi.datasource.jpa.repository.primary.PrimaryMultiTableRepository; +import com.xkcoding.multi.datasource.jpa.repository.second.SecondMultiTableRepository; +import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import java.util.List; + @RunWith(SpringRunner.class) @SpringBootTest +@Slf4j public class SpringBootDemoMultiDatasourceJpaApplicationTests { + @Autowired + private PrimaryMultiTableRepository primaryRepo; + @Autowired + private SecondMultiTableRepository secondRepo; + @Autowired + private Snowflake snowflake; + + @Test + public void testInsert() { + PrimaryMultiTable primary = new PrimaryMultiTable(snowflake.nextId(),"测试名称-1"); + primaryRepo.save(primary); + + SecondMultiTable second = new SecondMultiTable(); + BeanUtil.copyProperties(primary, second); + secondRepo.save(second); + } + + @Test + public void testUpdate() { + primaryRepo.findAll().forEach(primary -> { + primary.setName("修改后的"+primary.getName()); + primaryRepo.save(primary); + + SecondMultiTable second = new SecondMultiTable(); + BeanUtil.copyProperties(primary, second); + secondRepo.save(second); + }); + } @Test - public void contextLoads() { + public void testDelete() { + primaryRepo.deleteAll(); + + secondRepo.deleteAll(); + } + + @Test + public void testSelect() { + List primary = primaryRepo.findAll(); + log.info("【primary】= {}", primary); + + List second = secondRepo.findAll(); + log.info("【second】= {}", second); } }