美文网首页
spring boot oracle连接配置

spring boot oracle连接配置

作者: 一介书生独醉江湖 | 来源:发表于2022-05-06 16:15 被阅读0次
    # pom.xml
    # 引入oracle,mybatis依赖
    
            <!--oracle 依赖-->
            <dependency>
                <groupId>com.oracle.database.jdbc</groupId>
                <artifactId>ojdbc6</artifactId>
                <version>11.2.0.4</version>
            </dependency>
            <!--oracle 依赖-->
    
            <!--mybatis 依赖-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.2.0</version>
            </dependency>
            <!--mybatis 依赖-->
    

    一、方式一

    # 第一种方式只修改application.yml文件;
    # 如果只有一个数据源,那么直接使用这种方式即可;
    
    spring:
      datasource:
        url: jdbc:oracle:thin:@//*.*.*.28:1521/orcl
        driver-class-name: oracle.jdbc.driver.OracleDriver
        username: your_user_name
        password: your_user_password
    

    二、方式二

    # 第二种方式支持多数据源;
    # 如果使用这种方式,yml文件稍微有点不一样;如下:
    
    spring:
      datasource:
        fxdb:
          jdbc-url: jdbc:oracle:thin:@//*.*.*.28:1521/orcl
          username: your_user_name
          password: your_user_password
          driverClassName: oracle.jdbc.OracleDriver
    
    # 创建一个配置类;
    # 如果需要多数据源,创建多个  *DataSourceConfig,然后修改配置内的约定即可;
    # 最后在yml文件中配置数据源(例如上面的 spring.datasource.fxdb);
    
    package com.sawl.fxapi.config;
    
    import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionTemplate;
    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.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    /**
     * @Author ds
     * @Date 2022-05-06
     */
    @Configuration
    @MapperScan(basePackages = "com.sawl.fxapi.api.*.dao", sqlSessionTemplateRef = "fxJdbcTemplate")
    public class FXDataSourceConfig {
    
        @Bean(name = "fxDataSource")
        @ConfigurationProperties(prefix="spring.datasource.fxdb")
        public DataSource fxDataSource(){
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "fxJdbcFactory")
        public SqlSessionFactory fxJdbcFactory(@Qualifier(value ="fxDataSource") DataSource dataSource) throws Exception {
            MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/fx/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "fxJdbcManager")
        public DataSourceTransactionManager fxJdbcManager(@Qualifier(value = "fxDataSource") DataSource dataSource){
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "fxJdbcTemplate")
        public SqlSessionTemplate fxJdbcTemplate(@Qualifier(value = "fxJdbcFactory") SqlSessionFactory sqlSessionFactory){
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    

    相关文章

      网友评论

          本文标题:spring boot oracle连接配置

          本文链接:https://www.haomeiwen.com/subject/ochzyrtx.html