c3p0、dbcp数据源在spring的配置

代码]spring 的配置文件     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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"
     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-3.0.xsd" >
 
     <!-- 配置数据源的位置 dbcp.properties -->
     <context:property-placeholder location= "classpath:dbcp.properties" />
     
     <!-- 配置数据源的基本信息-->
     <bean id= "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" >
         <property name= "username" value= "${jdbc.username}" ></property>
         <property name= "password" value= "${jdbc.password}" ></property> 
         <property name= "url" value= "${jdbc.url}" ></property>
         <property name = "driverClassName" value= "${jdbc.driverClass}" </property>
         <property name= "initialSize" value= "${jdbc.initialSize}" ></property>
         <property name= "maxActive" value= "${jdbc.maxActive}" ></property>
         
     </bean>
     
 
      <!-- 导入外部的资源文件 c3p0.properties-->
     <context:property-placeholder location= "classpath:c3p0.properties" />
     
     <!-- 配置数据源 -->
     <bean id= "dataSource1" class = "com.mchange.v2.c3p0.ComboPooledDataSource" >
         <property name= "user" value= "${jdbc.user}" ></property>
         <property name= "password" value= "${jdbc.password}" ></property>
         <property name= "driverClass" value= "${jdbc.driverClass}" ></property>
         <property name= "jdbcUrl" value= "${jdbc.jdbcUrl}" ></property>
         
         <property name= "initialPoolSize" value= "${jdbc.initPoolSize}" >   </property>
         <property name= "maxPoolSize" value= "${jdbc.maxPoolSize}" ></property>
     </bean>
     
 
</beans>

2. [代码]dbcp和c3p0的配置文件配置     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
dbcp.properties的配置:
 
jdbc.user=root
jdbc.password=
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql: //localhost:3306/test
 
jdbc.initPoolSize= 5
jdbc.maxPoolSize= 10
 
 
c3p0.properties 的配置:
 
jdbc.username=root
jdbc.password=
jdbc.driverClass=org.gjt.mm.mysql.Driver
jdbc.url=jdbc:mysql: //localhost:3306/test
 
 
jdbc.initialSize= 5
jdbc.maxActive= 10

3. [代码]定义测试类     跳至 [1] [2] [3] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.xiaomo.spring.jdbc;
 
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
* 测试dbcp的数据源连接是否成功
*/
public class Test {
     public static void main(String[] args) {
         ApplicationContext ctx = new ClassPathXmlApplicationContext( "dbcp.xml" );
     
         DataSource dataSource = ctx.getBean(DataSource. class );
         
         System.out.println(dataSource);
    }
}

猜你喜欢

转载自blog.csdn.net/wjq008/article/details/49250505
今日推荐