1. 程式人生 > >Druid工具配置使用

Druid工具配置使用

一、 配置maven

<!-- mysql資料庫連線池 pool -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.0.8</version>
</dependency>

二、自定義DataSourceFactory類, 用來封裝第三方DataSource

import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;
import com.alibaba.druid.pool.DruidDataSource;

public class DruidDataSourceFactory extends UnpooledDataSourceFactory{
	public DruidDataSourceFactory(){
		this.dataSource = new DruidDataSource();
	}
}

三、配置mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"/>
			<dataSource type="com.****.***.web.DruidDataSourceFactory">
                <property name="url" value="${jdbc_url}" />
                <property name="username" value="${jdbc_username}" />
                <property name="password" value="${jdbc_password}" />
                <!-- 配置初始化大小、最小、最大 -->
                <property name="initialSize" value="1" />
                <property name="minIdle" value="1" />
                <property name="maxActive" value="20" />

                <!-- 配置獲取連線等待超時的時間 -->
                <property name="maxWait" value="60000" />

                <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->
                <property name="timeBetweenEvictionRunsMillis" value="60000" />

                <!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->
                <property name="minEvictableIdleTimeMillis" value="300000" />

                <property name="validationQuery" value="SELECT 'x'" />
                <property name="testWhileIdle" value="true" />
                <property name="testOnBorrow" value="false" />
                <property name="testOnReturn" value="false" />

                <!-- 配置監控統計攔截的filters,去掉後監控介面sql無法統計 -->
                <property name="filters" value="stat" />
            </dataSource>
		</environment>
	</environments>

	<mappers>
		<package name="com.***.****.entity" />
	</mappers>
</configuration>

四、配置訪問路徑(web.xml)

<!-- druid監控 -->
<servlet>
	<servlet-name>DruidStatView</servlet-name>
	<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
	<url-pattern>/druid/*</url-pattern>
</servlet-mapping>

當然還可以配置一些過濾條件

  • 詳情參見: