包含三部分内容

1.spring jdbc

2. spring datasource

3.spring Connection pooling

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

https://pan.baidu.com/s/1mhNCofI

另外有一个druid数据库连接池以及监控的例子 DataSourceProject

测试方法

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="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/yun?characterEncoding=UTF8" />
<property name="username" value="root" />
<property name="password" value="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="${jdbc.driverClassName}" />
<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="10" /> <!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="10000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 --> <property name="defaultAutoCommit" value="true" /> <!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
<property name="validationQuery" value="select 1 " />
<property name="filters" value="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="false" />
</bean>
</beans>

spring数据源的更多相关文章

  1. Spring 数据源配置二:多数据源

    通过上一节  Spring 数据源配置一: 单一数据源  我们了解单一数据源的配置, 这里我们继续多个数据源的配置 如下(applicationContent.xml 内容) 一:  Spring   ...

  2. 【JAVA】Spring 数据源配置整理

            在Spring中,不但可以通过JNDI获取应用服务器的数据源,也可以直接在Spring容器中配置数据源,此外,还可以通过代码的方式创建一个数据源,以便进行无依赖的单元测试. 配置数据源 ...

  3. Spring 数据源配置三:多数据源

    在上一节中,我们讲述了多数据的情况: 1. 数据源不同(数据库厂商不同, 业务范围不同, 业务数据不同) 2. SQL mapper 文件不同, 3. mybatis + 数据方言不同 即最为简单的多 ...

  4. Spring 数据源配置一:单一数据源

    最近遇到一个项目,需要访问都多个数据源,并且数据库是不同厂商(mysql,  sqlserver). 所以对此做了一些研究,这里咱们采用渐进的方式来展开,先谈谈单一数据源配置.(稍后有时间会陆续补充其 ...

  5. Log4j配置文件位置+Spring数据源配置文件位置

    一个.Log4j配置文件位置 1.加载自己主动 当应用程序启动,缺省情况下,这将是src文件夹搜索log4j.xml型材.如果不存在.我们将继续寻找log4j.properties文件,仅仅要找到当中 ...

  6. Spring 数据源

    1.使用org.springframework.jdbc.datasource.DriverManagerDataSource说明:DriverManagerDataSource建立连接是只要有连接就 ...

  7. spring数据源、数据库连接池

    什么是数据源.数据库连接池? DataSource通常被称为数据源,它包含连接池和连接池管理两个部分,习惯上也经常把DataSource称为连接池. 数据库连接池的基本思想:为数据库连接建立一个“缓冲 ...

  8. 聊聊、Spring 数据源

    平时开发中我们每天都会跟数据库打交道,页面上显示的数字,图片,语音,等等都存在某个地方,而我们就是要从那个地方拿到我们想要的.现在存储数据的方式越来越多,多种多样,但用的最多的还是关系数据库.Spri ...

  9. spring数据源配置

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-// ...

随机推荐

  1. 谈谈java中的final关键字

    知识点:final(最终的)关键字修饰类.方法.属性 1.final修饰类:那么这个就无法被继承,如String类.StringBuffer类.System类 2.final修饰方法:被修饰的方法不能 ...

  2. [JVM] - 不就是JVM么 JVM的继续探究

    前面几章跟着作者的脚步实现了使用Go语言查看java的.class文件源码(16进制) 复习一下: 相比Java语言,Go的访问控制非常简单,只有公开和私有两种. 所有首字母大写的类型, 结构体,字段 ...

  3. HDU 2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意: 给出m个模式串,求长度不超过n的且至少包含一个模式串的字符串个数. 思路: 如果做过poj2778 ...

  4. HDU 6092 Rikka with Subset(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=6092 题意: 给出两个数组A和B,A数组一共可以有(1<<n)种不同的集合组合,B中则记录了每个数出 ...

  5. UVa 1603 破坏正方形

    https://vjudge.net/problem/UVA-1603 题意:有一个火柴棍组成的正方形网格,计算至少要拿走多少根火柴才能破坏所有正方形. 思路:从边长为1的正方形开始遍历,将正方形的边 ...

  6. 微信网页跳转页面常见bug处理

    微信网页跳转页面常见bug处理 1.不要直接用a链接直接跳转 2.url后加上时间戳 function gohome() { window.location.href = "../home/ ...

  7. go 通道

    1. package main import "fmt" func sum(s []int, c chan int) { sum := for _, v := range s { ...

  8. Spring boot 中Hibernate 使用

    spring.jpa.properties.hibernate.hbm2ddl.auto=有四种配置方式,分别如下: 是hibernate的配置属性,其主要作用是:自动创建.更新.验证数据库表结构.该 ...

  9. 重新拾取的jquery

    最新JQ API学习地址:http://www.css88.com/jqapi-1.9/error/

  10. Unity 如何在打包的时候执行一些逻辑

    1.如果想让unity在打包的过程中,执行一些逻辑,那么该如何做呢?代码如下: using UnityEditor; using UnityEditor.Build; using UnityEngin ...