Recently, due to the requirements of the project, I need to modify the history code to add greenplum connection, but I encountered problems in operation:
I. Current Situation:
1. Mybatisplus has been used in the project to automatically generate SQL, which cannot be replaced
2. JdbcTemplate is used
Two, problems encountered in the process of transformation
1. At the beginning, the MVN package can only run on MAC IDEA, Windows IDEA, and MVN package.
2. Then, local IDEA can run normally, but an error occurs after MVN package is packaged.
Third, solve the problem
1. pom.xml
2. Configure multiple data sources
Data source configured for mysql’s Mapper
FIG. 1
Figure 2
3. The application.properties file is automatically configured
4. Launch class annotations
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
Copy the code
Fourth, explain
Figure 1 and Figure 2 show mysql and Greenplum data sources configured for the specified Mapper, respectively.
Focus on Figure 1
1. Since multiple data sources are configured and JdbcTemplate is used in the project, specify a dynamicDataSource or another data source for the JdbcTemplate. If the JdbcTemplate is not used, you can ignore it
@Bean
public JdbcTemplate jdbcTemplate(@Qualifier("dynamicDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "dynamicDataSource")
public DynamicDataSource DynamicDataSource(){
return new DynamicDataSource();
}
Copy the code
2. GlobalConfig
Since the previous code used a configuration file to configure the data source, the code mode is used to configure multiple data sources, so you need to load the configuration in the invocation mode shown in the code
3. MybatisSqlSessionFactoryBean
Because the project USES the mybatisplus, so when using code configuration of data sources, it is recommended to use MybatisSqlSessionFactoryBean create a data source, and configuration, according to “figure 1” can normal boot.
4. @SpringBootApplication( exclude = DataSourceAutoConfiguration.class)
Because the transformation of multiple data sources is equivalent to manually specifying the data sources, there is no need for automatic data source assembly