Java C3p0在Spring中运用
1、导入jar包
/Spring/lib/mysql-connector-java-5.1.39-bin.jar
/Spring/lib/c3p0-0.9.1.2.jar
/Spring/lib/cglib-3.2.2.jar
/Spring/lib/mchange-commons-java-0.2.12.jar
2、配置db文件,以mysql为例
jdbc.url=root
jdbc.password=niuheng
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/vShop?useUnicode=true&characterEncoding=UTF-8
jdbc.driverClass=com.mysql.jdbc.Driver
3、配置Spring文件
<!-- 加载配置文件 -->
<bean id="dbPropertyConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源dataSourse -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="user" value="${jdbc.url}">
</property>
<property name="password" value="${jdbc.password}">
</property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}">
</property>
<property name="driverClass" value="${jdbc.driverClass}">
</property>
<!-- 连接池中保留的最小连接数,默认为:3 -->
<property name="minPoolSize" value="3" />
<!-- 连接池中保留的最大连接数。默认值: 15 -->
<property name="maxPoolSize" value="15" />
</bean>
<!-- 配置jdbc template -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>