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 "-// ...
随机推荐
- 【第九章】 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...
- org.apache.axis2.AxisFault: java.lang.Error: Unresolved compilation problem:
原创:转载请注明出处 今天遇到以下一个异常,找了好长时间,就是不知道什么原因, 在网上搜了好多,也没搜到相关的解决方法. 1.异常展示, org.apache.axis2.AxisFault: jav ...
- confluence导出pdf 文字显示不全
当使用confluence编辑页面时,当一行的文字过多,且中间没什么逗号分隔时,有时会出现导出的pdf文件中,这一行显示的文字不全的情况. 如: 很明显费用的费字没有显示完全,且后面还有其他的字. 可 ...
- Java编程语言下 Selenium 驱动各个浏览器代码
这里采用的是Selenium3.7版本,首先介绍的是在Windows环境下运行的: 总结下注意事项: 1,设置各个浏览器的Driver路径 System.setProperty("" ...
- JavaMail直接发送邮件
一般用JavaMail发送邮件都需要先登录到外部smtp服务器(如smtp.163.com) 二次转发,其实只要得到域名的邮件交换服务器地址(MX)就可以直接将邮件发出 这里用到了dnsjava(下载 ...
- C#中简单的文件操作实例
using System; using System.IO; namespace Demo { class Program { static string tmpPath = @"D:/Lg ...
- Linux : 密码正确不能正常登陆,日志提示Could not get shadow information for user
今天,再玩Centos7的时候,尝试修改了下ssh的端口.因为默认开启了SELinux,如果没有修改这个文件配置就修改端口sshd服务就不能正常启动了. 但是,当我修改会22端口的时候还是不能正常登陆 ...
- 20170112xlVBA查询SQL
Sub NextSeven_CodeFrame() '应用程序设置 Application.ScreenUpdating = False Application.DisplayAlerts = Fal ...
- php--------http 状态代码及其原因
HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...
- Java基础-IO流(13)
IO流用来处理设备之间的数据传输.可以实现文件复制,上传文件和下载文件. Jdk提供的流继承了四大类:InputStream(字节输入流),OutputStream(字节输出流),Reader(字符输 ...