第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

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

標(biāo)簽:
SpringBoot

配置

  • 首先配置application.yml文件,这里利用阿里的Druid作为连接池,base和follow分别代表主库和从库

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource    base:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: oracle.jdbc.driver.OracleDriver
      initialize: true #指定初始化数据源,是否用data.sql来初始化,默认: true
      name: base
      jdbc-url: jdbc:oracle:thin:@189.126.156.396:9522:oratest
      username: 用户名      password: 密码    follow:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      initialize: true
      name: follow      url: jdbc:mysql://162.66.66.66:3666/paygateway
      username: 你的歌用户名      password: 你的密码
---#下面是分页和日志相关内容,不需要可以删掉pagehelper:
  reasonable: true
  helperDialect: oracle
  support-methods-arguments: true
  params: count=countSqlmybatis:
  configuration:
    mapUnderscoreToCamelCase: truelogging:
  level:
    com:
      xxx:
        paygateway:
          dao: DEBUG    org:
      spring:
        springboot:
          dao: DEBUG      springframework: WARN
  • 配置主库Configuration,注意prefix前缀和@Primary注解(表示主库)和下划线转驼峰方法的配置

@Configuration@MapperScan(basePackages = "com.xxx.paygateway.dao.db1", sqlSessionTemplateRef = "baseSqlSessionTemplate")public class BaseDataSourceConfig {    //这里配置数据源
    @Bean(name = "baseDataSource")    @ConfigurationProperties(prefix = "spring.datasource.base")    @Primary 
    public DataSource setDataSource() {        return DataSourceBuilder.create().build();
    }    //这里配置事务管理器
    @Bean(name = "baseTransactionManager")    @Primary
    public DataSourceTransactionManager setTransactionManager(@Qualifier("baseDataSource") DataSource dataSource) {        return new DataSourceTransactionManager(dataSource);
    }    //这里配置SqlSessionFactory,连接工厂
    @Bean(name = "baseSqlSessionFactory")    @Primary
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        //此处配置下划线转驼峰
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        bean.setDataSource(dataSource);        //此处配置mybatis扫描路径
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/base/*.xml"));        return bean.getObject();
    }    //这里配置SqlSessionTemplate,标准操作模板
    @Bean(name = "baseSqlSessionTemplate")    @Primary
    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
  • 配置从库Configuration,与从库基本一致,注意注解和前缀就可以了

@Configuration@MapperScan(basePackages = "com.xxx.paygateway.dao.db2", sqlSessionTemplateRef = "fSqlSessionTemplate")public class FollowDataSourceConfig {    @Bean(name = "fDataSource")    @ConfigurationProperties(prefix = "spring.datasource.follow")    public DataSource setDataSource() {        return new DruidDataSource();
    }    @Bean(name = "fTransactionManager")    public DataSourceTransactionManager setTransactionManager(@Qualifier("fDataSource") DataSource dataSource) {        return new DataSourceTransactionManager(dataSource);
    }    @Bean(name = "fSqlSessionFactory")    public SqlSessionFactory setSqlSessionFactory(@Qualifier("fDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/follow/*.xml"));        return bean.getObject();
    }    @Bean(name = "fSqlSessionTemplate")    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("fSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
  • 把两个数据库的dao层和mapper.xml文件分到不同的文件夹下面,1归1,2归2。service层正常调用即可!


    webp

    image.png


    webp



作者:后厂村老司机
链接:https://www.jianshu.com/p/033e0ebeb617


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消