spring configuration dataSource: druid database connection pool

    Use Alibaba's open source Druid to configure the database connection pool. Also record the test configuration process here. In general, druid's configuration file is very similar to dbcp.

    The project section references the maven configuration of the jar:

    <!-- druid -->
    <dependency>
    	<groupId>commons-pool</groupId>
    	<artifactId>commons-pool</artifactId>
    	<version>1.6</version>
	</dependency>
    <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>druid</artifactId>
    	<version>1.1.6</version>
	</dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jdbc</artifactId>
    	<version>4.0.0.RELEASE</version>
	</dependency>
    <dependency>
   		<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.38</version>
	</dependency>

1. Create a new simple spring project.

Project directory description:

druidTestfileBeans.xml: Spring application context.

jdbc.properties: Druid database connection pool configuration file.

UserDao.java: DAO layer bean.

MainDruid.java: Test class.

The database tables and table data for testing are as follows:

2. Code

jdbc.properties:

#数据库驱动
jdbc.driverClassName=com.mysql.jdbc.Driver
#数据库
jdbc.url=jdbc:mysql://172.21.4.45:3307/cdspdb?useUnicode=true&amp;characterEncoding=utf-8
#用户名
jdbc.username=cdsp
#密码
jdbc.password=cdsp
#别名方式,扩展插件,监控统计用的filter:stat,日志用的filter:log4j,防御sql注入的filter:wall
jdbc.filters=stat 
#最大连接数
jdbc.maxActive=300
#初始化连接数
jdbc.initialSize=2
#获取连接最大等待时间
jdbc.maxWait=60000
#最小连接数
jdbc.minIdle=1
#检测连接有效性的时间间隔
jdbc.timeBetweenEvictionRunsMillis=60000
#连接保持空闲而不被驱逐的最长时间
jdbc.minEvictableIdleTimeMillis=300000
#连接有效性,检测sql
jdbc.validationQuery=SELECT 'x'
#定时检测空闲连接有效性
jdbc.testWhileIdle=true
#检测获取的连接的有效性
jdbc.testOnBorrow=false
#检测要归还的连接的有效性
jdbc.testOnReturn=false
#是否缓存preparedStatement,即PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
jdbc.poolPreparedStatements=false
jdbc.maxPoolPreparedStatementPerConnectionSize=50

druidTestfileBeans.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- jdbc配置文件 -->
	<context:property-placeholder location="com/springtest/druid/jdbc.properties" />
	<!-- 配置dbcp数据源连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">   
    	<property name="url" value="${jdbc.url}" />  
    	<property name="username" value="${jdbc.username}" />  
    	<property name="password" value="${jdbc.password}" />    
    	<property name="filters" value="${jdbc.filters}" />  
    	<property name="maxActive" value="${jdbc.maxActive}" />  
    	<property name="initialSize" value="${jdbc.initialSize}" />  
    	<property name="maxWait" value="${jdbc.maxWait}" />  
    	<property name="minIdle" value="${jdbc.minIdle}" />  
    	<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />  
    	<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />  
   		<property name="validationQuery" value="${jdbc.validationQuery}" />  
    	<property name="testWhileIdle" value="${jdbc.testWhileIdle}" />  
    	<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />  
    	<property name="testOnReturn" value="${jdbc.testOnReturn}" />  
    	<property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}" />  
    	<property name="maxPoolPreparedStatementPerConnectionSize" value="${jdbc.maxPoolPreparedStatementPerConnectionSize}" />  
	</bean> 
		
	<!-- 引入spingjdbc -->
	<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<bean id="UserDao" class="com.springtest.dbcp.UserDao">
		<property name="jdbcTemplate" ref="JdbcTemplate"></property>
	</bean>
</beans>

UserDao.java:

package com.springtest.druid;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

public class UserDao {
	
	JdbcTemplate jdbcTemplate;
	
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
        this.jdbcTemplate = jdbcTemplate;  
    } 
	public List<Map<String,Object>> queryByName(String name)
	{
		String sql = "select * from thnb_user where user_name='" + name + "' ";
		List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
}

MainDruid.java:

package com.springtest.druid;

import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springtest.dbcp.UserDao;

public class MainDruid {

	public static void main(String[] args) {
		String contextXmlPath2 = "com/springtest/druid/druidTestfileBeans.xml";
		ApplicationContext ctx = new ClassPathXmlApplicationContext(contextXmlPath2);
		UserDao userDao = (UserDao) ctx.getBean("UserDao");
		List<Map<String,Object>> list = userDao.queryByName("thb");
		System.out.println(list);
	}

}

4. Test verification

    Execute MainDruid.java and print as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326075771&siteId=291194637