Spring配置数据源
Spring配置数据源
- 数据源的作用
- 环境准备
- 手动创建c3p0数据源
- 封装抽取关键信息,手动创建c3p0数据源
- 使用Spring容器配置数据源
数据源的作用
数据源(连接池)是提高程序性能如出现的
事先实例化数据源,初始化部分连接资源
使用连接资源时从数据源中获取
使用完毕后将连接资源归还给数据源
常见的数据源有:DBCP、C3P0、BoneCP、Druid
环境准备
idea中创建一个maven项目,导入如下所需的基本坐标(mysql、Junit、spring-contex) 两种数据池可以看自己情况都导或者导入其中一个,Junit看个人需要可导可不导。
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.3</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency></dependencies>
在resources目录下下建一个名为jdbc.properties的文件,
properties文件数据配置如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1
jdbc.username=root
jdbc.password=123456
手动创建c3p0数据源
//手动测试c3p0数据源@Testpublic void test1() throws Exception {//创建数据源对象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();}
封装抽取关键信息,手动创建c3p0数据源
//用文件流的方式进行读取Properties properties = new Properties();properties.load(new FileReader("src/main/resources/jdbc.properties"));String driver = properties.getProperty("jdbc.driver");String url = properties.getProperty("jdbc.url");String username = properties.getProperty("jdbc.username");String password = properties.getProperty("jdbc.password");
第二种加载配置文件方式:
//用resourceBundle进行读取配置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");
任选上面两种方式,读取文件信息
//手动测试c3p0数据源 加载配置文件@Testpublic void test4() throws Exception {/*任选上面两种方式,读取文件信息读取配置文件....*/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();}
使用Spring容器配置数据源
把数据源的创建权交给Spring容器去完成,在resource目录下创建一个SQLConfiguration.xml的Spring配置文件
<?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.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/db1"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean>
</beans>
对应的测试代码如下:
//spring容器创建@Testpublic void test5() throws Exception {ApplicationContext app= new ClassPathXmlApplicationContext("SQLConfiguration.xml");
// Object dataSource = app.getBean("dataSource");DataSource dataSource = app.getBean(DataSource.class);Connection connection = dataSource.getConnection();System.out.println(connection);connection.close();}
上面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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 加载properties配置文件--><context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" 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>
</beans>
将xmlns="http://www.springframework.org/schema/beans"
中的beans改成context,命名空间和约束路径都要进行修改。
注意细节:
spring容器加载properties配置文件方式如下:
<!-- 加载properties配置文件--><context:property-placeholder location="classpath:jdbc.properties"/><property name="" value="${key}" />
配置完成后,即可使用。
参考:https://blog.csdn.net/weixin_59654772/article/details/122549314
视频资料:黑马程序员spring教程第二天
链接地址:视频地址