SpringBoot data source configuration
SpringBoot single data source configuration
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; /** * Data source configuration class * * @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(); / / Use the attribute copy type must be the same // 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; /** * Data source configuration * @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; //<!-- Configure initialization size, min, max--> @Value("${dataSource.initialSize}") private int initialSize; @Value("${dataSource.minIdle}") private int minIdle; @Value("${dataSource.maxActive}") private int maxActive; //<!-- Configure the time for getting a connection to wait for timeout--> private long maxWait = 60000; //<!-- How long is the configuration interval before detection is performed to detect idle connections that need to be closed, in milliseconds--> private long timeBetweenEvictionRunsMillis = 60000; //<!-- Configure the minimum lifetime of a connection in the pool, in milliseconds--> private long minEvictableIdleTimeMillis = 300000; private String validationQuery = "SELECT 'x'"; private boolean testWhileIdle = true; private boolean testOnBorrow = false; private boolean testOnReturn = false; //<!-- Configure filters for monitoring statistics interception --> 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; } }