最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用SpringBoot 配置Oracle和H2雙數(shù)據(jù)源及問題

 更新時(shí)間:2021年11月18日 09:32:29   作者:TheBiiigBlue  
這篇文章主要介紹了使用SpringBoot 配置Oracle和H2雙數(shù)據(jù)源及問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在上節(jié)使用了H2之后感覺很爽,很輕便,正好有個(gè)項(xiàng)目要求簡(jiǎn)單,最好不適用外部數(shù)據(jù)庫(kù),于是就想著把H2數(shù)據(jù)庫(kù)集成進(jìn)來,這個(gè)系統(tǒng)已經(jīng)存在了一個(gè)Oracle,正好練習(xí)下配置多數(shù)據(jù)源,而在配置多數(shù)據(jù)源時(shí),H2的schema配置不生效真是花了我好長(zhǎng)時(shí)間才解決。。。所以也記錄一下

配置POM

<!-- oracle -->
 <dependency>
     <groupId>com.github.noraui</groupId>
     <artifactId>noraui</artifactId>
     <version>2.4.0</version>
 </dependency>
<!-- h2-->
 <dependency>
     <groupId>com.h2database</groupId>
     <artifactId>h2</artifactId>
     <version>1.4.197</version>
 </dependency>
 <!-- mybatisplus -->
 <dependency>
 	  <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.1.1</version>
 </dependency>

配置yml

spring:
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  datasource:
    driver-class-name: org.h2.Driver
    schema: classpath:h2/schema-h2.sql
    data: classpath:h2/data-h2.sql
    jdbc-url: jdbc:h2:file:D:/Cache/IdeaWorkSpace/BigData/CustomerModel/src/main/resources/h2/data/h2_data
    username: root
    password: a123456
    initialization-mode: always
    oracle:
     driver-class-name: oracle.jdbc.driver.OracleDriver
     jdbc-url: jdbc:oracle:thin:@xxx:1521:cmis
     username: xxx
     password: xxx
  h2:
    console:
      enabled: true
      path: /h2-console

可以看到配置中配置了兩個(gè)數(shù)據(jù)源,主數(shù)據(jù)源是H2,第二個(gè)數(shù)據(jù)源是Oracle,接下來是通過配置類來注入數(shù)據(jù)源

配置注入

配置H2主數(shù)據(jù)源

package com.caxs.warn.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
 * @Author: TheBigBlue
 * @Description:
 * @Date: 2019/9/18
 */
