spring配置数据源+数据源(连接池)的作用+抽取jdbc配置文件

本文详细介绍Spring框架下数据源(连接池)的配置方法,包括如何通过XML配置文件加载外部properties文件,以及如何使用Spring创建c3p0和Druid数据源。同时,提供了具体的测试代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring配置数据源
  1. 数据源(连接池)的作用

    1. 数据源(连接池)是提高程序性能而出现的
    2. 事先实例化数据源,初始化部分连接资源
    3. 使用连接资源时候从数据源中获取
    4. 使用完毕后将连接资源归还数据源
  2. spring配置数据源

    1. 抽取jdbc配置文件

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      引入context       
             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.xsd">
      <!--加载外部的properties文件-->
          <context:property-placeholder location="classpath:jdbc.properties"/>
          <!--    spring 配置数据源-->
      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <!--    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>-->
      <!--    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>-->
      <!--    <property name="user" value="root"></property>-->
      <!--    <property name="password" value="root"></property>-->
          调取配置文件中的value使用$
          <property name="driverClass" value="${jdbc.driver}"></property>
          <property name="jdbcUrl" value="${jdbc.url}"></property>
          <property name="user" value="${jdbc.username}"></property>
          <property name="password" value="${jdbc.password}"></property>
      </bean>
      </beans>
      
    2. 整体测试过程

      jdbc.driver = com.mysql.jdbc.Driver
      jdbc.url = jdbc:mysql://localhost:3306/test
      jdbc.username = root
      jdbc.password = root
      
package com.iteheima.test;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javafx.application.Application;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class DataSourceTest {
    @Test
    public void test() throws Exception{
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jsbc.driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
    @Test
    // 测试手动创建c3p0数据源
    public void test1() throws PropertyVetoException, SQLException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        // 设置基本的连接参数
        dataSource.setDriverClass("com.mysql.jdbc.driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
    @Test
    // 测试手动创建c3p0数据源(加载配置properties文件形式)
    public void test3() throws PropertyVetoException, SQLException {
        // 读取配置文件 对应的是 resources
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");
        String driver = rb.getString("jdbc.driver");
        String url = rb.getString("jdbc.url");
        String username = rb.getString("jdbc.username");
        String password = rb.getString("jdbc.password");
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        // 设置基本的连接参数
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
    @Test
    // 测试Spring创建c3p0数据源(加载配置properties文件形式)
    public void test4() throws PropertyVetoException, SQLException {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = app.getBean(DataSource.class);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值