1. application.properties
#\u4E3B\u5E93\u914D\u7F6E spring.datasource.primary.url=jdbc:mysql://mysql.test.xxxxx.com:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.primary.username=root spring.datasource.primary.password=root spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.primary.type=com.zaxxer.hikari.HikariDataSource spring.datasource.secondary.url=jdbc:mysql://mysql.log.test.xxxx.com:3306/test_log?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.secondary.username=root spring.datasource.secondary.password=root spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.secondary.type=com.zaxxer.hikari.HikariDataSource
2. 配置类
2.1 datasource配置
import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; 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 com.zaxxer.hikari.HikariDataSource; @Configuration public class DataSourceConfig { @Bean @Qualifier("primaryDataSourceProperties") @Primary @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSourceProperties primaryDataSourceProperties() { return new DataSourceProperties(); } @Autowired @Qualifier("primaryDataSourceProperties") private DataSourceProperties primaryDataSourceProperties; @Bean @Qualifier("primaryDataSource") @Primary public DataSource primaryDataSource() { return primaryDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build(); } // @Bean @Qualifier("secondaryDataSourceProperties") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSourceProperties secondaryDataSourceProperties() { return new DataSourceProperties(); } @Autowired @Qualifier("secondaryDataSourceProperties") private DataSourceProperties secondaryDataSourceProperties; @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") public DataSource secondaryDataSource() { return secondaryDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build(); } }
2.2 主库配置
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; 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; /* * 主库的配置 */ @Configuration @MapperScan(basePackages = "com.xxxx.xxxx.mapper",sqlSessionFactoryRef = "primarySqlSessionFactory") public class DatabaseConfig { @Autowired @Qualifier("primaryDataSource") private DataSource primaryDataSource; @Bean("primarySqlSessionFactory") @Qualifier("primarySqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(primaryDataSource); return sessionFactoryBean.getObject(); } } // class DatabaseConfig
2.3 日志库配置
import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /* * 主库的配置 */ @Configuration @MapperScan(basePackages = "com.xxxx.xxxx.logmapper",sqlSessionFactoryRef = "secondarySqlSessionFactory") public class LogDatabaseConfig{ @Autowired @Qualifier("secondaryDataSource") private DataSource secondaryDataSource; @Bean(name = "secondarySqlSessionFactory") public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(secondaryDataSource); return sessionFactoryBean.getObject(); } } // class LogDatabaseConfig
3. 使用