SpringBoot 数据源配置
SpringBoot单数据源配置
DataSourceConfig.java
package com.haixiangpuhui.credit.web.warehouse.consumer.config.db;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
import com.haixiangpuhui.credit.web.warehouse.consumer.config.properties.DataBaseProperties;
/**
* 数据源配置类
*
* @author HX-011
*
*/
@Configuration
@MapperScan(basePackages = DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig {
public static final String PACKAGE = "com.haixiangpuhui.credit.web.warehouse.consumer.mapper";
public static final String MAPPER_LOCATION = "classpath:mybatis/*.xml";
public static final String TYPE_ALIASES_PACKAGE = "com.haixiangpuhui.credit.web.warehouse.consumer.domain,"
+ "com.haixiangpuhui.credit.web.warehouse.consumer.form,"
+ "com.haixiangpuhui.credit.web.warehouse.consumer.vo";
@Autowired
private DataBaseProperties dataBaseProperties;
@Bean(name = "dataSource")
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
//使用属性copy 类型必须一样
// BeanUtils.copyProperties(dataBaseProperties, dataSource);
dataSource.setUrl(dataBaseProperties.getUrl());
dataSource.setDriverClassName(dataBaseProperties.getDriverClassName());
dataSource.setUsername(dataBaseProperties.getUsername());
dataSource.setPassword(dataBaseProperties.getPassword());
dataSource.setInitialSize(dataBaseProperties.getInitialSize());
dataSource.setMinIdle(dataBaseProperties.getMinIdle());
dataSource.setMaxActive(dataBaseProperties.getMaxActive());
dataSource.setMaxWait(dataBaseProperties.getMaxWait());
dataSource.setTimeBetweenEvictionRunsMillis(dataBaseProperties.getTimeBetweenEvictionRunsMillis());
dataSource.setMinEvictableIdleTimeMillis(dataBaseProperties.getMinEvictableIdleTimeMillis());
dataSource.setValidationQuery(dataBaseProperties.getValidationQuery());
dataSource.setTestWhileIdle(dataBaseProperties.getTestWhileIdle());
dataSource.setTestOnBorrow(dataBaseProperties.getTestOnBorrow());
dataSource.setTestOnReturn(dataBaseProperties.getTestOnReturn());
dataSource.setFilters(dataBaseProperties.getFilters());
return dataSource;
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource)
throws Exception {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage(DataSourceConfig.TYPE_ALIASES_PACKAGE);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(DataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
DataBaseProperties.java
package com.haixiangpuhui.credit.web.warehouse.consumer.config.properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* 数据源配置
* @author HX-011
*
*/
@Component
@PropertySource(value="config/database.properties")
public class DataBaseProperties {
@Value("${database.driver}")
private String driverClassName;
@Value("${database.url}")
private String url;
@Value("${database.username}")
private String username;
@Value("${database.password}")
private String password;
//<!-- 配置初始化大小、最小、最大 -->
@Value("${dataSource.initialSize}")
private int initialSize;
@Value("${dataSource.minIdle}")
private int minIdle;
@Value("${dataSource.maxActive}")
private int maxActive;
//<!-- 配置获取连接等待超时的时间 -->
private long maxWait = 60000;
//<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
private long timeBetweenEvictionRunsMillis = 60000;
//<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
private long minEvictableIdleTimeMillis = 300000;
private String validationQuery = "SELECT 'x'";
private boolean testWhileIdle = true;
private boolean testOnBorrow = false;
private boolean testOnReturn = false;
//<!-- 配置监控统计拦截的filters -->
private String filters = "stat";
public String getDriverClassName() {
return driverClassName;
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public int getInitialSize() {
return initialSize;
}
public int getMinIdle() {
return minIdle;
}
public int getMaxActive() {
return maxActive;
}
public long getMaxWait() {
return maxWait;
}
public long getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRunsMillis;
}
public long getMinEvictableIdleTimeMillis() {
return minEvictableIdleTimeMillis;
}
public String getValidationQuery() {
return validationQuery;
}
public boolean getTestWhileIdle() {
return testWhileIdle;
}
public boolean getTestOnBorrow() {
return testOnBorrow;
}
public boolean getTestOnReturn() {
return testOnReturn;
}
public String getFilters() {
return filters;
}
}