springboot 整合mybaties带多个数据源

springboot集成了mybaties,默认情况下只需指定spring运行的配置文件,你可以不用做些什么,mapper的数据源会自动加载,但对于多数据源时该怎么配置呢?
首先看看我的目录树,我的项目需要访问两个跨域的数据库源。你需要做的事如下:
springboot 整合mybaties带多个数据源_第1张图片

1. springboot的启动类指定exclude,这项属性在旧版是在@EnableAutoConfiguration设置。@MapperScan设置为projectName.mapper所在的主包,见上图。酌情修改

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
    DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
@MapperScan("gocron_manager_system.mapper")

2.配置文件,编写数据源,如下:屏蔽关键字,但并不影响阅读。数据连接信息最后一个点之前的可随意命名,但末尾的必须一致。springboot 2.0前 jdbc-url 需改成 url

server.port=9527

#数据链接1
datasource.ultraman.jdbc-url=jdbc:mysql://xxx:3306/xxx?characterEncoding=utf-8&useAffectedRows=true
datasource.ultraman.username=xx
datasource.ultraman.password=xx
datasource.ultraman.driverClassName=com.mysql.jdbc.Driver

#数据链接2
datasource.clouddev.jdbc-url=jdbc:mysql://xxx:3306/xxx?characterEncoding=utf-8&useAffectedRows=true
datasource.clouddev.username=xx
datasource.clouddev.password=xx
datasource.clouddev.driverClassName=com.mysql.jdbc.Driver

3.mapper类,有几个数据源,创建几个。我的mapper是以注解方式实现,并未使用xml。且是接口,不需要编写实现类

@Mapper
@Repository
public interface UltramanMapper {

  @Select("Select task_id, run_freq FROM t_project WHERE id=#{ultramanId}")
  UltramanProject getUltramanProject(@Param("ultramanId") int ultramanId);

}

4.编写与数据源相对的config类,有几个数据源就得创建几个类。需要注意设置@MapperScan的basePackage指向你的mapper类所在的包。里面的一些Bean名称,注意一致。对于没使用xml实现mapper的,sqlSessionFactory方法getResources("classpath*:mapper/clouddev/*.xml"))可以随意,不用创建目录

#####cloddev数据源,config类

@Configuration
@MapperScan(basePackages = "gocron_manager_system.mapper.clouddev", sqlSessionFactoryRef = "clouddevSqlSessionFactory")
public class ClouddevDataSourceConfig {

  @Primary
  @Bean(name = "clouddevDataSource")
  @ConfigurationProperties("datasource.clouddev")
  public DataSource masterDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "clouddevSqlSessionFactory")
  public SqlSessionFactory sqlSessionFactory(@Qualifier("clouddevDataSource") DataSource dataSource)
      throws Exception {
    SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
        .getResources("classpath*:mapper/clouddev/*.xml"));
    return sessionFactoryBean.getObject();
  }

}

 #####ultraman数据源,config类

@Configuration
@MapperScan(basePackages = "gocron_manager_system.mapper.ultraman", sqlSessionFactoryRef = "ultramanSqlSessionFactory")
public class UltramanDataSourceConfig {

  @Primary
  @Bean(name = "ultramanDataSource")
  @ConfigurationProperties("datasource.ultraman")
  public DataSource masterDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "ultramanSqlSessionFactory")
  public SqlSessionFactory sqlSessionFactory(@Qualifier("ultramanDataSource") DataSource dataSource)
      throws Exception {
    SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
        .getResources("classpath*:mapper/ultraman/*.xml"));
    return sessionFactoryBean.getObject();
  }

}

4.使用的时候,自动注入即可进行方法的调用

  @Autowired
  private LogMetaMapper logMetaMapper;

  @Autowired
  private UltramanMapper ultramanMapper;

 

你可能感兴趣的:(java)