spring中配置c3p0数据源

来源:互联网 发布:怡海软件技术有限公司 编辑:程序博客网 时间:2024/05/16 12:06

1:applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
                  http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
                        
      <!-- 导入资源文件 -->
  <context:property-placeholder location="classpath:db.properties"/>
  <!-- 配置C3p0数据源 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="user"><value>root</value></property>
    <property name="password"><value>123456</value></property>
    <property name="jdbcUrl"><value>jdbc:mysql://localHost/mydatabase</value></property>
    <property name="driverClass"><value>com.mysql.jdbc.Driver</value></property>
    <property name="initialPoolSize"><value>5</value></property>
    <property name="minPoolSize"><value>5</value></property>
    <property name="maxPoolSize"><value>10</value></property>
  </bean>
 </beans>


  2:db.properties

username=root
password=123456
jdbcUrl=jdbc:mysql://localHost/mydatabase
driverClass=com.mysql.jdbc.Driver
minPoolSize=5
initialPoolSize=5
maxPoolSize=15

 

 3:JUnit

package com.spring.jdbc;


import static org.junit.Assert.*;


import java.sql.SQLException;


import javax.sql.DataSource;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class JDBCTest {
  private ApplicationContext ctx=null;
  {
 ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
  }
@Test
public void test() throws SQLException {
DataSource dataSource=ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 幼师面试没特长怎么办 资格证到6年没换怎么办 中央巡视组走了怎么办 校园招聘学生违约怎么办 应届毕业生考上公务员档案怎么办 移动硬盘弹不出来怎么办 录音笔电池坏了怎么办 上不吃大厕怎么办 跨考研究生面试怎么办 手机信息查重怎么办 闲鱼不予申诉怎么办 广东理工职业学院没选到课怎么办 淘宝开店认证后怎么办 手机微信解冻怎么办 微信永久封号怎么办 拒绝微信登录怎么办 微信支付冻结怎么办 微信被冻结了钱怎么办 爱剪辑视频卡顿怎么办 优慕课密码忘了怎么办 高考填空题雷同怎么办 佳能600d模糊怎么办 教师面试没过怎么办 触电了怎么办安全教案 论文导师不指导怎么办 中考没有考好怎么办 mooc刷不满课时怎么办 公寓房小孩读书怎么办 小学搬迁周围住户怎么办 qq空间无法回复怎么办 入职年龄不够怎么办 鱼生病了怎么办 急救 打错电话怎么办 礼仪 脚脱皮有臭味怎么办 想做淘宝手模怎么办 办模特卡被骗怎么办 上菜时台面已摆满菜怎么办 幼儿园发现疑似水痘怎么办 幼儿园孩子长水痘怎么办 穿高跟鞋大腿疼怎么办 穿高跟鞋走路难看怎么办