@Configuration
@MapperScan(basePackages = "com.caxs.warn.mapper.h2", sqlSessionFactoryRef = "h2SqlSessionFactory")
public class H2DSConfig {
    @Bean(name = "h2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "h2TransactionManager")
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(this.dataSource());
    }
    @Bean(name = "h2SqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("h2DataSource") DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sessionFactory.getObject();
    }
    @Bean(name = "h2Template")
    public JdbcTemplate h2Template(@Qualifier("h2DataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

配置oracle從數(shù)據(jù)源

package com.caxs.warn.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
 * @Author: TheBigBlue
 * @Description:
 * @Date: 2019/9/18
 */
@Configuration
@MapperScan(basePackages = "com.caxs.warn.mapper.oracle",sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDSConfig {
    @Bean(name = "oracleDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.oracle")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "oracleTransactionManager")
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(this.dataSource());
    }
    @Bean(name = "oracleSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sessionFactory.getObject();
    }
    @Bean(name = "oracleTemplate")
    public JdbcTemplate oracleTemplate(@Qualifier("oracleDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

問題

Schema “classpath:h2/schema-h2.sql” not found

  

經(jīng)過上面的配置就可以使用雙數(shù)據(jù)源了,但是當(dāng)我們測(cè)試時(shí)會(huì)發(fā)現(xiàn)報(bào)如下錯(cuò)誤:Schema “classpath:h2/schema-h2.sql” not found,這個(gè)問題我也是找了好久,因?yàn)樵谂渲玫珨?shù)據(jù)源的時(shí)候沒有這個(gè)問題的,在配置多數(shù)據(jù)源才有了這個(gè)問題。

在這里插入圖片描述   

單數(shù)據(jù)源時(shí),是直接SpringBoot自動(dòng)配置DataSource的,這個(gè)時(shí)候是正常的,而當(dāng)配置多數(shù)據(jù)源時(shí),我們是通過@Configuration來配置數(shù)據(jù)源的,懷疑問題出在 DataSourceBuilder 創(chuàng)建數(shù)據(jù)源這個(gè)類上,而單數(shù)據(jù)源自動(dòng)裝載時(shí)不會(huì)出現(xiàn)這樣的問題。然后百度搜了下這個(gè)DataSourceBuilder,看到文章中實(shí)例的配置中schema是這樣寫的:

package com.caxs.warn.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * @Author: TheBigBlue
 * @Description: 服務(wù)啟動(dòng)后,初始化數(shù)據(jù)庫(kù)
 * @Date: 2019/9/19
 */
@Component
public class ApplicationRunnerService implements ApplicationRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRunnerService.class);
    @Autowired
    @Qualifier("h2Template")
    private JdbcTemplate h2Template;
    @Value("${invoke.schema.location}")
    private String schema;
    @Value("${invoke.data.location}")
    private String data;
    /**
     * @Author: TheBigBlue
     * @Description: 項(xiàng)目啟動(dòng),執(zhí)行sql文件初始化
     * @Date: 2019/9/19
     * @Param args:
     * @Return:
     **/
    @Override
    public void run(ApplicationArguments args) {
        String schemaContent = this.getFileContent(schema);
        String dataContent = this.getFileContent(data);
        h2Template.execute(schemaContent);
        h2Template.execute(dataContent);
    }
    /**
     * @Author: TheBigBlue
     * @Description: 獲取classpath下sql文件內(nèi)容
     * @Date: 2019/9/19
     * @Param filePath:
     * @Return:
     **/
    private String getFileContent(String filePath) {
        BufferedReader bufferedReader = null;
        String string;
        StringBuilder data = new StringBuilder();
        try {
            ClassPathResource classPathResource = new ClassPathResource(filePath);
            bufferedReader = new BufferedReader(new InputStreamReader(classPathResource.getInputStream()));
            while ((string = bufferedReader.readLine()) != null) {
                data.append(string);
            }
        } catch (IOException e) {
            LOGGER.error("加載ClassPath資源失敗", e);
        }finally {
            if(null != bufferedReader){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return data.toString();
    }
}

在這里插入圖片描述   

抱著嘗試的態(tài)度改了下,發(fā)現(xiàn)果然沒問題了?。≡瓉硎窃赟pringBoot2.0之后schema對(duì)應(yīng)的DataSourceProperties類中schema屬性是一個(gè)List,所以需要前面加 - (yml中加-映射集合),記錄下防止后面再踩坑。

在這里插入圖片描述

在這里插入圖片描述

Table “USER” not found; SQL statement:

在這里插入圖片描述   

這個(gè)問題也是在只有配置多數(shù)據(jù)源時(shí)才會(huì)碰到的問題,就是配置的spring.datasource.schema和spring.datasource.data無效。這個(gè)我看了下如果是配置單數(shù)據(jù)源,springboot自動(dòng)加載Datasource,是沒問題的,但是現(xiàn)在是我們自己維護(hù)的datasource: return DataSourceBuilder.create().build();所以感覺還是DataSourceBuilder在加載數(shù)據(jù)源的時(shí)候的問題,但是還是沒有找到原因。有網(wǎng)友說必須加initialization-mode: ALWAYS這個(gè)配置,但是我配置后也是不能用的。

在這里插入圖片描述   

最后沒辦法就配置了一個(gè)類,在springboot啟動(dòng)后,自己加載文件,讀取其中的sql內(nèi)容,然后用jdbcTemplate去執(zhí)行了下,模擬了下初始化的操作。。。后面如果有時(shí)間再來解決這個(gè)問題。

package com.caxs.warn.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * @Author: TheBigBlue
 * @Description: 服務(wù)啟動(dòng)后,初始化數(shù)據(jù)庫(kù)
 * @Date: 2019/9/19
 */
@Component
public class ApplicationRunnerService implements ApplicationRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRunnerService.class);
    @Autowired
    @Qualifier("h2Template")
    private JdbcTemplate h2Template;
    @Value("${invoke.schema.location}")
    private String schema;
    @Value("${invoke.data.location}")
    private String data;
    /**
     * @Author: TheBigBlue
     * @Description: 項(xiàng)目啟動(dòng),執(zhí)行sql文件初始化
     * @Date: 2019/9/19
     * @Param args:
     * @Return:
     **/
    @Override
    public void run(ApplicationArguments args) {
        String schemaContent = this.getFileContent(schema);
        String dataContent = this.getFileContent(data);
        h2Template.execute(schemaContent);
        h2Template.execute(dataContent);
    }
    /**
     * @Author: TheBigBlue
     * @Description: 獲取classpath下sql文件內(nèi)容
     * @Date: 2019/9/19
     * @Param filePath:
     * @Return:
     **/
    private String getFileContent(String filePath) {
        BufferedReader bufferedReader = null;
        String string;
        StringBuilder data = new StringBuilder();
        try {
            ClassPathResource classPathResource = new ClassPathResource(filePath);
            bufferedReader = new BufferedReader(new InputStreamReader(classPathResource.getInputStream()));
            while ((string = bufferedReader.readLine()) != null) {
                data.append(string);
            }
        } catch (IOException e) {
            LOGGER.error("加載ClassPath資源失敗", e);
        }finally {
            if(null != bufferedReader){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return data.toString();
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 數(shù)組獲取最大和最小值的實(shí)例實(shí)現(xiàn)

    Java 數(shù)組獲取最大和最小值的實(shí)例實(shí)現(xiàn)

    這篇文章主要介紹了Java 數(shù)組獲取最大和最小值的實(shí)例實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • dom4j操作xml的demo(分享)

    dom4j操作xml的demo(分享)

    下面小編就為大家?guī)硪黄猟om4j操作xml的demo(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java用BigDecimal解決double類型相減時(shí)可能存在的誤差

    Java用BigDecimal解決double類型相減時(shí)可能存在的誤差

    這篇文章主要介紹了Java用BigDecimal解決double類型相減時(shí)可能存在的誤差,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Java實(shí)現(xiàn)字符串的分割(基于String.split()方法)

    Java實(shí)現(xiàn)字符串的分割(基于String.split()方法)

    Java中的我們可以利用split把字符串按照指定的分割符進(jìn)行分割,然后返回字符串?dāng)?shù)組,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)字符串的分割的相關(guān)資料,是基于jDK1.8版本中的String.split()方法,需要的朋友可以參考下
    2022-09-09
  • SpringBoot集成Druid監(jiān)控頁(yè)面最小化配置操作

    SpringBoot集成Druid監(jiān)控頁(yè)面最小化配置操作

    這篇文章主要介紹了SpringBoot集成Druid監(jiān)控頁(yè)面最小化配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Java http請(qǐng)求封裝工具類代碼實(shí)例

    Java http請(qǐng)求封裝工具類代碼實(shí)例

    這篇文章主要介紹了Java http請(qǐng)求封裝工具類代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 深入了解Spring中Bean的作用域和生命周期

    深入了解Spring中Bean的作用域和生命周期

    這篇文章主要介紹了深入了解Spring中Bean的作用域和生命周期,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot配置開發(fā)環(huán)境的詳細(xì)步驟(JDK、Maven、IDEA等)

    SpringBoot配置開發(fā)環(huán)境的詳細(xì)步驟(JDK、Maven、IDEA等)

    文章介紹了如何配置SpringBoot開發(fā)環(huán)境,包括安裝JDK、Maven和IDEA,并提供了詳細(xì)的步驟和配置方法,感興趣的朋友一起看看吧
    2024-12-12
  • Springboot?注解EqualsAndHashCode詳解

    Springboot?注解EqualsAndHashCode詳解

    注解@EqualsAndHashCode主要用于自動(dòng)生成equals方法和hashCode方法,callSuper屬性為true時(shí),生成的方法會(huì)包括父類字段,為false則只包含當(dāng)前類字段,IDEA工具中有檢查提示并可自動(dòng)修復(fù)相關(guān)代碼,確保注解正確使用,更多詳解可查閱相關(guān)文檔
    2024-10-10
  • Java中ThreadLocal的使用

    Java中ThreadLocal的使用

    這篇文章主要介紹了Java中ThreadLocal的使用,靜態(tài)內(nèi)部類的加載是在程序中調(diào)用靜態(tài)內(nèi)部類的時(shí)候加載的,和外部類的加載沒有必然關(guān)系, 但是在加載靜態(tài)內(nèi)部類的時(shí)候 發(fā)現(xiàn)外部類還沒有加載,那么就會(huì)先加載外部類 ,加載完外部類之后,再加載靜態(tài)內(nèi)部類,需要的朋友可以參考下
    2023-09-09

最新評(píng)論

绥中县| 兴隆县| 遵义市| 普兰店市| 高安市| 莱州市| 汉川市| 泾源县| 稷山县| 沙雅县| 二连浩特市| 四会市| 新沂市| 军事| 永年县| 都江堰市| 肥城市| 怀化市| 临城县| 沧州市| 老河口市| 应城市| 客服| 湘潭县| 兴化市| 鲁甸县| 内乡县| 昆山市| 贺州市| 大丰市| 凤山市| 黎平县| 壶关县| 兰西县| 永登县| 英德市| 和田市| 和平区| 灵石县| 舒兰市| 锡林浩特市|