springboot-mybatis-starter链接多个数据库

 spring.datasource.weight.username=sa
 spring.datasource.weight.password=123
 spring.datasource.weight.url=jdbc:sqlserver://10.83.4.90:1433;DatabaseName=weight20
 spring.datasource.weight.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
 
 spring.datasource.gps.username=sa
 spring.datasource.gps.password=sggps_123
 spring.datasource.gps.url=jdbc:sqlserver://10.10.1.249:1433;DatabaseName=SGPTGPS
 spring.datasource.gps.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
 
 mybatis.type-aliases-package=com.dfmc.szxinterface.model
 #查看源代码 map-locations是数组
 mybatis.mapper-locations=classpath:mapper/gps/*.xml,classpath:mapper/weight/*.xml
 mybatis.configuration.map-underscore-to-camel-case=true

 

 //EnableAutoConfiguration注解,关闭springBoot关于mybatis的一些自动注入
 @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
 //加入定时器
 @EnableScheduling
 @SpringBootApplication
 public class DataTransGsiotApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(DataTransGsiotApplication.class, args);
    }
 }

 

 package com.dfmc.szxinterface.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.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.env.Environment;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 
 import javax.sql.DataSource;
 
 @Configuration
 @MapperScan(basePackages = "com.dfmc.szxinterface.mapper.gps",
         sqlSessionFactoryRef = "gpsSqlSessionFactory")
 public class gpsDataSourceConfig {
     @Autowired
     Environment env;
 
     @Primary
     @Bean(name = "gpsDataSource")
     @ConfigurationProperties(prefix = "spring.datasource.gps")
     public DataSource gpsDataSource(){
         return DataSourceBuilder.create()
                .url(env.getProperty("spring.datasource.gps.url"))
                .username(env.getProperty("spring.datasource.gps.username"))
                .password(env.getProperty("spring.datasource.gps.password"))
                .driverClassName(env.getProperty("spring.datasource.gps.driver-class-name")).build();
    }
 
     @Bean(name = "gpsSqlSessionFactory")
     public SqlSessionFactory sqlSessionFactory(@Qualifier("gpsDataSource") DataSource dataSource) throws Exception {
         SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
         sessionFactoryBean.setDataSource(dataSource);
         sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/gps/*.xml"));
         return sessionFactoryBean.getObject();
    }
 }

 

 package com.dfmc.szxinterface.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.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.env.Environment;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 
 import javax.sql.DataSource;
 
 @Configuration
 @MapperScan(basePackages = "com.dfmc.szxinterface.mapper.weight", sqlSessionFactoryRef = "weightSqlSessionFactory")
 public class weightDataSourceConfig {
     @Autowired
     Environment env;
 
     @Primary
     @Bean(name = "weightDataSource")
     @ConfigurationProperties(prefix="spring.datasource.weight")
     public DataSource weightDataSource(){
         return DataSourceBuilder.create()
                .url(env.getProperty("spring.datasource.weight.url"))
                .username(env.getProperty("spring.datasource.weight.username"))
                .password(env.getProperty("spring.datasource.weight.password"))
                .driverClassName(env.getProperty("spring.datasource.weight.driver-class-name"))
                .build();
    }
 
     @Bean(name = "weightSqlSessionFactory")
     public SqlSessionFactory sqlSessionFactory(@Qualifier("weightDataSource") DataSource dataSource) throws Exception {
         SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
         sessionFactoryBean.setDataSource(dataSource);
         sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/weight/*.xml"));
         return sessionFactoryBean.getObject();
    }
 }

 

 Date objDate = new Date();
 SimpleDateFormat objSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 //HH 0-23
 //hh 1-12 am/pm
 @Scheduled(cron = "50 0 * * * *" ) //查看源代码cron

 

posted @ 2021-11-04 14:47  mtgold  阅读(165)  评论(0)    收藏  举报