項目中遇到需要連接多個數(shù)據(jù)庫嗦锐,本來使用SpringBoot默認配置連接是非常簡單的晌姚,但是由于涉及多個數(shù)據(jù)庫,不得不再自定義配置了痘括,一次性整明白驶兜,下次就之間copy使用。
- 1.首先學(xué)習(xí)一個注解
@ConfigurationProperties(prefix = "druid")
默認注入远寸,配置文件中druid開頭的屬性抄淑。eg:
druid.url=jdbc:postgresql://139.198.x.x:1x020/account
druid.url2=jdbc:postgresql://139.198.x.x:1x020/oto_saas
druid.driver-class=org.postgresql.Driver
druid.username=root
druid.password=XXX123
druid.initial-size=1
druid.min-idle=1
druid.max-active=20
druid.test-on-borrow=true
druid.timeBetweenEvictionRunsMillis=9000
/**
*
* @author liuxin
* @since 2017/4/19
*/
@ConfigurationProperties(prefix = "druid")
public class DruidProperties {
private String url;
private String url2;
private String username;
private String password;
private String driverClass;
private int maxActive;//最大連接數(shù)
private int minIdle;//最小連接數(shù)
private int initialSize;//初始化數(shù)量和
private boolean testOnBorrow;
private Long timeBetweenEvictionRunsMillis;//心跳
- 2.添加數(shù)據(jù)源配置A
/**
* @Package: pterosaur.account.config.druid
* @Description: account 數(shù)據(jù)源1
* @author: liuxin
* @date: 17/4/21 下午7:11
*/
@Configuration
@EnableConfigurationProperties(DruidProperties.class) //開啟屬性注入,通過@autowired注入 //注入DruidProerties,就是根據(jù)第一個注解,創(chuàng)建的配置類
@ConditionalOnClass(DruidDataSource.class)//判斷這個類是否在classpath中存在
@ConditionalOnProperty(prefix = "druid", name = "url")
@MapperScan(basePackages = "pterosaur.account.mapper.account", sqlSessionTemplateRef = "accountSqlSessionTemplate")//配置實體類包
public class DruidAutoConfiguration1 {
@Autowired
private DruidProperties properties;
@Bean(name = "accountDataSource")
@Primary //DataSource 是一個接口驰后,因為是兩個數(shù)據(jù)源肆资,所以用Primary標記其中一個,防止報錯
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
if (properties.getInitialSize() > 0) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > 0) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > 0) {
dataSource.setMaxActive(properties.getMaxActive());
}
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataSource;
}
@Bean(name = "accountSqlSessionFactory")
@Primary //同上
public SqlSessionFactory testSqlSessionFactory(@Qualifier("accountDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/account/*.xml"));
return bean.getObject(); //配置映射文件地址
}
@Bean(name = "accountTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("accountDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "accountSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("accountSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
- 3.添加數(shù)據(jù)源配置B
package pterosaur.account.config.druid;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* @Package: pterosaur.account.config.druid
* @Description: otosaas 數(shù)據(jù)源2
* @author: liuxin
* @date: 17/4/21 下午7:11
*/
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
@ConditionalOnClass(DruidDataSource.class)
@ConditionalOnProperty(prefix = "druid", name = "url")
@MapperScan(basePackages = "pterosaur.account.mapper.otosaas", sqlSessionTemplateRef = "otoSaaSSqlSessionTemplate")
public class DruidAutoConfiguration2 {
@Autowired
private DruidProperties properties;
@Bean(name = "otoSaaSDataSource")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl2());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
if (properties.getInitialSize() > 0) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > 0) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > 0) {
dataSource.setMaxActive(properties.getMaxActive());
}
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataSource;
}
@Bean(name = "otoSaaSSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("otoSaaSDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/otosaas/*.xml"));
return bean.getObject();
}
@Bean(name = "accountTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("otoSaaSDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "otoSaaSSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("otoSaaSSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@ConditionalOnBean(僅僅在當(dāng)前上下文中存在某個對象時灶芝,才會實例化一個Bean)
@ConditionalOnClass(某個class位于類路徑上郑原,才會實例化一個Bean)
@ConditionalOnExpression(當(dāng)表達式為true的時候唉韭,才會實例化一個Bean)
@ConditionalOnMissingBean(僅僅在當(dāng)前上下文中不存在某個對象時,才會實例化一個Bean)
@ConditionalOnMissingClass(某個class類路徑上不存在的時候犯犁,才會實例化一個Bean)