1、编写配置文件,我是在.properties中配置得,也可在.yml中配置
### datasource1 读我们本地库
spring.datasource.db1.jdbc-url=jdbc:mysql://172.25.0.98:3306/sz_train_platform?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.db1.username=root
spring.datasource.db1.password=123456
spring.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db1.druid.time-between-eviction-runs-millis=3600000
### datasource1 写监控库
spring.datasource.db2.jdbc-url=jdbc:mysql://172.25.0.98:3306/largescreen?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.db2.username=root
spring.datasource.db2.password=123456
spring.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db2.druid.time-between-eviction-runs-millis=3600000
2、修改启动类注解,‘@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})’关闭自动根据jdbc的jar包的依赖来自动配置项目。
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@EnableTransactionManagement // 开启事物管理
@SpringBootApplication(scanBasePackages = "com")
//@EnableScheduling
public class PullZhgApplication {
public static void main(String[] args) {
// 解决elasticsearch启动保存问题(Redis和ElasticSearch中都含有netty包且版本可能不一致)
System.setProperty("es.set.netty.runtime.available.processors", "false");
SpringApplication.run(PullZhgApplication.class, args);
}
}
3、编写主数据库配置文件,MybatisSqlSessionFactoryBean可改为SqlSessionFactoryBean,我使用该Bean是因为需要配置mybatis plus;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* 数据库1配置
* @author
*/
@Configuration
@MapperScan(basePackages = "com.xxl.execute.monitoring.mapper.db1", sqlSessionTemplateRef = "oneSqlSessionTemplate")
public class OneDataSourceConfig {
@Bean(name = "oneDataSource")
@ConfigurationProperties(prefix = "spring.datasource.db1")
@Primary
public DataSource oneDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "oneSqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
return bean.getObject();
}
@Bean(name = "oneTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "oneSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
4、编写从数据库配置类
mport javax.sql.DataSource;
/**
* 数据库2配置
* @author
*/
@Configuration
@MapperScan(basePackages = "com.xxl.execute.monitoring.mapper.db2",sqlSessionTemplateRef = "twoSqlSessionTemplate")
public class TwoDataSourceConfig {
@Bean(name = "twoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "twoSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("twoDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));
return bean.getObject();
}
@Bean(name = "twoTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("twoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "twoSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("twoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
5、上诉配置mapper和xml的时候注意区分: