亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot多數(shù)據(jù)源配置詳細教程(JdbcTemplate、mybatis)

 更新時間:2021年03月19日 09:38:58   作者:互聯(lián)網(wǎng)編程  
這篇文章主要介紹了SpringBoot多數(shù)據(jù)源配置詳細教程(JdbcTemplate、mybatis),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

多數(shù)據(jù)源配置

首先是配置文件

  • 這里采用yml配置文件,其他類型配置文件同理
  • 我配置了兩個數(shù)據(jù)源,一個名字叫ds1數(shù)據(jù)源,一個名字叫ds2數(shù)據(jù)源,如果你想配置更多的數(shù)據(jù)源,繼續(xù)加就行了
spring:
 # 數(shù)據(jù)源配置
 datasource:
  ds1: #數(shù)據(jù)源1
   driver-class-name: com.mysql.jdbc.Driver # mysql的驅動你可以配置別的關系型數(shù)據(jù)庫
   url: jdbc:mysql://ip:3306/db1 #數(shù)據(jù)源地址
   username: root # 用戶名
   password: root # 密碼
  ds2: # 數(shù)據(jù)源2
   driver-class-name: com.mysql.jdbc.Driver # mysql的驅動你可以配置別的關系型數(shù)據(jù)庫
   url: jdbc:mysql://ip:3307/db2#數(shù)據(jù)源地址
   username: root # 用戶名
   password: root # 密碼

多數(shù)據(jù)源配置

增加一個Springboot的配置類

/**
 * 多數(shù)據(jù)源配置
 */
@Configuration
public class DataSourceConfig {

  //主數(shù)據(jù)源配置 ds1數(shù)據(jù)源
  @Primary
  @Bean(name = "ds1DataSourceProperties")
  @ConfigurationProperties(prefix = "spring.datasource.ds1")
  public DataSourceProperties ds1DataSourceProperties() {
    return new DataSourceProperties();
  }

  //主數(shù)據(jù)源 ds1數(shù)據(jù)源
  @Primary
  @Bean(name = "ds1DataSource")
  public DataSource ds1DataSource(@Qualifier("ds1DataSourceProperties") DataSourceProperties dataSourceProperties) {
    return dataSourceProperties.initializeDataSourceBuilder().build();
  }

  //第二個ds2數(shù)據(jù)源配置
  @Bean(name = "ds2DataSourceProperties")
  @ConfigurationProperties(prefix = "spring.datasource.ds2")
  public DataSourceProperties ds2DataSourceProperties() {
    return new DataSourceProperties();
  }

  //第二個ds2數(shù)據(jù)源
  @Bean("ds2DataSource")
  public DataSource ds2DataSource(@Qualifier("ds2DataSourceProperties") DataSourceProperties dataSourceProperties) {
    return dataSourceProperties.initializeDataSourceBuilder().build();
  }
}

JdbcTemplate多數(shù)據(jù)源配置

增加一個Springboot配置類

/**
 * JdbcTemplate多數(shù)據(jù)源配置
 * 依賴于數(shù)據(jù)源配置
 *
 * @see DataSourceConfig
 */
@Configuration
public class JdbcTemplateDataSourceConfig {

  //JdbcTemplate主數(shù)據(jù)源ds1數(shù)據(jù)源
  @Primary
  @Bean(name = "ds1JdbcTemplate")
  public JdbcTemplate ds1JdbcTemplate(@Qualifier("ds1DataSource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
  }

  //JdbcTemplate第二個ds2數(shù)據(jù)源
  @Bean(name = "ds2JdbcTemplate")
  public JdbcTemplate ds2JdbcTemplate(@Qualifier("ds2DataSource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
  }
}

mybatis 多數(shù)據(jù)源配置

增加一個SpringBoot配置類

mybatis多數(shù)據(jù)源的原理是根據(jù)不同包,調用不同的數(shù)據(jù)源,你只需要把你的mapper.java和mapper.xml(我喜歡叫dao.java和dao.xml)寫在某個package中,springboot自動幫你實現(xiàn)數(shù)據(jù)源切換
核心代碼就這句

@MapperScan(basePackages ="com.web.ds2.**.dao", sqlSessionTemplateRef = "ds2SqlSessionTemplate")

用來指定包掃描指定sqlSessionTemplateRef
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/web/ds2/**/*.xml"));
用來指定mapper.xml的路徑

詳細配置代碼如下

/**
 * Mybatis主數(shù)據(jù)源ds1配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.web.ds1.**.dao", sqlSessionTemplateRef = "ds1SqlSessionTemplate")
public class MybatisPlusConfig4ds1 {

  //主數(shù)據(jù)源 ds1數(shù)據(jù)源
  @Primary
  @Bean("ds1SqlSessionFactory")
  public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(dataSource);
    sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
            getResources("classpath*:com/web/ds1/**/*.xml")); 
    return sqlSessionFactory.getObject();
  }

