Spring配置數(shù)據(jù)源的三種方式(小結(jié))
一、前言
今天學(xué)習(xí)了用spring配置Druid數(shù)據(jù)源的三種方式,整理了學(xué)習(xí)筆記,希望大家喜歡!
二、數(shù)據(jù)源的作用
- 數(shù)據(jù)源(連接池)是提高程序性能如出現(xiàn)的
- 事先實(shí)例化數(shù)據(jù)源,初始化部分連接資源
- 使用連接資源時(shí)從數(shù)據(jù)源中獲取
- 使用完畢后將連接資源歸還給數(shù)據(jù)源
常見的數(shù)據(jù)源:DBCP
、C3P0
、BoneCP
、Druid
等等,本文主要以Druid數(shù)據(jù)源為案例實(shí)現(xiàn)Spring對(duì)數(shù)據(jù)源的開發(fā)應(yīng)用
三、開發(fā)數(shù)據(jù)源的方式
方式1:手動(dòng)輸入
先創(chuàng)建一個(gè)maven工程,引入依賴,為了方便起見,我還導(dǎo)入了Junit的依賴,此外,還有mysql的驅(qū)動(dòng)依賴、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>
直接編寫一個(gè)測(cè)試類,開始測(cè)試
@Test public void test1() throws SQLException { //創(chuàng)建數(shù)據(jù)源對(duì)象 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ū)動(dòng)類Driver的包路徑、setUrl()
設(shè)置要連接的數(shù)據(jù)庫(kù)的地址、setUsername()
自己的數(shù)據(jù)庫(kù)用戶名、setPassword()
數(shù)據(jù)庫(kù)密碼
運(yùn)行結(jié)果:
方式2:Properties配置文件
在resources
下建一個(gè)名為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
編寫一個(gè)測(cè)試類,開始測(cè)試
@Test public void test2() throws SQLException { //ResourceBundle這個(gè)類專門用來(lái)讀取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ù)庫(kù)發(fā)生了改變,就只需要在Properties文件中進(jìn)行修改,從而不需要從代碼中修改,提高了開發(fā)的效率
方式3:Spring配置數(shù)據(jù)源
繼續(xù)使用前面的jdbc.properties
文件,我們可以將數(shù)據(jù)源的創(chuàng)建權(quán)交由Spring容器去完成,編寫一個(gè)名為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>
通過(guò)這種spring配置文件的方式,我們就可以獲取了數(shù)據(jù)源,接下來(lái)寫一個(gè)代碼用來(lái)測(cè)試
@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的屬性值那么長(zhǎng),有沒有感覺到一絲絲的不舒服,反正我是有。那么有沒有一種方法能夠?qū)⑴渲酶拥那逦髁四??答案是:有!那么該如何做呢?/p>
首先要做的是,把jdbc.properties
配置文件的對(duì)象放進(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é)會(huì)這種配置方式!
到此這篇關(guān)于Spring配置數(shù)據(jù)源的三種方式(小結(jié))的文章就介紹到這了,更多相關(guān)Spring配置數(shù)據(jù)源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot多數(shù)據(jù)源配置并通過(guò)注解實(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)
- 解決springboot項(xiàng)目不配置數(shù)據(jù)源啟動(dòng)報(bào)錯(cuò)問題
- springboot整合多數(shù)據(jù)源配置方式
- Spring配置數(shù)據(jù)源流程與作用詳解
相關(guān)文章
Java使用wait/notify實(shí)現(xiàn)線程間通信上篇
wait()和notify()是直接隸屬于Object類,也就是說(shuō)所有對(duì)象都擁有這一對(duì)方法,下面這篇文章主要給大家介紹了關(guān)于使用wait/notify實(shí)現(xiàn)線程間通信的相關(guān)資料,需要的朋友可以參考下2022-12-12Spring AOP在web應(yīng)用中的使用方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Spring AOP在web應(yīng)用中的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring AOP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12springboot中的controller注意事項(xiàng)說(shuō)明
這篇文章主要介紹了springboot中的controller注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03