1.数据源(连接池)的作用
- 数据源(连接池)是提高程序性能出现的
- 事先实例化数据源,初始化部分连接资源
- 使用连接资源时从数据源中获取
- 使用完毕后将连接资源归还给数据源
常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid
2.手动创建C3P0的数据源
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.company</groupId> <artifactId>spring_ioc_anno</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
DataSourceTest.java
package com.company.test; 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 // 测试手动创建c3p0数据源 public void test1() throws PropertyVetoException, SQLException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db1"); dataSource.setUser("root"); dataSource.setPassword("123456"); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } }
3.手动创建Druid的数据源
手动创建Druid数据源
@Test // 测试手动创建druid数据源 public void test2() throws SQLException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/db1"); dataSource.setUsername("root"); dataSource.setPassword("123456"); DruidPooledConnection druidPooledConnection = dataSource.getConnection(); System.out.println(druidPooledConnection); druidPooledConnection.close(); }
4.Spring产生数据源对象
可以将DataSource的创建权交给Spring容器去完成
(1)Spring容器产生c3p0数据源对象
@Test // 测试Spring容器产生c3p0数据源对象 public void test3() throws SQLException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = applicationContext.getBean(DataSource.class); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); }
applicationContext.xml
(2)Spring容器产生druid数据源对象
@Test //测试Spring容器产生druid数据源对象 public void test4() throws SQLException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = applicationContext.getBean(DataSource.class); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); }
applicationContext.xml
(3)applicationContext.xml中加载外部的properties文件
首先,需要引入context命名空间和约束路径:
命名空间:xmlns:context="http://www.springframework.org/schema/context"
约束路径:http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
Spring容器加载properties文件:
如果这个文件在resources(类加载路径)下,前面加classpath。
用SpEL(spring表达式)可以获取Spring容器中那个key指定的值
jdbc_c3p0.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/db1 jdbc.username=root jdbc.password=123456
完整代码:
package com.company.test; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledConnection; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; public class DataSourceTest { @Test // 测试手动创建c3p0数据源 public void test1() throws PropertyVetoException, SQLException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db1"); dataSource.setUser("root"); dataSource.setPassword("123456"); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } @Test // 测试手动创建druid数据源 public void test2() throws SQLException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/db1"); dataSource.setUsername("root"); dataSource.setPassword("123456"); DruidPooledConnection druidPooledConnection = dataSource.getConnection(); System.out.println(druidPooledConnection); druidPooledConnection.close(); } @Test // 测试Spring容器产生c3p0数据源对象 public void test3() throws SQLException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = applicationContext.getBean(DataSource.class); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } @Test //测试Spring容器产生druid数据源对象 public void test4() throws SQLException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = applicationContext.getBean(DataSource.class); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } }