中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

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

舞阳县| 女性| 积石山| 措美县| 游戏| 屯门区| 呼图壁县| 德昌县| 宁明县| 莱阳市| 大洼县| 房产| 石狮市| 六安市| 临洮县| 遂川县| 仁布县| 施秉县| 汶川县| 友谊县| 云南省| 寿阳县| 阿拉善右旗| 封丘县| 宁陵县| 渝中区| 额敏县| 毕节市| 博爱县| 涿州市| 大关县| 休宁县| 衡水市| 沐川县| 茌平县| 清苑县| 湖南省| 曲周县| 泉州市| 湘乡市| 通海县|