zoukankan      html  css  js  c++  java
  • jdbctemplate配置多数据源

    一、创建项目导入相关依赖

       

    1.1

       

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-jdbc</artifactId>

    </dependency>

       

    <dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <scope>runtime</scope>

    <version>5.1.27</version>

    </dependency>

       

    <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>druid-spring-boot-starter</artifactId>

    <version>1.1.10</version>

    </dependency>

       

       

    二、创建User实体类,UserService,UserController

       

    2.1

       

    在application.properties文件中设置多数据源

       

    spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource

    spring.datasource.one.url=jdbc:mysql://127.0.0.1:3306/jdbctemplates?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT

    spring.datasource.one.username=root

    spring.datasource.one.password=123

       

    spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource

    spring.datasource.two.url=jdbc:mysql://127.0.0.1:3306/jdbctemplates2?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT

    spring.datasource.two.username=root

    spring.datasource.two.password=123

       

    2.2

       

    由于我们配置了多数据源,springboot提供的自动化配置也就失效,需要我们手动配置。

       

    创建DataSourceConfig和jdbctemplateConfig配置类

       

       

       

    @Qualifier

    作用告诉他使用哪个DataSource

    @ConfigurationProperties

    加载application.perteries的哪个数据源

       

       

       

    2.2

    创建UserService

       

       

    3.3

    创建Controller

       

       

  • 相关阅读:
    LintCode "Maximum Gap"
    LintCode "Wood Cut"
    LintCode "Expression Evaluation"
    LintCode "Find Peak Element II"
    LintCode "Remove Node in Binary Search Tree"
    LintCode "Delete Digits"
    LintCode "Binary Representation"
    LeetCode "Game of Life"
    LintCode "Coins in a Line"
    LintCode "Word Break"
  • 原文地址:https://www.cnblogs.com/fernfei/p/12112026.html
Copyright © 2011-2022 走看看