zoukankan      html  css  js  c++  java
  • JAVA创建数据源(JdbcTemplate)

    一.JdbcTemplate模板的配置类

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package org.springframework.boot.autoconfigure.jdbc;
    
    import javax.sql.DataSource;
    import org.springframework.boot.autoconfigure.AutoConfigureAfter;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.jdbc.core.JdbcOperations;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    
    @Configuration
    @ConditionalOnClass({DataSource.class, JdbcTemplate.class})
    @ConditionalOnSingleCandidate(DataSource.class)
    @AutoConfigureAfter({DataSourceAutoConfiguration.class})
    public class JdbcTemplateAutoConfiguration {
        private final DataSource dataSource;
    
        public JdbcTemplateAutoConfiguration(DataSource dataSource) {
            this.dataSource = dataSource;
        }
    
        @Bean
        @Primary
        @ConditionalOnMissingBean({JdbcOperations.class})
        public JdbcTemplate jdbcTemplate() {
            return new JdbcTemplate(this.dataSource);
        }
    
        @Bean
        @Primary
        @ConditionalOnMissingBean({NamedParameterJdbcOperations.class})
        public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
            return new NamedParameterJdbcTemplate(this.dataSource);
        }
    }

    二.创建数据源,配置数据源

    package com.sie.watsons.base.sync.model.dao;
    
    import org.apache.commons.dbcp.BasicDataSource;
    import org.apache.commons.lang.StringUtils;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    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 org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import javax.sql.DataSource;
    
    @Configuration
    public class RmsDataSource {
    
        //配置第一个数据源
        @Bean(name = "primaryDataSource")
        @Qualifier("primaryDataSource")
        @ConfigurationProperties(prefix="spring.datasource.primary")
        public DataSource primaryDataSource() {
            return DataSourceBuilder.create().build();
        }
        @Bean(name = "primaryJdbcTemplate")
        public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
            return new JdbcTemplate(dataSource);
        }
        //配置第二个数据源
        @Bean(name = "secondaryDataSource")
        @Qualifier("secondaryDataSource")
        @Primary
        @ConfigurationProperties(prefix="spring.datasource.secondary")
        public DataSource secondaryDataSource() {
            return DataSourceBuilder.create().build();
        }
        @Bean(name = "secondaryJdbcTemplate")
        public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) {
            return new JdbcTemplate(dataSource);
        }
    }
  • 相关阅读:
    工厂方法模式
    单例模式
    .NET平台下几种SOCKET模型的简要性能供参考
    easy ui 教程
    ACCESS数据库改名asp或asa
    库函数strcpy/strlen的工作方式
    opencv cvPreCornerDetect
    BlobTracker
    图像处理 Mine
    几种常见模式识别算法整理和总结
  • 原文地址:https://www.cnblogs.com/lingtiaoti/p/12535889.html
Copyright © 2011-2022 走看看