美文网首页
Springboot 多源mysql连接

Springboot 多源mysql连接

作者: 驕傲的兎孒 | 来源:发表于2019-06-26 15:07 被阅读0次

    Pom引用

    <parent>

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

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

    <version>2.0.4.RELEASE</version>

    <relativePath/>

    </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</artifactId>

    </dependency>

    <dependency>

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

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

    <scope>test</scope>

    </dependency>

    <dependency>

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

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

    </dependency>

    <dependency>

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

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

    </dependency>

        <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-test</artifactId>

    <version>5.0.9.RELEASE</version>

    <scope>test</scope>

    </dependency>

    <dependency>

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

    <artifactId>spring-boot-test</artifactId>

    </dependency>

    <dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>4.12</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-test</artifactId>

    <version>5.0.8.RELEASE</version>

    <scope>compile</scope>

    </dependency>

    <dependency>

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

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

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-jdbc</artifactId>

    <version>5.0.7.RELEASE</version>

    </dependency>

    <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>

    </dependency>

    <dependency>

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

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

    <scope>test</scope>

    </dependency>

    <dependency>

    <groupId>log4j</groupId>

    <artifactId>log4j</artifactId>

    <version>1.2.17</version>

    </dependency>

    <dependency>

    <groupId>com.github.ulisesbocchio</groupId>

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

    <version>2.1.0</version>

    </dependency>

    <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-configuration-processor</artifactId>

    <optional>true</optional>

    </dependency>

    </dependencies>

    Mysql数据源

    spring.datasource.primary.jdbc-url=jdbc:mysql://XX.XX.XX.XX:3306/XX?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useCompression=true&useSSL=false

    spring.datasource.primary.username=XX

    spring.datasource.primary.password=XX

    spring.datasource.primary.max-idle=10

    spring.datasource.primary.max-wait=10000

    spring.datasource.primary.min-idle=5

    spring.datasource.primary.initial-size=5

    #spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver

    spring.datasource.secondary.jdbc-url=jdbc:mysql://192.168.XX.XX:3306/XX?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useCompression=true&useSSL=false

    spring.datasource.secondary.username=XX

    spring.datasource.secondary.password=X

    spring.datasource.secondary.max-idle=10

    spring.datasource.secondary.max-wait=10000

    spring.datasource.secondary.min-idle=5

    spring.datasource.secondary.initial-size=5

    双源配置DataSourceConfig

    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.context.annotation.Primary;

    import org.springframework.jdbc.core.JdbcTemplate;

    import javax.sql.DataSource;

    /**

    * @Author: Liuyong

    * @Description:

    * @Time:2019/6/21 15:20

    */

    @Configuration

    public class DataSourceConfig {

    @Bean(name ="primaryDataSource")

    @Qualifier("primaryDataSource")

    @ConfigurationProperties(prefix="spring.datasource.primary")

    public DataSource primaryDataSource() {

    return DataSourceBuilder.create().build();

    }

    @Bean(name ="secondaryDataSource")

    @Qualifier("secondaryDataSource")

    @Primary

        @ConfigurationProperties(prefix="spring.datasource.secondary")

    public DataSource secondaryDataSource() {

    return DataSourceBuilder.create().build();

    }

    @Bean(name ="primaryJdbcTemplate")

    public JdbcTemplate primaryJdbcTemplate(

    @Qualifier("primaryDataSource") DataSource dataSource) {

    return new JdbcTemplate(dataSource);

    }

    @Bean(name ="secondaryJdbcTemplate")

    public JdbcTemplate secondaryJdbcTemplate(

    @Qualifier("secondaryDataSource") DataSource dataSource) {

    return new JdbcTemplate(dataSource);

    }

    }

    双源调用:

    @Autowired

    @Qualifier("primaryJdbcTemplate")

    protected JdbcTemplatejdbcTemplate1;

    @Autowired

    @Qualifier("secondaryJdbcTemplate")

    protected JdbcTemplatejdbcTemplate2;

    相关文章

      网友评论

          本文标题:Springboot 多源mysql连接

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