Spring框架学习记录 4 配置数据源

配置数据源

(1)导入相关依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.25</version>
</dependency>

<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.5</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.6</version>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

(2)创建数据源连接测试类 DataSourceTest

对于数据库连接池 c3p0 和 druid 的创建,首先需要创建数据源对象,然后设置连接信息,最后获取连接资源即可,使用结束后需归还资源,这两种连接池的创建步骤一致,相关方法名有些许不同

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class DataSourceTest {

	@Test
	public void createC3p0() throws PropertyVetoException, SQLException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/xyz");
		dataSource.setUser("root");
		dataSource.setPassword("admin");
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		connection.close();
	}

	@Test
	public void createDruid() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/xyz");
		dataSource.setUsername("root");
		dataSource.setPassword("admin");
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		connection.close();

	}

}

连接信息解耦

(1)将连接信息保存在 resources 下的 jdbc.properties 中

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xyz
jdbc.username=root
jdbc.password=admin

(2)使用 ResourceBundle 读取 properties 配置文件,创建数据源连接

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

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


public class DataSourceTest {

	@Test
	public void createC3p0() throws PropertyVetoException, SQLException {
		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
	public void createDruid() throws SQLException {
		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");
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(driver);
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		Connection connection = dataSource.getConnection();
		System.out.println(connection);
		connection.close();
	}

}

getBundle() 方法需要传入待加载的 properties 文件名(不需要扩展名),这里的路径是相对于类的根路径而言的


Spring 配置数据源

(1)加载 properties 文件,读取连接信息

对于 properties 文件的读取,首先声明 context 命名空间,在 xml 配置文件的 <beans> 标签上增加如下代码

xmlns:context="http://www.springframework.org/schema/context"

以及在 xsi:schemaLocation 后面增加

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

使用 <context:property-placeholder> 标签加载 properties 文件,用 location 指定文件路径,这里的路径是相对于类的根路径而言的

<context:property-placeholder location="jdbc.properties"/>

(2)在 xml 配置文件中配置数据源对象的 bean ,并设置连接信息

使用 EL 表达式将 properties 文件中的连接信息传给对应的 bean 的属性

<bean id="dataSource_c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<bean id="dataSource_druid" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

完整 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"
       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">

    <context:property-placeholder location="jdbc.properties"/>

    <bean id="dataSource_c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id="dataSource_druid" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
</beans>

猜你喜欢

转载自blog.csdn.net/qq_25274377/article/details/120295981