Spring配置连接池和dao使用jdbcTemplate
1、Spring配置c3p0连接池
(1)导入c3p0连接池jar包和spring依赖c3p0使用的jar包
原始链接方式:
ComboPooledDataSource source = new ComboPooledDataSource();
source.setDriverClass("oracle.jdbc.OracleDriver");
source.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:ORCL");
source.setUser("System");
source.setPassword("a5121921");
(2)创建spring配置文件,配置连接池
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入属性值 -->
<property name="driverClass" value="oracle.jdbc.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:ORCL"></property>
<property name="user" value ="System"></property>
<property name="password" value="a5121921"></property>
</bean>
2、dao使用jdbcTemplate
(1)创建dao与service对象,把dao注入service,把jdbcTemplate注入dao,把datasource注入jdbcTemplate
<!-- 配置jdbcTemplate并注入DataSource -->
<bean id = "template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>