文章目录
一般方式配置数据源
数据源(连接池)的作用
-
数据源(连接池)是提高程序性能如出现的
-
事先实例化数据源,初始化部分连接资源
-
使用连接资源时从数据源中获取
-
使用完毕后将连接资源归还给数据源
常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等
数据源开发的步骤
- 导入数据源的坐标和数据库驱动坐标
- 创建数据源对象
- 设置数据源的基本连接数据
- 使用数据源获取连接资源和归还连接资源
数据源创建演示(c3p0)
创建maven-web骨架项目
导入mysql、c3p0、druid、junit坐标
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
创建编写测试类
获取mybatis数据库的连接
package com.taotao.test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.beans.PropertyVetoException;
import java.sql.Connection;
/**
* create by 刘鸿涛
* 2022/4/7 0:38
*/
public class DataSourceTest {
@Test
//测试手动创建 c3p0数据源
public void test1() throws Exception {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis?");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("12345");
Connection connection = comboPooledDataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
测试运行
数据源创建演示(druid)
编写测试类
package com.taotao.test;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.beans.PropertyVetoException;
import java.sql.Connection;
/**
* create by 刘鸿涛
* 2022/4/7 0:38
*/
public class DataSourceTest {
@Test
//测试手动创建 c3p0数据源
public void test1() throws Exception {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis?");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("12345");
Connection connection = comboPooledDataSource.getConnection();
System.out.println(connection);
connection.close();
}
@Test
//测试手动创建 druid数据源
public void test2() throws Exception {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql://localhost:3306/mybatis?");
druidDataSource.setUsername("root");
druidDataSource.setPassword("12345");
DruidPooledConnection connection = druidDataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
测试运行
抽出耦合的数据源
新建文件
test - java - resources - jdbc.properties
编写测试类,加载properties配置文件
@Test
//测试手动创建 c3p0数据源(加载properties配置文件)
public void test3() throws Exception {
//读取配置文件
//ResourceBudle类是jdk util包自带的
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 comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass(driver);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(username);
comboPooledDataSource.setPassword(password);
Connection connection = comboPooledDataSource.getConnection();
System.out.println(connection);
connection.close();
}
测试运行
Spring配置数据源
可以将DataSource的创建权交由Spring容器去完成
导入Spring坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
创建编写Spring.xml
text-resources
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<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/mybatis?"></property>
<property name="user" value="root"></property>
<property name="password" value="12345"></property>
</bean>
</beans>
编写测试类
@Test
//测试Spring容器产生数据源对象
public void test4() throws Exception{
//获得Spring xml文件
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//使用id
DataSource dataSource = (DataSource) app.getBean("dataSource");
//反射获取,不太安全,比较方便
// DataSource dataSource = app.getBean(DataSource.class);
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
测试运行
抽取jdbc配置文件
applicationContext.xml加载jdbc.properties配置文件获取连接信息
首先,需要引入context命名空间和约束路径:
编写Spring.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">
<!-- 加载外部的properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<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>
测试运行
总结
- 配置完后,提高耦合性,后期维护起来也相对简单,只需更改properties配置文件,即可对整个项目jdbc连接做出贡献