  @Primary
  @Bean(name = "ds1TransactionManager")
  public DataSourceTransactionManager ds1TransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }

  @Primary
  @Bean(name = "ds1SqlSessionTemplate")
  public SqlSessionTemplate ds1SqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}
/**
 * Mybatis 第二個ds2數(shù)據(jù)源配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.web.ds2.**.dao", sqlSessionTemplateRef = "ds2SqlSessionTemplate")
public class MybatisPlusConfig4ds2 {

  //ds2數(shù)據(jù)源
  @Bean("ds2SqlSessionFactory")
  public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(dataSource);
    sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
        getResources("classpath*:com/web/ds2/**/*.xml"));
    return sqlSessionFactory.getObject();
  }

//事務支持
  @Bean(name = "ds2TransactionManager")
  public DataSourceTransactionManager ds2TransactionManager(@Qualifier("ds2DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }

  @Bean(name = "ds2SqlSessionTemplate")
  public SqlSessionTemplate ds2SqlSessionTemplate(@Qualifier("ds2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}

mybatis-plus 多數(shù)據(jù)源配置

mybatis-plus是mybatis的增強版,只增加,不影響。也就是說使用mybatis-plus兼容原來所有的mybatis代碼和配置。然后又做了很多增加和簡化使用,具體看官網(wǎng)教程https://mybatis.plus/

相對于mybatis的多數(shù)據(jù)源配置就是改了下 SqlSessionFactory
核心代碼就是修改mybatis為mybatis-plus,如下

  @Bean("ds2SqlSessionFactory")
  public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(dataSource);
    MybatisConfiguration configuration = new MybatisConfiguration();
    configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    configuration.setJdbcTypeForNull(JdbcType.NULL);
    sqlSessionFactory.setConfiguration(configuration);
    sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
        getResources("classpath*:com/web/ds2/**/*.xml"));
    sqlSessionFactory.setPlugins(new Interceptor[]{
        new PaginationInterceptor(),
        new PerformanceInterceptor()
//            .setFormat(true),
    });
    sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false));
    return sqlSessionFactory.getObject();
  }

全部配置代碼如下

/**
 * Mybatis-plus ds2數(shù)據(jù)源配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.web.ds2.**.dao", sqlSessionTemplateRef = "ds2SqlSessionTemplate")
public class MybatisPlusConfig4ds2{

  //ds2數(shù)據(jù)源
  @Bean("ds2SqlSessionFactory")
  public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(dataSource);
    MybatisConfiguration configuration = new MybatisConfiguration();
    configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    configuration.setJdbcTypeForNull(JdbcType.NULL);
    sqlSessionFactory.setConfiguration(configuration);
    sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
        getResources("classpath*:com/web/ds2/**/*.xml"));
    sqlSessionFactory.setPlugins(new Interceptor[]{
        new PaginationInterceptor(),
        new PerformanceInterceptor()
//            .setFormat(true),
    });
    sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false));
    return sqlSessionFactory.getObject();
  }

  @Bean(name = "ds2TransactionManager")
  public DataSourceTransactionManager ds2TransactionManager(@Qualifier("ds2DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }

  @Bean(name = "ds2SqlSessionTemplate")
  public SqlSessionTemplate ds2SqlSessionTemplate(@Qualifier("ds2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}
/**
 * Mybatis-plus 主數(shù)據(jù)源ds1配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.web.ds1.**.dao", sqlSessionTemplateRef = "ds1SqlSessionTemplate")
public class MybatisPlusConfig4ds1 {

  //主數(shù)據(jù)源 ds1數(shù)據(jù)源
  @Primary
  @Bean("ds1SqlSessionFactory")
  public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(dataSource);
    MybatisConfiguration configuration = new MybatisConfiguration();
    configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
    configuration.setJdbcTypeForNull(JdbcType.NULL);
    sqlSessionFactory.setConfiguration(configuration);
    sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
            getResources("classpath*:com/ds1/web/ds1/**/*.xml"));
    sqlSessionFactory.setPlugins(new Interceptor[]{
        new PaginationInterceptor(),
        new PerformanceInterceptor()
//            .setFormat(true),
    });
    sqlSessionFactory.setGlobalConfig(new GlobalConfig().setBanner(false));
    return sqlSessionFactory.getObject();
  }

  @Primary
  @Bean(name = "ds1TransactionManager")
  public DataSourceTransactionManager ds1TransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }

  @Primary
  @Bean(name = "ds1SqlSessionTemplate")
  public SqlSessionTemplate ds1SqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}

到此這篇關于SpringBoot多數(shù)據(jù)源配置詳細教程(JdbcTemplate、mybatis)的文章就介紹到這了,更多相關SpringBoot多數(shù)據(jù)源配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論