Spring配置數(shù)據(jù)源流程與作用詳解
一、數(shù)據(jù)源的作用
- 數(shù)據(jù)源(連接池)是提高程序性能出現(xiàn)的
- 事先實(shí)例化數(shù)據(jù)源,初始化部分連接資源
- 使用連接資源時(shí)從數(shù)據(jù)源中獲取
- 使用完畢后將連接資源歸還給數(shù)據(jù)源
常見的數(shù)據(jù)源(連接池):DBCP、C3P0、BoneCP、Druid等
在JavaSE中的JDBC就是通過數(shù)據(jù)源獲取數(shù)據(jù)庫中的數(shù)據(jù)
二、數(shù)據(jù)源手動(dòng)創(chuàng)建
1、數(shù)據(jù)源的開發(fā)步驟
導(dǎo)入數(shù)據(jù)源的坐標(biāo)和數(shù)據(jù)庫的坐標(biāo)
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>創(chuàng)建數(shù)據(jù)源對(duì)象
設(shè)計(jì)數(shù)據(jù)源的基本連接數(shù)據(jù)
使用數(shù)據(jù)源獲取連接資源和歸還連接資源
2、手動(dòng)創(chuàng)建c3p0數(shù)據(jù)源
@Test
public void test01() throws PropertyVetoException, SQLException {
//創(chuàng)建數(shù)據(jù)源
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//設(shè)置數(shù)據(jù)庫連接參數(shù)
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/atm");
dataSource.setUser("root");
dataSource.setPassword("123456");
//獲取連接對(duì)象
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}3、手動(dòng)創(chuàng)建druid數(shù)據(jù)源
@Test
public void test02() throws SQLException {
//創(chuàng)建數(shù)據(jù)源
DruidDataSource dataSource = new DruidDataSource();
//設(shè)置數(shù)據(jù)庫連接參數(shù)
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/atm");
dataSource.setUsername("root");
dataSource.setPassword("123456");
//獲取連接對(duì)象
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
4、通過properties配置文件創(chuàng)建連接池
上述手動(dòng)創(chuàng)建數(shù)據(jù)源的過程中顯而易見出現(xiàn)一個(gè)問題,那就是代碼的維護(hù)性較差,倘若需要修改數(shù)據(jù)庫的數(shù)據(jù),當(dāng)java程序進(jìn)行編寫就變成了字節(jié)碼文件,修改字節(jié)碼文件是比較困難的,不利于系統(tǒng)的維護(hù)
通過使用properties配置文件來創(chuàng)建連接池就可以較好地解決此類問題
在resources文件夾下創(chuàng)建相對(duì)應(yīng)的properties配置文件,在里面編寫數(shù)據(jù)庫連接參數(shù)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/atm
jdbc.username=root
jdbc.password=123456
在java程序中獲取properties配置文件的數(shù)據(jù),并給它設(shè)置數(shù)據(jù)庫連接參數(shù)
@Test
public void test3() throws Exception {
//加載類路徑下的jdbc.properties
ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
//創(chuàng)建數(shù)據(jù)源
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//設(shè)置數(shù)據(jù)庫連接參數(shù)
dataSource.setDriverClass(jdbc.getString("jdbc.driver"));
dataSource.setJdbcUrl(jdbc.getString("jdbc.url"));
dataSource.setUser(jdbc.getString("jdbc.username"));
dataSource.setPassword(jdbc.getString("jdbc.password"));
//獲取連接對(duì)象
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
5、通過spring配置數(shù)據(jù)源
在spring框架中,spring框架的容器可以完成DataSource的創(chuàng)建
- DataSource有無參數(shù)構(gòu)造方法,而spring默認(rèn)是通過無參構(gòu)造方法實(shí)例化對(duì)象
- DataSource要想使用通過set方法來設(shè)置數(shù)據(jù)庫連接信息,spring是可以通過set方法進(jìn)行字符串的注入
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/> <property name="user" value="root"/> <property name="password" value="123456"/> </bean>
配置完spring的配置文件的,就可以在java程序中獲取bean容器返回的DataSource對(duì)象
@Test
public void test4() throws SQLException {
ApplicationContext app = new ClassPathXmlApplicationContext("applicatonContext.xml");
DataSource dataSource = (DataSource) app.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
6、通過spring抽取jdbc配置文件
在實(shí)際開發(fā)過程中,并不習(xí)慣在spring配置文件中把數(shù)據(jù)庫的相關(guān)參數(shù)寫死,而是將它寫在properties配置文件中,spring配置文件通過抽取properties配置文件從而獲取數(shù)據(jù)庫相關(guān)參數(shù)
要先抽取properties配置文件的相關(guān)數(shù)據(jù),首先要引入context命名空間和約束路徑
命名空間:xmlns:context=“http://www.springframework.org/schema/context”
約束路徑:http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

<context:property-placeholder location="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>
erty name=“jdbcUrl” value=" j d b c . u r l " / > < p r o p e r t y n a m e = " u s e r " v a l u e = " {jdbc.url}"/> <property name="user" value=" jdbc.url"/><propertyname="user"value="{jdbc.username}"/>
> **相關(guān)參數(shù)就像javaweb中的EL表達(dá)是那些編寫,即可獲取相對(duì)應(yīng)的數(shù)據(jù)**
到此這篇關(guān)于Spring配置數(shù)據(jù)源流程與作用詳解的文章就介紹到這了,更多相關(guān)Spring配置數(shù)據(jù)源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot多數(shù)據(jù)源配置并通過注解實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源
- SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置問題及解決方法
- 使用SpringBoot配置多數(shù)據(jù)源的經(jīng)驗(yàn)分享
- spring注解 @PropertySource配置數(shù)據(jù)源全流程
- springboot配置多個(gè)數(shù)據(jù)源兩種方式實(shí)現(xiàn)
- Spring配置數(shù)據(jù)源的三種方式(小結(jié))
- 解決springboot項(xiàng)目不配置數(shù)據(jù)源啟動(dòng)報(bào)錯(cuò)問題
- springboot整合多數(shù)據(jù)源配置方式
相關(guān)文章
AJAX省市區(qū)三級(jí)聯(lián)動(dòng)下拉菜單(java版)
這篇文章主要介紹了AJAX省市區(qū)三級(jí)聯(lián)動(dòng)下拉菜單(java版)的相關(guān)資料,需要的朋友可以參考下2016-01-01
Java中的notyfy()和notifyAll()的本質(zhì)區(qū)別
很多朋友對(duì)java中的notyfy()和notifyAll()的本質(zhì)區(qū)別不了解,今天小編抽空給大家整理一篇教程關(guān)于Java中的notyfy()和notifyAll()的本質(zhì)區(qū)別,需要的朋友參考下吧2017-02-02
微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn)UV 數(shù)據(jù)統(tǒng)計(jì)的詳
這篇文章主要介紹了微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn) UV 數(shù)據(jù)統(tǒng)計(jì),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
mysql+spring+mybatis實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離的代碼配置
今天小編就為大家分享一篇關(guān)于mysql+spring+mybatis實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離的代碼配置,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03

