This commit is contained in:
trang
2017-07-11 21:53:04 +08:00
parent 23a490b6b8
commit e59154fdc1
13 changed files with 282 additions and 29 deletions
+1 -2
View File
@@ -1,2 +1 @@
# druid-spring-boot
Spring Boot Starter for Druid
# Druid Spring Boot Starter
+1 -1
View File
@@ -11,7 +11,7 @@
</parent>
<artifactId>druid-spring-boot-autoconfigure</artifactId>
<name>druid-spring-boot-autoconfigure</name>
<name>${project.artifactId}</name>
<dependencies>
<dependency>
+51 -8
View File
@@ -1,15 +1,58 @@
<?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"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>druid-spring-boot</artifactId>
<groupId>com.github.drtrang</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>druid-spring-boot-samples</artifactId>
<parent>
<groupId>com.github.drtrang</groupId>
<artifactId>druid-spring-boot</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>druid-spring-boot-samples</artifactId>
<name>${project.artifactId}</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.drtrang</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,33 @@
package com.github.trang.druid;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.trang.druid.mapper.CityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* DruidSpringBootStarterApplication
*
* @author trang
*/
@SpringBootApplication
public class DruidSpringBootStarterApplication implements CommandLineRunner {
public static void main(String[] args) {
System.setProperty("druid.logType", "slf4j");
SpringApplication.run(DruidSpringBootStarterApplication.class, args);
}
@Autowired
private DruidDataSource dataSource;
@Autowired
private CityMapper cityMapper;
@Override
public void run(String... args) throws Exception {
System.out.println(cityMapper.findByState("CA"));
}
}
@@ -0,0 +1,19 @@
package com.github.trang.druid.mapper;
import com.github.trang.druid.model.City;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* CityMapper
*
* @author trang
*/
@Mapper
public interface CityMapper {
@Select("select * from city where state = #{state}")
City findByState(@Param("state") String state);
}
@@ -0,0 +1,59 @@
package com.github.trang.druid.model;
import java.io.Serializable;
/**
* City
*
* @author trang
*/
public class City implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String state;
private String country;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return getId() + "," + getName() + "," + getState() + "," + getCountry();
}
}
@@ -0,0 +1,66 @@
debug: false
logging:
level:
root: info
org.springframework: info
org.springframework.jdbc: info
com.github.trang: debug
com.alibaba.druid.pool.DruidDataSourceStatLoggerImpl: debug
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:file:./samples
username: root
password: root
druid:
initial-size: 1
min-idle: 1
max-active: 10
max-wait: 30000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-open-prepared-statements: 20
use-global-data-source-stat: true
stat:
db-type: h2
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
slf4j:
enabled: true
connection-log-enabled: false
connection-log-error-enabled: true
statement-log-enabled: false
statement-log-error-enabled: true
statement-sql-pretty-format: true
statement-executable-sql-log-enable: true
result-set-log-enabled: false
result-set-log-error-enabled: true
wall:
enabled: true
db-type: h2
log-violation: true
throw-exception: false
aop:
enabled: true
patterns:
- com.github.trang.mapper.*
web:
web-stat-filter:
enabled: true
stat-view-servlet:
enabled: true
schema: classpath:import.sql
aop:
auto: true
proxy-target-class: true
mybatis:
type-aliases-package: com.github.trang.druid.model
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS city;
CREATE TABLE city (
id INT(6) PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(64),
state VARCHAR(32),
country VARCHAR(32)
);
INSERT INTO city (NAME, state, country) VALUES ('San Francisco', 'CA', 'US');
@@ -0,0 +1,39 @@
package com.github.trang.druid.test;
import com.alibaba.druid.pool.DruidDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
/**
* @author trang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
public class DruidTests {
@Autowired
private DruidDataSource dataSource;
@Test
public void testDataSource() {
assertEquals(dataSource.getInitialSize(), 1);
assertEquals(dataSource.getMinIdle(), 1);
assertEquals(dataSource.getMaxActive(), 10);
assertEquals(dataSource.getMaxWait(), 30000);
assertEquals(dataSource.getTimeBetweenEvictionRunsMillis(), 60000);
assertEquals(dataSource.getMinEvictableIdleTimeMillis(), 300000);
assertEquals(dataSource.getValidationQuery(), "SELECT 1");
assertEquals(dataSource.isTestWhileIdle(), true);
assertEquals(dataSource.isTestOnBorrow(), false);
assertEquals(dataSource.isTestOnReturn(), false);
assertEquals(dataSource.getMaxPoolPreparedStatementPerConnectionSize(), 20);
assertEquals(dataSource.isUseGlobalDataSourceStat(), true);
}
}
-15
View File
@@ -1,15 +0,0 @@
<?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">
<parent>
<artifactId>druid-spring-boot</artifactId>
<groupId>com.github.drtrang</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>druid-spring-boot-starter-test</artifactId>
</project>
+1 -1
View File
@@ -11,7 +11,7 @@
</parent>
<artifactId>druid-spring-boot-starter</artifactId>
<name>druid-spring-boot-starter</name>
<name>${project.artifactId}</name>
<dependencies>
<dependency>
@@ -1 +1 @@
provides: spring-boot,spring-boot-configuration-processor,druid,druid-spring-boot-autoconfigure
provides: spring-boot-autoconfigure,spring-boot-configuration-processor,druid,druid-spring-boot-autoconfigure
+1 -1
View File
@@ -8,7 +8,7 @@
<version>1.0.0</version>
<packaging>pom</packaging>
<name>druid-spring-boot</name>
<name>${project.artifactId}</name>
<description>Spring Boot Druid Starter</description>
<url>https://github.com/drtrang/druid-spring-boot</url>