上篇简要介绍了如何在spring中配置默认的连接池和jdbc模板,这篇来介绍开源的连接池配置与属性引入

C3P0连接池配置:

引入jar包

配置c3p0连接池

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- ==============配置C3P0连接池=============== -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3307/test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean> <!-- ==============配置jdbc模板================ -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<!-- ==属性注入== -->
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>

在实际开发中,习惯于将jdbc的这些配置抽取出来,使结构更加清晰,所以,接下来介绍引入属性文件

抽取属性文件

在src目录下新建jdbc.properties

修改applicationContext5.xml的代码为如下即可

<!-- ===============引入属性文件============= -->
<!-- 第一种方式通过一个bean标签引入的(很少) -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/> </bean> -->
<!-- 第二种方式,通过context标签引入 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- ==========配置c3p0连接池============ -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!-- ==============配置jdbc模板================ -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<!-- ==属性注入== -->
<property name="dataSource" ref="dataSource"></property>
</bean>

Spring的jdbc模板2:使用开源的连接池的更多相关文章

  1. Spring的jdbc模板1

    Spring是EE开发的一站式框架,有EE开发的每一层解决方案.Spring对持久层也提供了解决方案:ORM模块和jdbc模块,ORM模块在整合其他框架的时候使用 Spring提供了很多的模板用于简化 ...

  2. 四、spring的JDBC模板和事务管理

    Spring的JDBC模板 Spring是JavaEE开发的一站式框架,对各种持久化技术都提供了简单的模板 ORM持久化技术 模板类 JDBC org.springframework.jdbc.cor ...

  3. Spring的JDBC模板

    Spring对持久层技术支持 JDBC : org.springframework.jdbc.core.JdbcTemplate Hibernate3.0 : org.springframework. ...

  4. Java学习笔记43(Spring的jdbc模板)

    在之前的学习中,我们执行sql语句,需要频繁的开流,关流比较麻烦,为了更加的简化代码,我们使用Spring 的jdbc模板jdbcTemplate来简化我们的代码量:需要导入的包有: 我们在之前的dr ...

  5. Spring系列 之数据源的配置 数据库 数据源 连接池的区别

    Spring系列之数据源的配置 数据源,连接池,数据库三者的区别 连接池:这个应该都学习过,比如c3p0,druid等等,连接池的作用是为了提高程序的效率,因为频繁的去创建,关闭数据库连接,会对性能有 ...

  6. Spring的jdbc模板3:完成CURD操作

    测试类代码如下 package zcc.spring_jdbc.demo2; import java.sql.ResultSet; import java.sql.SQLException; impo ...

  7. java框架之Spring(3)-JDBC模板使用&事务管理

    下面内容使用到的 jar 包下载 JDBC模板使用 入门 1.导包,如要导入 Spring 的基本开发包.数据库驱动包.Spring 提供的 JDBC 模板包,如下: 2.测试: @Test publ ...

  8. 十八 Spring的JDBC模板:引入外部属性文件

    配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: <?xml version="1.0" encoding="UT ...

  9. 十七 Spring的JDBC模版:使用开源连接池,DBCP,C3P0

    DBCP的配置以及使用 引入jar包

随机推荐

  1. js_jquery_创建cookie有效期问题_时区问题

    用jquery设置Cookie过期的两种方式: $.cookie('名', '值', { expires: 过期时间(DateTime), path: "/", domain: w ...

  2. [android] 帧布局

    /*******************2016年5月3日 更新**************************************/ 知乎:如何理解andriod中的View和framela ...

  3. Spring Bean的生命周期,《Spring 实战》书中的官方说法

    连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...

  4. SpringMVC 的运行原理

    0. 灵魂的拷问   问:SpringMVC 是什么?它有什么作用?  答:SpringMVC 的全称是 Spring Web Model-View-Controller,它是 Spring Fram ...

  5. D. GukiZ and Binary Operations(矩阵+二进制)

    D. GukiZ and Binary Operations   We all know that GukiZ often plays with arrays. Now he is thinking ...

  6. Dom对象的研究

    1.逻辑运算  ||  &&  ! 1||2   5&&4     !0 || 遇到第一个为true 的数字就终止并返回 && 遇到第一个为false ...

  7. canvas 画时钟 会动呦

    //半径 var r = 130; //重置原点 ctx.save(); ctx.translate(400, 500); //使用translate重置原点 function drawClock() ...

  8. Stable Fur Generation on Mesh

    After tested the Maya 2015 XGen Grooming, we dropped it, that's really slow and unstable, totally no ...

  9. Android Service、IntentService,Service和组件间通信

    Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...

  10. Testlink1.9.17使用方法(第十三章 使用中遇到的问题)

    第十三章 使用中遇到的问题 一. 登录Testlink后,新建一个项目后,会出现如下提示: 解决办法:打开Testlink安装文件夹下的config.inc.php文件, 原来:$tlCfg-> ...