springboot配置HANA和Oracle多數(shù)據(jù)源及PageHelper

配置數(shù)據(jù)庫信息

  • 因?yàn)橐玫紻ruid讀取配置,所以將druid提到了first后面
spring:
  datasource:
    first:
      druid:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.sap.db.jdbc.Driver
        url: jdbc:sap://
        username: 
        password:
        initialSize: 5
        minIdle: 5
        maxActive: 20
        # 配置獲取連接等待超時的時間
        maxWait: 60000
        # 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
        timeBetweenEvictionRunsMillis: 60000
        # 配置一個連接在池中最小生存的時間,單位是毫秒
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUMMY
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        # 打開PSCache,并且指定每個連接上PSCache的大小
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
        connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
    second:
      druid:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:oracle:
        username: 
        password: 
        initialSize: 5
        minIdle: 5
        maxActive: 20
        # 配置獲取連接等待超時的時間
        maxWait: 60000
        # 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
        timeBetweenEvictionRunsMillis: 60000
        # 配置一個連接在池中最小生存的時間,單位是毫秒
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        # 打開PSCache,并且指定每個連接上PSCache的大小
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
        connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000

寫兩個Dataconfig文件,通過MapperScan掃描來指定那些用哪個數(shù)據(jù)源

@Configuration
@MapperScan(basePackages = {"com.xxxx.xx.xx.xxxx"},sqlSessionFactoryRef = "SqlSessionFactory1")
public class DataSource1Config {

    @Bean(name = "Properties1")
    @ConfigurationProperties(prefix = "spring.datasource.first")
    public Properties Properties1(){
        return new Properties();
    }
    /*
    * 創(chuàng)建數(shù)據(jù)源datasource
    * */
    @Bean(name = "DataSource1")
    @Primary
    public DruidDataSource masterDataSource(@Qualifier("Properties1")Properties properties) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.configFromPropety(properties);
        try {
            druidDataSource.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return druidDataSource;
    }

    /**
    * 創(chuàng)建sqlsessionfactory
    */
    @Bean(name = {"SqlSessionFactory1"})
    @Primary
    public SqlSessionFactory Hana1SqlSessionFactory(@Qualifier("DataSource1") DruidDataSource druidDataSource) throws Exception {
      final  SqlSessionFactoryBean sessionFactoryBean=new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(druidDataSource);

        //分頁插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "hsqldb");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments","true");
        properties.setProperty("params","pageNum=pageNum;pageSize=pageSize;");
        interceptor.setProperties(properties);
        sessionFactoryBean.setPlugins(new Interceptor[] {interceptor});

        return sessionFactoryBean.getObject();
    }

    /*
    * 創(chuàng)建事務(wù)DataSourceTransactionManager
    * */
    @Bean(name = {"TransactionManager1"})
    @Primary
    public DataSourceTransactionManager Hana1TransactionManager(){
        return new DataSourceTransactionManager(masterDataSource(Properties1()));
    }

    @Bean(name = {"SqlSessionTemplate1"})
    @Primary
    public SqlSessionTemplate Hana1SqlSessionTemplate(@Qualifier("SqlSessionFactory1") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

第二個dataconfig也是一樣寫,就不重復(fù)上代碼了。

最后需要注意在module的pom文件下加上依賴并將druid.jar放到resource下

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.19</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/druid-1.1.19.jar</systemPath>
</dependency>

下面這是我的jar目錄

druid.jar

如果報(bào)如下錯就是上面的jar和pom沒有配好,會在druidDatasource.init();的時候報(bào)錯。

java.sql.SQLException: unknown jdbc driver : jdbc:sap://xx.xx.xx

另外我沒有把依賴都寫上,也很簡單,我懶得寫了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容