首页 > 代码库 > spring数据源

spring数据源

包含三部分内容

1.spring jdbc

2. spring datasource

3.spring Connection pooling

完整的项目请往百度云盘下载:

 

 

测试方法

package com.guo.dataSourceDemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.alibaba.druid.pool.DruidDataSource;

public class JunitCase {


	@Test//	1.spring jdbc
	public void test(){
//		a.连接字符串
		String url = "jdbc:mysql://localhost:3306/yun?characterEncoding=UTF8"
				+ "&user=root&password=centos"
				+ "&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		try {
//			b.注册驱动
			Class.forName("com.mysql.jdbc.Driver");
//			c.获取连接
			con = DriverManager.getConnection(url);
//			d.获取句柄
			stmt = con.createStatement();
			String query = "select * from eac_core_biz";
//			e.执行语句
			result = stmt.executeQuery(query);
//			f.获取结果
			while(result.next()){
				String id = result.getString(1);
				String biz = result.getString(2);
				String name = result.getString(3);
				System.out.println("id:"+id+",biz:"+biz+",name:"+name);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
//			g.关闭连接
			closeResultSet(result);
			closeStatement(stmt);
			closeConnection(con);
		}
		
	}
	
	@Test //2. spring datasource
	public void test1(){
		ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
		DriverManagerDataSource ds = (DriverManagerDataSource) context.getBean("dataSource");
		
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
	
		try {
			con = ds.getConnection();
			stmt = con.createStatement();
			String query = "select * from eac_core_biz";
			result = stmt.executeQuery(query);
			while(result.next()){
				String id = result.getString(1);
				String biz = result.getString(2);
				String name = result.getString(3);
				System.out.println("id:"+id+",biz:"+biz+",name:"+name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeResultSet(result);
			closeStatement(stmt);
			closeConnection(con);
		}
	}
	
	@Test //3.spring Connection pooling
	public void test2(){
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-pool.xml");
		DruidDataSource ds = (DruidDataSource) context.getBean("dataSource");
		
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
	
		try {
			con = ds.getConnection();
			stmt = con.createStatement();
			String query = "select * from eac_core_biz";
			result = stmt.executeQuery(query);
			while(result.next()){
				String id = result.getString(1);
				String biz = result.getString(2);
				String name = result.getString(3);
				System.out.println("id:"+id+",biz:"+biz+",name:"+name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeResultSet(result);
			closeStatement(stmt);
			closeConnection(con);
		}
	}
	
	public void closeResultSet(ResultSet result){
		if(result!=null){
			try {
				result.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public void closeStatement(Statement stmt){
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	public void closeConnection(Connection con){
		if(con!=null){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}

  

application.properties

jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://127.0.0.1:3306/yun 
jdbc.username=root
jdbc.password=centos

context.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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd ">
    
    <bean id="dataSource"     
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">     
        <property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver" />  
        <property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/yun?characterEncoding=UTF8" />  
        <property name="username" value="http://www.mamicode.com/root" />     
        <property name="password" value="http://www.mamicode.com/centos" />  
    </bean>  
    
</beans>

spring-pool.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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd ">

 <bean id="propertyConfigure" 
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
  <property name="locations"> 
   <list> 
    <value>application.properties</value> 
   </list> 
  </property> 
 </bean>

 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
  init-method="init" destroy-method="close"> 
  <property name="driverClassName" value="http://www.mamicode.com/${jdbc.driverClassName}" /> 
  <property name="url" value="http://www.mamicode.com/${jdbc.url}" /> 
  <property name="username" value="http://www.mamicode.com/${jdbc.username}" /> 
  <property name="password" value="http://www.mamicode.com/${jdbc.password}" /> 
  <!-- 配置初始化大小、最小、最大 --> 
  <property name="initialSize" value="http://www.mamicode.com/1" /> 
  <property name="minIdle" value="http://www.mamicode.com/1" /> 
  <property name="maxActive" value="http://www.mamicode.com/10" />

  <!-- 配置获取连接等待超时的时间 --> 
  <property name="maxWait" value="http://www.mamicode.com/10000" />

  <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 
  <property name="timeBetweenEvictionRunsMillis" value="http://www.mamicode.com/60000" />

  <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
  <property name="minEvictableIdleTimeMillis" value="http://www.mamicode.com/300000" />

  <property name="testWhileIdle" value="http://www.mamicode.com/true" />

  <!-- 这里建议配置为TRUE,防止取到的连接不可用 --> 
  <property name="testOnBorrow" value="http://www.mamicode.com/true" /> 
  <property name="testOnReturn" value="http://www.mamicode.com/false" />

  <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 
  <property name="poolPreparedStatements" value="http://www.mamicode.com/true" /> 
  <property name="maxPoolPreparedStatementPerConnectionSize" value="http://www.mamicode.com/20" />

  <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->

  <property name="defaultAutoCommit" value="http://www.mamicode.com/true" />

  <!-- 验证连接有效与否的SQL,不同的数据配置不同 --> 
  <property name="validationQuery" value="http://www.mamicode.com/select 1" /> 
  <property name="filters" value="http://www.mamicode.com/stat" /> 
  <property name="proxyFilters"> 
   <list> 
    <ref bean="logFilter" /> 
   </list> 
  </property> 
 </bean>

 <bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter"> 
  <property name="statementExecutableSqlLogEnable" value="http://www.mamicode.com/false" /> 
 </bean>
</beans>

 

spring数据源