数据源实际上就是学过的数据库连接池
数据源(连接池)的作用:
①提高程序性能
②事先实例化数据源,初始化部分连接资源
③使用连接资源时从数据源中获取
④使用完毕后将连接资源归还给数据源
步骤:
1、在 pom.xml 文件中导入坐标
2、创建一个测试类,用于测试是否能够成功连接数据库
注意:
加载 properties 配置文件和不加载 properties 配置文件的区别:
如果不加载配置文件,有很多具体的数据库参数信息都设置在代码中,就可以说这些数据库参数信息与当前Java文件耦合在了一起,如果后期我们更换数据库,就需要找到源码修改这些参数信息,项目还要重新部署上线,因此这种方式不可取,而加载配置文件的方式就可以很好地解决这种问题,将这些数据库参数信息抽取出来放到一个配置文件中,以后即使更换数据库,不需要找到源码,只需要在配置文件中修改相应的参数信息即可,这也就使数据库参数信息与Java文件解耦了,降低了耦合性。
2.1 手动创建 c3p0 数据源
@Test
//测试手动创建 c3p0 数据源
public void test1() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sn");
dataSource.setUser("root");
dataSource.setPassword("123");
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mchange.v2.c3p0.impl.NewProxyConnection@63a65a25
2.2 手动创建 c3p0 数据源(加载 properties 配置文件)
2.2.1 首先创建一个 jdbc.properties 配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sn
jdbc.username=root
jdbc.password=123
如图:
2.2.2 使用加载 properties 配置文件的方式手动创建 c3p0 数据源
@Test
//测试手动创建 c3p0 数据源(加载properties配置文件)
public void test2() throws Exception {
//1. 读取配置文件
//下面的参数"jdbc"是上面创建的jdbc.properties配置文件的前半部分
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");
//2. 创建数据源对象,设置连接参数
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
//3. 获取资源
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mchange.v2.c3p0.impl.NewProxyConnection@6dbb137d
2.3 手动创建 druid 数据源
@Test
//测试手动创建 druid 数据源
public void test3() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/sn");
dataSource.setUsername("root");
dataSource.setPassword("123");
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mysql.jdbc.JDBC4Connection@517cd4b
2.4 手动创建 druid 数据源(加载 properties 配置文件)
2.4.1 还使用上面创建的 jdbc.properties 配置文件
2.4.2 使用加载 properties 配置文件的方式手动创建 druid 数据源
@Test
//测试手动创建 druid 数据源(加载properties配置文件)
public void test4() throws Exception {
//下面的参数"jdbc"是上面创建的jdbc.properties配置文件的前半部分
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);
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mysql.jdbc.JDBC4Connection@679b62af
3、Spring 配置数据源
3.1 首先在 pom.xml 文件中导入 spring 的坐标
3.2 创建 applicationContext.xml 配置文件
3.3 applicationContext.xml 配置文件中的配置 c3p0 数据源
3.4 使用 Spring 容器产生 c3p0 数据源对象
@Test
//测试Spring容器产生 c3p0 数据源对象
public void test5() throws Exception{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = app.getBean(DataSource.class);
// DataSource dataSource = (DataSource) app.getBean("dataSource");//这种方式也行
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mchange.v2.c3p0.impl.NewProxyConnection@1c9b0314
3.5 applicationContext.xml 配置文件中的配置 druid 数据源
3.6 使用 Spring 容器产生 druid 数据源对象
@Test
//测试Spring容器产生 druid 数据源对象
public void test7() throws Exception{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource1 = (DataSource) app.getBean("dataSource1");
Connection connection = dataSource1.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mysql.jdbc.JDBC4Connection@5082d622
4、整体的 applicationContext.xml 配置
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">
5、还可以将 applicationContext.xml 配置文件中
5.1 首先创建一个 jdbc.properties 配置文件(还可以使用之前创建过的)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sn
jdbc.username=root
jdbc.password=123
5.2 在 applicationContext.xml 配置文件中引入context命名空间和约束路径
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">
如图:
5.3 加载外部的 jdbc.properties 配置文件,再使用 spring 容器引入外部的 jdbc.properties 配置文件中的值,产生数据源对象
5.4、再次运行上面的 3.4 和 3.6 的代码,依然能正确运行,说明使用这种方式也是可以的