Druid数据源配置
package cn. config;
import java. sql. SQLException;
import javax. sql. DataSource;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. boot. autoconfigure. condition. ConditionalOnMissingBean;
import org. springframework. boot. context. properties. EnableConfigurationProperties;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration;
import com. alibaba. druid. pool. DruidDataSource;
@Configuration
@EnableConfigurationProperties ( { DruidDataSourceProperties. class } )
public class DruidDataSourceConfig {
@Autowired
private DruidDataSourceProperties properties;
@Bean
@ConditionalOnMissingBean
public DataSource druidDataSource ( ) {
DruidDataSource druidDataSource = new DruidDataSource ( ) ;
druidDataSource. setDriverClassName ( properties. getDriverClassName ( ) ) ;
druidDataSource. setUrl ( properties. getUrl ( ) ) ;
druidDataSource. setUsername ( properties. getUsername ( ) ) ;
druidDataSource. setPassword ( properties. getPassword ( ) ) ;
druidDataSource. setInitialSize ( properties. getInitialSize ( ) ) ;
druidDataSource. setMinIdle ( properties. getMinIdle ( ) ) ;
druidDataSource. setMaxActive ( properties. getMaxActive ( ) ) ;
druidDataSource. setMaxWait ( properties. getMaxWait ( ) ) ;
druidDataSource. setTimeBetweenEvictionRunsMillis ( properties. getTimeBetweenEvictionRunsMillis ( ) ) ;
druidDataSource. setMinEvictableIdleTimeMillis ( properties. getMinEvictableIdleTimeMillis ( ) ) ;
druidDataSource. setValidationQuery ( properties. getValidationQuery ( ) ) ;
druidDataSource. setTestWhileIdle ( properties. isTestWhileIdle ( ) ) ;
druidDataSource. setTestOnBorrow ( properties. isTestOnBorrow ( ) ) ;
druidDataSource. setTestOnReturn ( properties. isTestOnReturn ( ) ) ;
druidDataSource. setPoolPreparedStatements ( properties. isPoolPreparedStatements ( ) ) ;
druidDataSource. setMaxPoolPreparedStatementPerConnectionSize ( properties. getMaxPoolPreparedStatementPerConnectionSize ( ) ) ;
try {
druidDataSource. setFilters ( properties. getFilters ( ) ) ;
druidDataSource. init ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return druidDataSource;
}
}
数据源属性
package cn. config;
import org. springframework. boot. context. properties. ConfigurationProperties;
@ConfigurationProperties ( prefix = "spring.datasource.druid" )
public class DruidDataSourceProperties {
private String driverClassName;
private String url;
private String username;
private String password;
private int initialSize;
private int minIdle;
private int maxActive = 100 ;
private long maxWait;
private long timeBetweenEvictionRunsMillis;
private long minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize;
private String filters;
public int getInitialSize ( ) {
return initialSize;
}
public void setInitialSize ( int initialSize) {
this . initialSize = initialSize;
}
public int getMinIdle ( ) {
return minIdle;
}
public void setMinIdle ( int minIdle) {
this . minIdle = minIdle;
}
public int getMaxActive ( ) {
return maxActive;
}
public void setMaxActive ( int maxActive) {
this . maxActive = maxActive;
}
public long getMaxWait ( ) {
return maxWait;
}
public void setMaxWait ( long maxWait) {
this . maxWait = maxWait;
}
public long getTimeBetweenEvictionRunsMillis ( ) {
return timeBetweenEvictionRunsMillis;
}
public void setTimeBetweenEvictionRunsMillis ( long timeBetweenEvictionRunsMillis) {
this . timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
public long getMinEvictableIdleTimeMillis ( ) {
return minEvictableIdleTimeMillis;
}
public void setMinEvictableIdleTimeMillis ( long minEvictableIdleTimeMillis) {
this . minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
public String getValidationQuery ( ) {
return validationQuery;
}
public void setValidationQuery ( String validationQuery) {
this . validationQuery = validationQuery;
}
public boolean isTestWhileIdle ( ) {
return testWhileIdle;
}
public void setTestWhileIdle ( boolean testWhileIdle) {
this . testWhileIdle = testWhileIdle;
}
public boolean isTestOnBorrow ( ) {
return testOnBorrow;
}
public void setTestOnBorrow ( boolean testOnBorrow) {
this . testOnBorrow = testOnBorrow;
}
public boolean isTestOnReturn ( ) {
return testOnReturn;
}
public void setTestOnReturn ( boolean testOnReturn) {
this . testOnReturn = testOnReturn;
}
public boolean isPoolPreparedStatements ( ) {
return poolPreparedStatements;
}
public void setPoolPreparedStatements ( boolean poolPreparedStatements) {
this . poolPreparedStatements = poolPreparedStatements;
}
public int getMaxPoolPreparedStatementPerConnectionSize ( ) {
return maxPoolPreparedStatementPerConnectionSize;
}
public void setMaxPoolPreparedStatementPerConnectionSize ( int maxPoolPreparedStatementPerConnectionSize) {
this . maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
}
public String getFilters ( ) {
return filters;
}
public void setFilters ( String filters) {
this . filters = filters;
}
public String getDriverClassName ( ) {
return driverClassName;
}
public void setDriverClassName ( String driverClassName) {
this . driverClassName = driverClassName;
}
public String getUrl ( ) {
return url;
}
public void setUrl ( String url) {
this . url = url;
}
public String getUsername ( ) {
return username;
}
public void setUsername ( String username) {
this . username = username;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
}
数据库运行
package cn. config;
import java. util. concurrent. Executor;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration;
import org. springframework. scheduling. annotation. EnableAsync;
import org. springframework. scheduling. concurrent. ThreadPoolTaskExecutor;
@Configuration
@EnableAsync
public class ExecutorConfig {
private int corePoolSize = 10 ;
private int maxPoolSize = 200 ;
private int queueCapacity = 10 ;
@Bean
public Executor myAsync ( ) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ( ) ;
executor. setCorePoolSize ( corePoolSize) ;
executor. setMaxPoolSize ( maxPoolSize) ;
executor. setQueueCapacity ( queueCapacity) ;
executor. setThreadNamePrefix ( "MySimpleExecutor-" ) ;
executor. initialize ( ) ;
return executor;
}
}
springboot启动类
package cn. ffcs;
import org. springframework. boot. SpringApplication;
import org. springframework. boot. autoconfigure. SpringBootApplication;
import org. springframework. cache. annotation. EnableCaching;
import org. springframework. cloud. client. circuitbreaker. EnableCircuitBreaker;
import org. springframework. cloud. client. discovery. EnableDiscoveryClient;
import org. springframework. cloud. netflix. hystrix. dashboard. EnableHystrixDashboard;
import org. springframework. cloud. openfeign. EnableFeignClients;
import org. springframework. context. annotation. EnableAspectJAutoProxy;
import org. springframework. scheduling. annotation. EnableAsync;
@SpringBootApplication
@EnableAsync
@EnableCaching
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableAspectJAutoProxy ( proxyTargetClass= true )
@EnableCircuitBreaker
public class AppConfiguration {
public static void main ( String[ ] args) {
SpringApplication. run ( AppConfiguration. class , args) ;
}
}
pom配置
< project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0</ modelVersion>
< parent>
< groupId> cn</ groupId>
< artifactId> Chinenana</ artifactId>
< version> 1.0.0</ version>
</ parent>
< artifactId> yun</ artifactId>
< name> yun</ name>
< url> http://maven.apache.org</ url>
< properties>
< project.build.sourceEncoding> UTF-8</ project.build.sourceEncoding>
</ properties>
< dependencies>
< dependency>
< groupId> commons-httpclient</ groupId>
< artifactId> commons-httpclient</ artifactId>
< version> 3.0.1</ version>
</ dependency>
< dependency>
< groupId> com.squareup.retrofit2</ groupId>
< artifactId> retrofit</ artifactId>
< version> 2.2.0</ version>
</ dependency>
< dependency>
< groupId> com.squareup.retrofit2</ groupId>
< artifactId> converter-gson</ artifactId>
< version> 2.2.0</ version>
</ dependency>
< dependency>
< groupId> org.quartz-scheduler</ groupId>
< artifactId> quartz</ artifactId>
< exclusions>
< exclusion>
< artifactId> slf4j-api</ artifactId>
< groupId> org.slf4j</ groupId>
</ exclusion>
</ exclusions>
</ dependency>
< dependency>
< groupId> cn.ffcs</ groupId>
< artifactId> iva-common</ artifactId>
< version> 1.0.0</ version>
</ dependency>
< dependency>
< groupId> cn.ffcs</ groupId>
< artifactId> iva-apiservice</ artifactId>
< version> 1.0.0</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
</ dependency>
< dependency>
< groupId> org.mybatis.spring.boot</ groupId>
< artifactId> mybatis-spring-boot-starter</ artifactId>
< version> 1.2.0</ version>
</ dependency>
</ dependencies>
< build>
< finalName> yun</ finalName>
< plugins>
< plugin>
< groupId> pl.project13.maven</ groupId>
< artifactId> git-commit-id-plugin</ artifactId>
</ plugin>
< plugin>
< groupId> org.apache.maven.plugins</ groupId>
< artifactId> maven-compiler-plugin</ artifactId>
< configuration>
< source> 1.8</ source>
< target> 1.8</ target>
</ configuration>
</ plugin>
< plugin>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-maven-plugin</ artifactId>
< configuration>
< fork> true</ fork>
</ configuration>
</ plugin>
</ plugins>
</ build>
</ project>