Spring配置數(shù)據(jù)源的三種方式(小結(jié))
一、前言
今天學(xué)習(xí)了用spring配置Druid數(shù)據(jù)源的三種方式,整理了學(xué)習(xí)筆記,希望大家喜歡!
二、數(shù)據(jù)源的作用
- 數(shù)據(jù)源(連接池)是提高程序性能如出現(xiàn)的
- 事先實(shí)例化數(shù)據(jù)源,初始化部分連接資源
- 使用連接資源時從數(shù)據(jù)源中獲取
- 使用完畢后將連接資源歸還給數(shù)據(jù)源
常見的數(shù)據(jù)源:DBCP、C3P0、BoneCP、Druid等等,本文主要以Druid數(shù)據(jù)源為案例實(shí)現(xiàn)Spring對數(shù)據(jù)源的開發(fā)應(yīng)用
三、開發(fā)數(shù)據(jù)源的方式
方式1:手動輸入
先創(chuàng)建一個maven工程,引入依賴,為了方便起見,我還導(dǎo)入了Junit的依賴,此外,還有mysql的驅(qū)動依賴、Druid數(shù)據(jù)源的依賴和spring依賴
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
</dependencies>
直接編寫一個測試類,開始測試
@Test
public void test1() throws SQLException {
//創(chuàng)建數(shù)據(jù)源對象
DruidDataSource dataSource = new DruidDataSource();
//設(shè)置數(shù)據(jù)源的基本連接數(shù)據(jù)
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("0315");
//使用數(shù)據(jù)源獲取連接資源
Connection connection = dataSource.getConnection();
//打印連接資源的信息
System.out.println(connection);
//關(guān)閉連接資源
connection.close();
}
分析: setDriverClassName()填入的是連接驅(qū)動類Driver的包路徑、setUrl()設(shè)置要連接的數(shù)據(jù)庫的地址、setUsername()自己的數(shù)據(jù)庫用戶名、setPassword()數(shù)據(jù)庫密碼
運(yùn)行結(jié)果:

方式2:Properties配置文件
在resources下建一個名為jdbc.properties的文件,填入數(shù)據(jù)源的基本連接數(shù)據(jù)
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=0315
編寫一個測試類,開始測試
@Test
public void test2() throws SQLException {
//ResourceBundle這個類專門用來讀取properties類型的文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
//設(shè)置數(shù)據(jù)源的基本連接數(shù)據(jù)
String driver = bundle.getString("jdbc.driver");
String url = bundle.getString("jdbc.url");
String username = bundle.getString("jdbc.username");
String password = bundle.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();
}
這種方式就比方式一好很多了,如果我們使用的數(shù)據(jù)庫發(fā)生了改變,就只需要在Properties文件中進(jìn)行修改,從而不需要從代碼中修改,提高了開發(fā)的效率
方式3:Spring配置數(shù)據(jù)源
繼續(xù)使用前面的jdbc.properties文件,我們可以將數(shù)據(jù)源的創(chuàng)建權(quán)交由Spring容器去完成,編寫一個名為applicationContext.xml的spring配置文件,把數(shù)據(jù)源放入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/test"></property>
<property name="username" value="root"></property>
<property name="password" value="0315"></property>
</bean>
</beans>
通過這種spring配置文件的方式,我們就可以獲取了數(shù)據(jù)源,接下來寫一個代碼用來測試
@Test
public void test3() throws SQLException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DruidDataSource dataSource = applicationContext.getBean(DruidDataSource.class);
DruidPooledConnection connection = dataSource.getConnection();
//打印連接信息
System.out.println(connection);
connection.close();
}
運(yùn)行結(jié)果:

不知道小伙伴們看到value的屬性值那么長,有沒有感覺到一絲絲的不舒服,反正我是有。那么有沒有一種方法能夠?qū)⑴渲酶拥那逦髁四??答案是:有!那么該如何做呢?/p>
首先要做的是,把jdbc.properties配置文件的對象放進(jìn)spring容器中,這樣就方便了以后的調(diào)用,具體代碼:
<?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">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>
分析: 首先要在頭文件中引入下圖所示的名稱空間,最后value的屬性值用${key}的方式獲取到jdbc.properties的value值,這樣的運(yùn)行結(jié)果也是跟上面一樣

四、總結(jié)
我們最需要掌握的就是最后一種方法,一定要學(xué)會這種配置方式!

到此這篇關(guān)于Spring配置數(shù)據(jù)源的三種方式(小結(jié))的文章就介紹到這了,更多相關(guān)Spring配置數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot多數(shù)據(jù)源配置并通過注解實(shí)現(xiàn)動態(tài)切換數(shù)據(jù)源
- SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置問題及解決方法
- 使用SpringBoot配置多數(shù)據(jù)源的經(jīng)驗(yàn)分享
- spring注解 @PropertySource配置數(shù)據(jù)源全流程
- springboot配置多個數(shù)據(jù)源兩種方式實(shí)現(xiàn)
- 解決springboot項(xiàng)目不配置數(shù)據(jù)源啟動報錯問題
- springboot整合多數(shù)據(jù)源配置方式
- Spring配置數(shù)據(jù)源流程與作用詳解
相關(guān)文章
Java使用wait/notify實(shí)現(xiàn)線程間通信上篇
wait()和notify()是直接隸屬于Object類,也就是說所有對象都擁有這一對方法,下面這篇文章主要給大家介紹了關(guān)于使用wait/notify實(shí)現(xiàn)線程間通信的相關(guān)資料,需要的朋友可以參考下2022-12-12
Spring AOP在web應(yīng)用中的使用方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Spring AOP在web應(yīng)用中的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring AOP具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
springboot中的controller注意事項(xiàng)說明
這篇文章主要介紹了springboot中的controller注意事項(xiàng)說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

