上一篇文章提到过DriverManagerDataSource只是Spring内置的数据库连接池,我们可选的方案还有c3p0数据库连接池以及DBCP数据库连接池。

所以这篇文章讲一下上面三种数据库连接池的配置文件的形式。

 

 

 

第一种:DriverManagerDataSource:Spring内置的数据库连接池。

 

第一步:编写配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


<!-- 配置spring 本身自带的数据库连接池 

这一步相当于
//1.创建数据库连接池,使用spring内置的连接池
    DriverManagerDataSource dataSource=new DriverManagerDataSource();
    //连接数据库驱动
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql:///spring3_day2");
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    


-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 初始化dataSource里面的参数 -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql:///spring3_day2"></property>
 <property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>

<!-- 构建一个jdbcTemplate的模板对象 ,这个模板对象里面有dataSource这个变量,这里给他赋予初始值
这一步相当于: //2.通过连接池构造模板对象
             JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);


-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>


</bean>




</beans>

第二步:编写Junit测试代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")

public class Jdbctest {
    //为了注解(根据类型注解的),不加这个注释的话 Spring怎么给jdbcTemplate赋值
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
    public void jdbcTemplatetest()
    {
        jdbcTemplate.execute("create table person2(id int primary key,name varchar(20))");
        
        
    }
    

}

结果:

创建了一个person2的表。

 

 

 

第二种:Apache的Dbcp连接池

第一步:从下载的依赖包(spring-framework-3.0.2.RELEASE-dependencies下的org.apache.commons里面)里导入Dbcp连接池的jar包。

21Spring_JdbcTemplatem模板工具类的使用——配置文件(连接三种数据库连接池)

第二步:在配置文件中配置数据库连接

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql:///spring3_day2"></property>
 <property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean> 

其他什么都不用改。

结果和以前的一样。

 

 

 

 

 

第三种:C3P0的连接池

第一步:从Spring的依赖库里找jar包。

21Spring_JdbcTemplatem模板工具类的使用——配置文件(连接三种数据库连接池)

第二步:在配置文件中配置数据库连接

 <bean ></property>
</bean>

其他什么都不用改。注意我用红色标出来的,这些都是和之前的数据库连接池不同的地方。去看C3P0的源代码就会发现,里面的成员变量就是这么定义的。

结果和以前的一样。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-17
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
相关资源
相似解决方案