业务场景介绍:需要将业务库数据定时同步至金融应急中心做数据同步
版本介绍:
JDK:1.8
SpringBoot:2.1.4
MySql:5.7
1.pom.xml
2.application.properties
3.DataSource
@Configuration
public class DataSourceConfig{
@Bean(name ="primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSourceprimaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name ="secondaryDataSource")
@Qualifier("secondaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSourcesecondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
4.1数据源primary对应jpa配置
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
basePackages={"com.hudai.ms.dao.p"})
public class PrimaryConfig{
@Autowired
@Qualifier("primaryDataSource")
private DataSourceprimaryDataSource;
@Primary
@Bean(name ="entityManagerPrimary")
public EntityManagerentityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Primary
@Bean(name ="entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBeanentityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource)
.packages("com.hudai.ms.entity.p")
.persistenceUnit("primaryPersistenceUnit")
.build();
}
@Primary
@Bean(name ="transactionManagerPrimary")
public PlatformTransactionManagertransactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
4.2数据源secondary对应jpa配置
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackages={"com.hudai.ms.dao.s"})
public class SecondaryConfig{
@Autowired @Qualifier("secondaryDataSource")
private DataSourcesecondaryDataSource;
@Bean(name ="entityManagerSecondary")
public EntityManagerentityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name ="entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBeanentityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDataSource)
.packages("com.hudai.ms.entity.s")
.persistenceUnit("secondaryPersistenceUnit")
.build();
}
@Bean(name ="transactionManagerSecondary")
PlatformTransactionManagertransactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
4.primary-reposity
public interface UserRepositoryextends JpaRepository {
}
secondary-reposity
public interface CkUserRepositoryextends JpaRepository{
}
5.entity自行创建
6.开始test
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class HxIfcertApiApplicationTests{
@Autowired
private UserRepositoryuserRepository;
@Autowired
private CkUserRepositoryckUserRepository;
@Test
public void testOne() {
Optional<DkUser> user=userRepository.findById(116270);
log.info("uid:{},username:{}",user.get().getUid(),user.get().getUsername());
CkUser ckUser=new CkUser();
ckUser.setUsername(user.get().getUsername());
ckUser.setAddtime(LocalDateTime.now().toString());
ckUserRepository.save(ckUser);
}
}