91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring-boot2.0 Mybatis多數據源配置

發布時間:2020-07-15 22:31:04 來源:網絡 閱讀:9910 作者:btmaxyyq 欄目:開發技術

spring-boot2.0 Mybatis多數據源配置

1.首先貼出pom.xml文件


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demoT002</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demoT002</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

         <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
         <!-- MySql驅動 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.然后是application.properties

#數據源1

spring.datasource.titan-master.jdbc-url=jdbc:mysql://192.168.7.284:3306/account?useUnicode=true&characterEncoding=utf-8
spring.datasource.titan-master.username=uat_api_java
spring.datasource.titan-master.password=54GDfethu634tIi6778FjsB
spring.datasource.titan-master.driver-class-name=com.mysql.jdbc.Driver

#數據源2
spring.datasource.db2.jdbc-url=jdbc:mysql://192.168.7.284:3306/basic_business?useUnicode=true&characterEncoding=utf-8
spring.datasource.db2.username=uat_api_java
spring.datasource.db2.password=54GDfethurSDFvhyU5634tIFjsB
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver

注意這邊不是url而使用的是jdbc-url

3.配置文件

主文件中:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})//exclude 排除自動讀取數據源 需要添加兩個數據庫
@ServletComponentScan
public class DemoT002Application {

    public static void main(String[] args) {
        SpringApplication.run(DemoT002Application.class, args);
    }
}

DataSource配置:

@Configuration
public class DataSourceConfig {
     @Bean(name = "ds1")
     @Primary 
        @ConfigurationProperties(prefix = "spring.datasource.titan-master") // application.properteis中對應屬性的前綴
        public DataSource dataSource1() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "ds2")
        @ConfigurationProperties(prefix = "spring.datasource.db2") // application.properteis中對應屬性的前綴
        public DataSource dataSource2() {
            return DataSourceBuilder.create().build();
        }
}

第一個連接配置:

@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.account"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {
    @Autowired
    @Qualifier("ds1")
    private DataSource ds1;

    @Bean
    @Primary 
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds1); // 使用titan數據源, 連接titan庫
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/com/example/demo/mapper/account/*.xml"));
        return factoryBean.getObject();

    }

    @Bean
    @Primary 
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory
        return template;
    }
}

第二個連接配置:

@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.basicbusiness"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {
    @Autowired
    @Qualifier("ds2")
    private DataSource ds2;

    @Bean
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds2);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/com/example/demo/mapper/basicbusiness/*.xml"));

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
        return template;
    }
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

辛集市| 台东县| 双鸭山市| 砀山县| 昌图县| 双江| 台东市| 潼关县| 安多县| 易门县| 喀喇沁旗| 五大连池市| 泸定县| 泰安市| 昆明市| 郁南县| 巩义市| 南京市| 法库县| 南漳县| 华安县| 根河市| 舞钢市| 全南县| 普安县| 金坛市| 沾化县| 平顶山市| 水富县| 玉林市| 常德市| 闻喜县| 闵行区| 山丹县| 济宁市| 甘南县| 化州市| 娄底市| 衡水市| 枝江市| 怀来县|