spring Boot integrated multi data source (with paging)

According to the needs of the project, the front end needs to be provided with data from different databases. Now a variety of data sources are integ...

According to the needs of the project, the front end needs to be provided with data from different databases.
Now a variety of data sources are integrated:
Direct post code:

application.yml:

#Port number: server: port: 8091 ################Journal################## logging: level: com.automic.donge.dao: debug #druid related configuration? Servertimezone = UTC & useunicode = true & usejdbc complianttimezoneshift = true & uselegacydatetimecode = false spring: datasource: #druid related configuration primary: #filters for monitoring statistics interception filters: stat driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver url: com.alibaba.druid.pool.DruidDataSource name: ruarlwater #Database name #Basic attributes jdbc-url: jdbc:sqlserver://localhost:1433;DatabaseName=ruarl username: sa password: Saadmin654321 second: driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/aunge?serverTimezone=UTC&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false username: root password: root name: automic_donge

Custom configuration class

DataSourceConfig.java:
package com.automic.donge.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource; /** * @ClassName: DataSourceConfig * @Author: Kun * @Description: * @Date: 2019/11/2 14:01 * @Version: 1.0 */ @Configuration public class DataSourceConfig { @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @Primary @ConfigurationProperties(prefix="spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondDataSource") @Qualifier("secondDataSource") @ConfigurationProperties(prefix = "spring.datasource.second") // Prefix of corresponding property in application.properteis public DataSource secondDataSource() { return DataSourceBuilder.create().build(); } }
MybatisDbAConfig.java:
package com.automic.donge.config; import com.github.pagehelper.PageInterceptor; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; import java.util.Properties; /** * @ClassName: MybatisDbAConfig * @Author: Kun * @Description: * @Date: 2019/11/2 14:02 * @Version: 1.0 */ @Configuration @MapperScan(basePackages = {"com.automic.donge.dao.priDataSource"}, sqlSessionFactoryRef = "primarySqlSessionFactory") public class MybatisDbAConfig { public static final String MAPPER_LOCATION = "classpath:com/automic/donge/mapper/priDataSource/*.xml"; @Bean(name = "primarySqlSessionFactory") public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); // Use the master data source to connect to the master library factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/automic/donge/mapper/priDataSource/*.xml")); //Add PageHelper plug-in Interceptor interceptor = new PageInterceptor(); Properties properties = new Properties(); //data base properties.setProperty("helperDialect", "sqlserver2012"); //Whether to use parameter offset as PageNum properties.setProperty("offsetAsPageNum", "true"); //count query or not properties.setProperty("rowBoundsWithCount", "true"); //Page rationalization or not properties.setProperty("reasonable", "false"); interceptor.setProperties(properties); factoryBean.setPlugins(new Interceptor[] ); return factoryBean.getObject(); } //Configure declarative transaction manager @Bean(name = "primaryTransactionManager") @Primary public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "primarySqlSessionTemplate") @Primary public SqlSessionTemplate primarySqlSessionTemplate( @Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
MybatisDbBConfig.java:
package com.automic.donge.config; import com.github.pagehelper.PageInterceptor; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; import java.util.Properties; /** * @ClassName: MybatisDbBConfig * @Author: Kun * @Description: * @Date: 2019/11/2 14:05 * @Version: 1.0 */ @Configuration @MapperScan(basePackages = {"com.automic.donge.dao.secondDataSource"}, sqlSessionFactoryRef = "secondSqlSessionFactory") public class MybatisDbBConfig { public static final String MAPPER_LOCATION = "classpath:com/automic/donge/mapper/secondDataSource/*.xml"; @Bean(name = "secondSqlSessionFactory") public SqlSessionFactory thirdSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/automic/donge/mapper/secondDataSource/*.xml")); //Add PageHelper plug-in Interceptor interceptor = new PageInterceptor(); Properties properties = new Properties(); //data base properties.setProperty("helperDialect", "mysql"); //Whether to use parameter offset as PageNum properties.setProperty("offsetAsPageNum", "true"); //count query or not properties.setProperty("rowBoundsWithCount", "true"); //Page rationalization or not properties.setProperty("reasonable", "false"); interceptor.setProperties(properties); factoryBean.setPlugins(new Interceptor[] ); return factoryBean.getObject(); } @Bean(name = "secondaryTransactionManager") @Primary public PlatformTransactionManager thirdaryTransactionManager( @Qualifier("secondDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "secondSqlSessionTemplate") @Primary public SqlSessionTemplate thirdSqlSessionTemplate( @Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
At present, the basic multiple data sources have been configured. The next dao and mapper packages need to build their own data source packages, as shown in the figure below

Test now and run it perfectly.

If you have any questions, please discuss them in private

Mr. Mr. Dong Published 27 original articles, won praise and 20000 visitors+ Private letter follow

18 January 2020, 10:20 | Views: 7236

Add new comment

For adding a comment, please log in
or create account

0 comments