今天的任务就是把同时连接SQLServer和MySql的环境搭配好
在组长的帮助下,一步一步测试摸索后终于完成了!
首先是把俩个数据源写在同一个yaml里面
配置完成后再去创建工具类
MySql工具类代码:
package com.gl.framework.config;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class MySqlConfig {
private final Logger LOG = LoggerFactory.getLogger(getClass());
@Value("${spring.datasource.druid.url}")
private String jdbcUrl;
@Value("${spring.datasource.driverClassName}")
private String jdbcDriver;
@Value("${spring.datasource.druid.username}")
private String jdbcUserName;
@Value("${spring.datasource.druid.password}")
private String jdbcPassword;
@Value("${spring.datasource.druid.initialSize}")
private Integer initialSize = 5;
@Value("${spring.datasource.druid.maxActive}")
private Integer maxActive = 20;
@Value("${spring.datasource.druid.minIdle}")
private Integer minIdle = 1;
@Value("${spring.datasource.druid.maxWait}")
private Integer maxWait = 60000;
@Value("${spring.datasource.druid.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.druid.testOnBorrow}")
private Boolean testOnBorrow = false;
@Value("${spring.datasource.druid.testOnReturn}")
private Boolean testOnReturn = false;
@Value("${spring.datasource.druid.testWhileIdle}")
private Boolean testWhileIdle = true;
private Boolean poolPreparedStatements = false;
private Integer maxOpenPreparedStatements = -1;
@Bean(name = "datasource")
@Primary
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
try {
ds.setUrl(jdbcUrl);
ds.setDriverClassName(jdbcDriver);
ds.setUsername(jdbcUserName);
ds.setPassword(jdbcPassword);
ds.setInitialSize(initialSize);
ds.setMaxActive(maxActive);
ds.setMinIdle(minIdle);
ds.setMaxWait(maxWait);
ds.setValidationQuery(validationQuery);
ds.setTestOnBorrow(testOnBorrow);
ds.setTestOnReturn(testOnReturn);
ds.setTestWhileIdle(testWhileIdle);
ds.setPoolPreparedStatements(poolPreparedStatements);
ds.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return null;
}
return ds;
}
@Bean(name = "jdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("datasource") DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
}
SQLServer工具类代码
package com.gl.framework.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
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;
@Configuration
public class SqlServerConfig {
private final Logger LOG = LoggerFactory.getLogger(getClass());
@Value("${spring.primary.datasource.druid.url}")
private String jdbcUrl;
@Value("${spring.primary.datasource.driverClassName}")
private String jdbcDriver;
@Value("${spring.primary.datasource.druid.username}")
private String jdbcUserName;
@Value("${spring.primary.datasource.druid.password}")
private String jdbcPassword;
@Value("${spring.primary.datasource.druid.initialSize}")
private Integer primaryInitialSize = 5;
@Value("${spring.primary.datasource.druid.maxActive}")
private Integer primaryMaxActive = 20;
@Value("${spring.primary.datasource.druid.minIdle}")
private Integer primaryMinIdle = 1;
@Value("${spring.primary.datasource.druid.maxWait}")
private Integer primaryMaxWait = 60000;
@Value("${spring.primary.datasource.druid.validationQuery}")
private String primaryValidationQuery;
@Value("${spring.primary.datasource.druid.testOnBorrow}")
private Boolean primaryTestOnBorrow = false;
@Value("${spring.primary.datasource.druid.testOnReturn}")
private Boolean primaryTestOnReturn = false;
@Value("${spring.primary.datasource.druid.testWhileIdle}")
private Boolean primaryTestWhileIdle = true;
private Boolean primaryPoolPreparedStatements = false;
private Integer primaryMaxOpenPreparedStatements = -1;
private String filters;
@Value("${spring.datasource.druid.filter.stat.log-slow-sql}")
private String logSlowSql;
private String druidUsername;
private String druidUassword;
@Bean(name="adDataSource")
public DataSource eagleDataSource() {
DruidDataSource ds = new DruidDataSource();
try {
ds.setUrl(jdbcUrl);
ds.setDriverClassName(jdbcDriver);
ds.setUsername(jdbcUserName);
ds.setPassword(jdbcPassword);
ds.setInitialSize(primaryInitialSize);
ds.setMaxActive(primaryMaxActive);
ds.setMinIdle(primaryMinIdle);
ds.setMaxWait(primaryMaxWait);
ds.setValidationQuery(primaryValidationQuery);
ds.setTestOnBorrow(primaryTestOnBorrow);
ds.setTestOnReturn(primaryTestOnReturn);
ds.setTestWhileIdle(primaryTestWhileIdle);
ds.setPoolPreparedStatements(primaryPoolPreparedStatements);
ds.setMaxOpenPreparedStatements(primaryMaxOpenPreparedStatements);
ds.setFilters(filters);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return null;
}
return ds;
}
@Bean(name = "adJdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("adDataSource") DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
}
配置完成后就可以直接使用jdbc操作相对应的数据库了!!



