spring数据源
包含三部分内容
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数据源的更多相关文章
- Spring 数据源配置二:多数据源
通过上一节 Spring 数据源配置一: 单一数据源 我们了解单一数据源的配置, 这里我们继续多个数据源的配置 如下(applicationContent.xml 内容) 一: Spring ...
- 【JAVA】Spring 数据源配置整理
在Spring中,不但可以通过JNDI获取应用服务器的数据源,也可以直接在Spring容器中配置数据源,此外,还可以通过代码的方式创建一个数据源,以便进行无依赖的单元测试. 配置数据源 ...
- Spring 数据源配置三:多数据源
在上一节中,我们讲述了多数据的情况: 1. 数据源不同(数据库厂商不同, 业务范围不同, 业务数据不同) 2. SQL mapper 文件不同, 3. mybatis + 数据方言不同 即最为简单的多 ...
- Spring 数据源配置一:单一数据源
最近遇到一个项目,需要访问都多个数据源,并且数据库是不同厂商(mysql, sqlserver). 所以对此做了一些研究,这里咱们采用渐进的方式来展开,先谈谈单一数据源配置.(稍后有时间会陆续补充其 ...
- Log4j配置文件位置+Spring数据源配置文件位置
一个.Log4j配置文件位置 1.加载自己主动 当应用程序启动,缺省情况下,这将是src文件夹搜索log4j.xml型材.如果不存在.我们将继续寻找log4j.properties文件,仅仅要找到当中 ...
- Spring 数据源
1.使用org.springframework.jdbc.datasource.DriverManagerDataSource说明:DriverManagerDataSource建立连接是只要有连接就 ...
- spring数据源、数据库连接池
什么是数据源.数据库连接池? DataSource通常被称为数据源,它包含连接池和连接池管理两个部分,习惯上也经常把DataSource称为连接池. 数据库连接池的基本思想:为数据库连接建立一个“缓冲 ...
- 聊聊、Spring 数据源
平时开发中我们每天都会跟数据库打交道,页面上显示的数字,图片,语音,等等都存在某个地方,而我们就是要从那个地方拿到我们想要的.现在存储数据的方式越来越多,多种多样,但用的最多的还是关系数据库.Spri ...
- spring数据源配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-// ...
随机推荐
- js键盘按钮keyCode及示例大全
以功能区分布 以 keycode 编号顺序分布 keycode 0 = keycode 1 = keycode 2 = keycode 3 = keycode 4 = keycode 5 = keyc ...
- mysql中index与Multiple-Column Indexes区别与联系
索引对提升SELECT/UPDATE语句查询速度有着立竿见影的效果,有索引和无索引,查询速度往往差几个数量级. 本次讨论一下index(每列作为一个索引,单列索引)和Multiple-Column I ...
- shuoj 418 丢史蒂芬妮(素数筛+sg函数)
丢史蒂芬妮 代码: #include<bits/stdc++.h> using namespace std; +; int SG[N][N]; bool S[N]; vector<i ...
- tomcat和java环境
mac tomcat http://blog.csdn.net/huyisu/article/details/38372663 mac jdk 1.8 http://wlb.wlb.blog.163. ...
- 在每个节点填充向右的指针 Populating Next Right Pointers in Each Node
2018-08-09 16:01:40 一.Populating Next Right Pointers in Each Node 问题描述: 问题求解: 由于是满二叉树,所以很好填充. public ...
- [Java学习] 强调一下编程风格
讲完了Java的基础语法,大家就可以编写简单的程序代码了,这里有必要强调一下编程风格. 代码风格虽然不影响程序的运行,但对程序的可读性却非常重要.自己编写的程序要让别人看懂,首先在排版方面要非常注意. ...
- (GoRails )使用Vue.js制作拖拉list功能(v5-8)
视频5 改进视觉效果,让list看起来更舒服.新增横向滚动功能. 参考我的trello:https://trello.com/b/BYvCBpyZ/%E6%AF%8F%E6%97%A5%E8%AE%B ...
- php--------http 状态代码及其原因
HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...
- 『Glob』常用方法记录
glob.glob(file) 返回匹配的文件 glob.glob(./flower_photos/tulips/*.jpg) Out[1]:<br># ['./flower_photos ...
- python中的注意事项
.python 中的 and 从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值: or 也是从左到有计算表达式,返回第一个为真的值: 其中数字 0 是假,其他都是真: 字 ...