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 "-// ...
随机推荐
- bootstrap datarangepicker如何使用
本文为博主原创,未经允许不得转载: 下载资源文件: 地址:Github:https://github.com/dangrossman/bootstrap-daterangepicker/ 1.页面引用 ...
- 不在同一主机:vsftpd+pam+mysql
配置环境:Centos7上的mariadb + Centos6上的vsftpd 一.安装所需要程序 1.安装vsftpd和pam_mysql(在centos6-->192.168.108.160 ...
- 111python
补充一句,为兼容平台文件地址可以这么写: file = os.path.join('dir', 'file_name')
- MYSQL语句:创建、授权、查询、修改、统计分析等 二 用户的创建、权限设置、删除
接着上面一的内容 4.设置更改用户密码 命令格式:SET PASSWORD FOR 'username'@'host'=PASSWORD('newpassword'); 如果是当前登录用户用:SET ...
- 用docker部署flask+gunicorn+nginx
说来惭愧,写了好几个flask django项目都是在原型阶段直接python app.py 运行的,涉及到部署用nginx和gunicorn 都是让别人帮我部署的,据说好像说很麻烦的样子,我就没自己 ...
- Oracle学习笔记(二)——临时表
在针对大数据量的多表级联查询或复杂事务处理的时候,引入Oracle临时表是一种不错的策略.因此,在解决实际需求时经常会遇到需要使用存储过程和临时表相互配合的情况.下面就Oracle如何创建临时表以及注 ...
- Java 集合-集合介绍
2017-10-30 00:01:09 一.Java集合的类关系图 二.集合类的概述 集合类出现的原因:面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类. ...
- 手把手教你开发jquery插件(三)
First, i want to add options to Tabs constructor like this: var tabs = $("div.tabs").tabs( ...
- Python 运算符重载
https://www.cnblogs.com/hotbaby/p/4913363.html
- (Gorails)vuejs系列视频: Webpacker/vue-resource(不再为官方推荐)。
频:https://gorails.com/episodes/using-vuejs-for-nested-forms-part-1?autoplay=1 在嵌套表格上使用vue.js. 在appli ...