依赖版本
Spring Boot:1.5.18
druid-spring-boot-starter:1.1.10
参考文档
druid-spring-boot-starter
pom配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| <?xml version="1.0" encoding="UTF-8"?> <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>
<groupId>com.mytest</groupId> <artifactId>druid</artifactId> <version>0.0.1-SNAPSHOT</version>
<properties>
<sqlite.version>3.15.1</sqlite.version> <mysql.version>5.1.41</mysql.version> <druid-starter.version>1.1.10</druid-starter.version> </properties>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.18.RELEASE</version> </parent>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid-starter.version}</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>${sqlite.version}</version> <scope>runtime</scope> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> <scope>runtime</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
|
springboot配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| spring.application.name=druid_test
spring.datasource.druid.one.url=jdbc:mysql://127.0.0.1:3306/one?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false spring.datasource.druid.one.username=root spring.datasource.druid.one.password=root spring.datasource.druid.one.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.one.initial-size=5 spring.datasource.druid.one.min-idle=15 spring.datasource.druid.one.max-active=60 spring.datasource.druid.one.validation-query=SELECT 1 spring.datasource.druid.one.test-on-borrow=true spring.datasource.druid.one.test-while-idle=true spring.datasource.druid.one.time-between-eviction-runs-millis=60000
spring.datasource.druid.two.url=jdbc:mysql://127.0.0.1:3306/two?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false spring.datasource.druid.two.username=root spring.datasource.druid.two.password=root spring.datasource.druid.two.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.two.initial-size=5 spring.datasource.druid.two.min-idle=15 spring.datasource.druid.two.max-active=60 spring.datasource.druid.two.validation-query=SELECT 1 spring.datasource.druid.two.test-on-borrow=true spring.datasource.druid.two.test-while-idle=true spring.datasource.druid.two.time-between-eviction-runs-millis=60000
spring.datasource.druid.use-global-data-source-stat=true
spring.datasource.druid.web-stat-filter.enabled=true spring.datasource.druid.web-stat-filter.url-pattern=/* spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/admin/druid/* spring.datasource.druid.stat-view-servlet.reset-enable=false
|
配置注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.slf4j.Logger; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
@Configuration public class DataSourceConfigurer { private static final Logger log = org.slf4j.LoggerFactory.getLogger(DataSourceConfigurer.class); @Primary @Bean(value = "dataSourceOne",initMethod = "init") @ConfigurationProperties("spring.datasource.druid.one") public DataSource dataSourceOne(){ log.info("Init DataSourceOne"); return DruidDataSourceBuilder.create().build(); }
@Bean(value = "dataSourceTwo",initMethod = "init") @ConfigurationProperties("spring.datasource.druid.two") public DataSource dataSourceTwo(){ log.info("Init DataSourceTwo"); return DruidDataSourceBuilder.create().build(); } }
|